weixin_authorize 1.6.0 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/Gemfile +4 -5
- data/README.md +19 -8
- data/lib/weixin_authorize.rb +31 -13
- data/lib/weixin_authorize/api.rb +3 -0
- data/lib/weixin_authorize/api/data_cube.rb +8 -0
- data/lib/weixin_authorize/api/mass.rb +78 -0
- data/lib/weixin_authorize/api/media.rb +50 -4
- data/lib/weixin_authorize/api/oauth.rb +38 -0
- data/lib/weixin_authorize/api/template.rb +34 -0
- data/lib/weixin_authorize/api/user.rb +18 -0
- data/lib/weixin_authorize/client.rb +45 -16
- data/lib/weixin_authorize/config.rb +12 -3
- data/lib/weixin_authorize/handler/result_handler.rb +1 -0
- data/lib/weixin_authorize/js_ticket/object_store.rb +21 -0
- data/lib/weixin_authorize/js_ticket/redis_store.rb +41 -0
- data/lib/weixin_authorize/js_ticket/store.rb +40 -0
- data/lib/weixin_authorize/token/object_store.rb +25 -0
- data/lib/weixin_authorize/token/redis_store.rb +35 -0
- data/lib/weixin_authorize/token/store.rb +72 -0
- data/lib/weixin_authorize/version.rb +1 -1
- data/spec/1_fetch_access_token_spec.rb +0 -1
- data/spec/2_fetch_jsticket_spec.rb +10 -0
- data/spec/api/custom_spec.rb +0 -2
- data/spec/api/groups_spec.rb +0 -1
- data/spec/api/mass_spec.rb +65 -0
- data/spec/api/media_spec.rb +1 -3
- data/spec/api/menu_spec.rb +0 -2
- data/spec/api/qrcode_spec.rb +3 -5
- data/spec/api/user_spec.rb +5 -2
- data/spec/spec_helper.rb +7 -4
- data/weixin_authorize.gemspec +5 -5
- metadata +31 -20
- data/lib/weixin_authorize/adapter/client_storage.rb +0 -23
- data/lib/weixin_authorize/adapter/redis_storage.rb +0 -34
- data/lib/weixin_authorize/adapter/storage.rb +0 -73
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7dcf07084b174215116839f53e3933a8360cd975
|
4
|
+
data.tar.gz: 7e92dc7924d59e62f8f3f84f22b29947e2468c28
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b42caf728256adac156fabb2c4ebfe62b37a1b7b3d3916713164c6ec761d4d2de5863904e38c96d2d4c23b6698551c35849abd79007c0a9586a5dafddd0c914b
|
7
|
+
data.tar.gz: 96acbc6e7563d3a44bdea2b9063e3b9fd1f43a00219cfe0d6ad91577c734a82dac144cec11fbb257917e1ca63396c515a5aa2f783c8815d2c33b867f0a09f6af
|
data/.rspec
CHANGED
data/Gemfile
CHANGED
@@ -1,16 +1,15 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
group :test, :development do
|
4
|
-
gem "rspec"
|
5
|
-
gem
|
6
|
-
gem "rake", "~> 0.9.6"
|
4
|
+
gem "rspec"
|
5
|
+
gem 'redis-namespace'
|
7
6
|
gem 'simplecov', '~> 0.7.1', :require => false
|
8
7
|
gem "codeclimate-test-reporter", require: nil
|
9
8
|
gem 'coveralls', require: false
|
10
9
|
# For debugger
|
11
|
-
gem "pry-rails"
|
10
|
+
gem "pry-rails"
|
12
11
|
|
13
|
-
gem "pry-
|
12
|
+
gem "pry-byebug"
|
14
13
|
end
|
15
14
|
|
16
15
|
# Specify your gem's dependencies in weixin_authorize.gemspec
|
data/README.md
CHANGED
@@ -7,24 +7,35 @@
|
|
7
7
|
|
8
8
|
Support using [Redis](http://redis.io) to store `access_token`
|
9
9
|
|
10
|
-
[
|
10
|
+
[Wiki](https://github.com/lanrion/weixin_authorize/wiki)
|
11
11
|
|
12
12
|
[Getting-Started](https://github.com/lanrion/weixin_authorize/wiki/Getting-Started)
|
13
13
|
|
14
|
+
## V2.0开发中:
|
15
|
+
https://github.com/lanrion/weixin_authorize/milestones/v2.0-dev
|
16
|
+
|
17
|
+
1. 重构API实现,调用方式
|
18
|
+
2. 对token,ticket的管理,提供第三方开发灵活者自助化
|
19
|
+
3. 尝试RestClient的弃用,选择更高效的HTTP client包
|
20
|
+
4. 支持更多的异常处理机制
|
21
|
+
|
14
22
|
注意:查看Wiki或者源代码时,请切换对应的版本来查看。Master处于不断更新完善分支。
|
15
23
|
|
16
24
|
## How to test
|
17
25
|
|
18
|
-
Go to https://github.com/lanrion/weixin_authorize/issues/2, apply a weixin sandbox test account and follow this account
|
26
|
+
Go to https://github.com/lanrion/weixin_authorize/issues/2, apply a weixin sandbox test account and follow this account
|
19
27
|
|
28
|
+
https://github.com/lanrion/weixin_authorize/blob/master/spec/spec_helper.rb
|
29
|
+
|
30
|
+
change your infos:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
ENV["APPID"]="wxe371e0960de5426a"
|
34
|
+
ENV["APPSECRET"]="572b93d3d20aea242692a804243a141b"
|
35
|
+
ENV["OPENID"]="oEEoyuEasxionjR5HygmEOQGwRcw"
|
20
36
|
```
|
21
|
-
export APPID="your test account weixin app_id"
|
22
|
-
export APPSECRET="your test account weixin appsecret"
|
23
|
-
export OPENID="your weixin openid"
|
24
|
-
```
|
25
|
-
Last, you have to **open a new terminal tag (Reload bash_profile)** , and run `rspec .`
|
26
37
|
|
27
|
-
|
38
|
+
then run `rspec .`
|
28
39
|
|
29
40
|
## Contributing
|
30
41
|
|
data/lib/weixin_authorize.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
require "rest-client"
|
2
|
-
|
3
2
|
require "carrierwave"
|
4
|
-
require "weixin_authorize/carrierwave/weixin_uploader"
|
5
|
-
|
6
3
|
require 'yajl/json_gem'
|
7
4
|
|
5
|
+
require "weixin_authorize/carrierwave/weixin_uploader"
|
8
6
|
require "weixin_authorize/config"
|
9
7
|
require "weixin_authorize/handler"
|
10
8
|
require "weixin_authorize/api"
|
@@ -12,26 +10,38 @@ require "weixin_authorize/client"
|
|
12
10
|
|
13
11
|
module WeixinAuthorize
|
14
12
|
|
15
|
-
#
|
16
|
-
|
17
|
-
|
18
|
-
|
13
|
+
# token store
|
14
|
+
module Token
|
15
|
+
autoload(:Store, "weixin_authorize/token/store")
|
16
|
+
autoload(:ObjectStore, "weixin_authorize/token/object_store")
|
17
|
+
autoload(:RedisStore, "weixin_authorize/token/redis_store")
|
18
|
+
end
|
19
|
+
|
20
|
+
module JsTicket
|
21
|
+
autoload(:Store, "weixin_authorize/js_ticket/store")
|
22
|
+
autoload(:ObjectStore, "weixin_authorize/js_ticket/object_store")
|
23
|
+
autoload(:RedisStore, "weixin_authorize/js_ticket/redis_store")
|
24
|
+
end
|
19
25
|
|
20
|
-
OK_MSG
|
21
|
-
OK_CODE
|
22
|
-
GRANT_TYPE = "client_credential"
|
26
|
+
OK_MSG = "ok"
|
27
|
+
OK_CODE = 0
|
28
|
+
GRANT_TYPE = "client_credential"
|
23
29
|
|
24
30
|
class << self
|
25
31
|
|
26
32
|
def http_get_without_token(url, headers={}, endpoint="plain")
|
27
33
|
get_api_url = endpoint_url(endpoint, url)
|
28
|
-
load_json(
|
34
|
+
load_json(resource(get_api_url).get(params: headers))
|
29
35
|
end
|
30
36
|
|
31
37
|
def http_post_without_token(url, payload={}, headers={}, endpoint="plain")
|
32
38
|
post_api_url = endpoint_url(endpoint, url)
|
33
39
|
payload = JSON.dump(payload) if endpoint == "plain" # to json if invoke "plain"
|
34
|
-
load_json(
|
40
|
+
load_json(resource(post_api_url).post(payload, params: headers))
|
41
|
+
end
|
42
|
+
|
43
|
+
def resource(url)
|
44
|
+
RestClient::Resource.new(url, rest_client_options)
|
35
45
|
end
|
36
46
|
|
37
47
|
# return hash
|
@@ -47,7 +57,11 @@ module WeixinAuthorize
|
|
47
57
|
end
|
48
58
|
|
49
59
|
def plain_endpoint
|
50
|
-
"
|
60
|
+
"#{api_endpoint}/cgi-bin"
|
61
|
+
end
|
62
|
+
|
63
|
+
def api_endpoint
|
64
|
+
"https://api.weixin.qq.com"
|
51
65
|
end
|
52
66
|
|
53
67
|
def file_endpoint
|
@@ -58,6 +72,10 @@ module WeixinAuthorize
|
|
58
72
|
"https://mp.weixin.qq.com/cgi-bin#{url}"
|
59
73
|
end
|
60
74
|
|
75
|
+
def open_endpoint(url)
|
76
|
+
"https://open.weixin.qq.com#{url}"
|
77
|
+
end
|
78
|
+
|
61
79
|
end
|
62
80
|
|
63
81
|
end
|
data/lib/weixin_authorize/api.rb
CHANGED
@@ -4,3 +4,6 @@ require "weixin_authorize/api/custom"
|
|
4
4
|
require "weixin_authorize/api/groups"
|
5
5
|
require "weixin_authorize/api/qrcode"
|
6
6
|
require "weixin_authorize/api/media"
|
7
|
+
require "weixin_authorize/api/mass"
|
8
|
+
require "weixin_authorize/api/oauth"
|
9
|
+
require "weixin_authorize/api/template"
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module WeixinAuthorize
|
3
|
+
module Api
|
4
|
+
module Mass
|
5
|
+
|
6
|
+
MSG_TYPE = ["mpnews", "image", "text", "voice", "mpvideo"]
|
7
|
+
|
8
|
+
# media_info= {"media_id" media_id}
|
9
|
+
# https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN
|
10
|
+
def mass_with_group(group_id, media_info, msgtype="mpnews")
|
11
|
+
group_option = {"filter" => {"group_id" => group_id.to_s}}
|
12
|
+
media = generate_media(msgtype, media_info, group_option)
|
13
|
+
|
14
|
+
mass_url = "#{mass_base_url}/sendall"
|
15
|
+
http_post(mass_url, media)
|
16
|
+
end
|
17
|
+
|
18
|
+
# https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN
|
19
|
+
# if mpvideo,
|
20
|
+
# media_info= {"media_id" => media_id, "title" => "title", "description" => "description"}
|
21
|
+
def mass_with_openids(openids, media_info, msgtype="mpnews")
|
22
|
+
openid_option = {"touser" => openids}
|
23
|
+
media = generate_media(msgtype, media_info, openid_option)
|
24
|
+
mass_url = "#{mass_base_url}/send"
|
25
|
+
http_post(mass_url, media)
|
26
|
+
end
|
27
|
+
|
28
|
+
# 请注意,只有已经发送成功的消息才能删除删除消息只是将消息的图文详情页失效,已经收到的用户,还是能在其本地看到消息卡片。
|
29
|
+
# 另外,删除群发消息只能删除图文消息和视频消息,其他类型的消息一经发送,无法删除。
|
30
|
+
def mass_delete_with_msgid(msg_id)
|
31
|
+
mass_url = "#{mass_base_url}/delete"
|
32
|
+
http_post(mass_url, {"msg_id" => msg_id})
|
33
|
+
end
|
34
|
+
|
35
|
+
# 预览接口【订阅号与服务号认证后均可用】
|
36
|
+
def mass_preview(openid, media_info, msgtype="mpnews")
|
37
|
+
openid_option = {"touser" => openid}
|
38
|
+
media = generate_media(msgtype, media_info, openid_option)
|
39
|
+
mass_url = "#{mass_base_url}/preview"
|
40
|
+
http_post(mass_url, media)
|
41
|
+
end
|
42
|
+
|
43
|
+
# 查询群发消息发送状态【订阅号与服务号认证后均可用】
|
44
|
+
def mass_get_status(msg_id)
|
45
|
+
mass_url = "#{mass_base_url}/get"
|
46
|
+
http_post(mass_url, {"msg_id" => msg_id})
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def mass_base_url
|
52
|
+
"/message/mass"
|
53
|
+
end
|
54
|
+
|
55
|
+
def generate_media(msgtype, media_info, option)
|
56
|
+
msgtype = msgtype.to_s
|
57
|
+
raise "#{msgtype} is a valid msgtype" if not MSG_TYPE.include?(msgtype)
|
58
|
+
{
|
59
|
+
msgtype => convert_media_info(msgtype, media_info),
|
60
|
+
"msgtype" => msgtype
|
61
|
+
}.merge(option)
|
62
|
+
end
|
63
|
+
|
64
|
+
# 如果用户填写的media信息,是字符串,则转换来符合的数据结构,如果 是hash,则直接使用用户的结构。
|
65
|
+
def convert_media_info(msgtype, media_info)
|
66
|
+
if media_info.is_a?(String)
|
67
|
+
if msgtype == "text"
|
68
|
+
return {"content" => media_info}
|
69
|
+
else
|
70
|
+
return {"media_id" => media_info}
|
71
|
+
end
|
72
|
+
end
|
73
|
+
media_info
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -22,8 +22,58 @@ module WeixinAuthorize
|
|
22
22
|
download_media_url
|
23
23
|
end
|
24
24
|
|
25
|
+
# 上传图文消息素材, 主要用于群发消息接口
|
26
|
+
# {
|
27
|
+
# "articles": [
|
28
|
+
# {
|
29
|
+
# "thumb_media_id":"mwvBelOXCFZiq2OsIU-p",
|
30
|
+
# "author":"xxx",
|
31
|
+
# "title":"Happy Day",
|
32
|
+
# "content_source_url":"www.qq.com",
|
33
|
+
# "content":"content",
|
34
|
+
# "digest":"digest"
|
35
|
+
# },
|
36
|
+
# {
|
37
|
+
# "thumb_media_id":"mwvBelOXCFZiq2OsIU-p",
|
38
|
+
# "author":"xxx",
|
39
|
+
# "title":"Happy Day",
|
40
|
+
# "content_source_url":"www.qq.com",
|
41
|
+
# "content":"content",
|
42
|
+
# "digest":"digest"
|
43
|
+
# }
|
44
|
+
# ]
|
45
|
+
# }
|
46
|
+
# Option: author, content_source_url
|
47
|
+
def upload_mass_news(news=[])
|
48
|
+
upload_news_url = "#{media_base_url}/uploadnews"
|
49
|
+
http_post(upload_news_url, {articles: news})
|
50
|
+
end
|
51
|
+
|
52
|
+
# media_id: 需通过基础支持中的上传下载多媒体文件来得到
|
53
|
+
# https://file.api.weixin.qq.com/cgi-bin/media/uploadvideo?access_token=ACCESS_TOKEN
|
54
|
+
|
55
|
+
# return:
|
56
|
+
# {
|
57
|
+
# "type":"video",
|
58
|
+
# "media_id":"IhdaAQXuvJtGzwwc0abfXnzeezfO0NgPK6AQYShD8RQYMTtfzbLdBIQkQziv2XJc",
|
59
|
+
# "created_at":1398848981
|
60
|
+
# }
|
61
|
+
def upload_mass_video(media_id, title="", desc="")
|
62
|
+
video_msg = {
|
63
|
+
"media_id" => media_id,
|
64
|
+
"title" => title,
|
65
|
+
"description" => desc
|
66
|
+
}
|
67
|
+
|
68
|
+
http_post("#{media_base_url}/uploadvideo", video_msg)
|
69
|
+
end
|
70
|
+
|
25
71
|
private
|
26
72
|
|
73
|
+
def media_base_url
|
74
|
+
"/media"
|
75
|
+
end
|
76
|
+
|
27
77
|
def process_file(media)
|
28
78
|
return media if media.is_a?(File) && jpep?(media)
|
29
79
|
|
@@ -41,10 +91,6 @@ module WeixinAuthorize
|
|
41
91
|
file
|
42
92
|
end
|
43
93
|
|
44
|
-
def media_base_url
|
45
|
-
"/media"
|
46
|
-
end
|
47
|
-
|
48
94
|
def process_media(uploader)
|
49
95
|
uploader = covert(uploader)
|
50
96
|
uploader.file.to_file
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module WeixinAuthorize
|
3
|
+
module Api
|
4
|
+
module Oauth
|
5
|
+
|
6
|
+
# 应用授权作用域: scope
|
7
|
+
# snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
|
8
|
+
# snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
|
9
|
+
# default is snsapi_base
|
10
|
+
# state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值
|
11
|
+
|
12
|
+
# 如果用户点击同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE
|
13
|
+
def authorize_url(redirect_uri, scope="snsapi_base", state="weixin")
|
14
|
+
require "erb"
|
15
|
+
redirect_uri = ERB::Util.url_encode(redirect_uri)
|
16
|
+
WeixinAuthorize.open_endpoint("/connect/oauth2/authorize?appid=#{app_id}&redirect_uri=#{redirect_uri}&response_type=code&scope=#{scope}&state=#{state}#wechat_redirect")
|
17
|
+
end
|
18
|
+
|
19
|
+
# 首先请注意,这里通过code换取的网页授权access_token,与基础支持中的access_token不同。公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。
|
20
|
+
|
21
|
+
# 微信通过请求 #authorize_url 方法后,会返回一个code到redirect_uri中
|
22
|
+
def get_oauth_access_token(code)
|
23
|
+
WeixinAuthorize.http_get_without_token("/sns/oauth2/access_token?appid=#{app_id}&secret=#{app_secret}&code=#{code}&grant_type=authorization_code", {}, "api")
|
24
|
+
end
|
25
|
+
|
26
|
+
# refresh_token: 填写通过access_token获取到的refresh_token参数
|
27
|
+
def refresh_oauth2_token(refresh_token)
|
28
|
+
WeixinAuthorize.http_get_without_token("/sns/oauth2/refresh_token?appid=#{app_id}&grant_type=refresh_token&refresh_token=#{refresh_token}", {}, "api")
|
29
|
+
end
|
30
|
+
|
31
|
+
# 如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。
|
32
|
+
def get_oauth_userinfo(openid, oauth_token, lang="zh_CN")
|
33
|
+
WeixinAuthorize.http_get_without_token("/sns/userinfo?access_token=#{oauth_token}&openid=#{openid}&lang=#{lang}", {}, "api")
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module WeixinAuthorize
|
3
|
+
module Api
|
4
|
+
module Template
|
5
|
+
|
6
|
+
# 设置所属行业
|
7
|
+
# 需要选择公众账号服务所处的2个行业,每月可更改1次所选行业;
|
8
|
+
# 初始化行业时,传入两个,每月更改时,传入一个即可。
|
9
|
+
def set_template_industry(industry_id1, industry_id2="")
|
10
|
+
industries = {industry_id1: industry_id1}
|
11
|
+
if industry_id2 != ""
|
12
|
+
industries.merge!({industry_id2: industry_id2})
|
13
|
+
end
|
14
|
+
http_post("/template/api_set_industry", industries)
|
15
|
+
end
|
16
|
+
|
17
|
+
# 获得模板ID
|
18
|
+
# code: 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
|
19
|
+
def add_template(code)
|
20
|
+
http_post("/template/api_add_template", template_id_short: code)
|
21
|
+
end
|
22
|
+
|
23
|
+
# 发送模板消息
|
24
|
+
def send_template_msg(touser, template_id, url, topcolor, data)
|
25
|
+
msg = {
|
26
|
+
touser: touser, template_id: template_id,
|
27
|
+
url: url, topcolor: topcolor, data: data
|
28
|
+
}
|
29
|
+
http_post("/message/template/send", msg)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -18,6 +18,24 @@ module WeixinAuthorize
|
|
18
18
|
http_get(followers_url, {next_openid: next_openid})
|
19
19
|
end
|
20
20
|
|
21
|
+
# 设置备注名
|
22
|
+
# http请求方式: POST(请使用https协议)
|
23
|
+
# https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token=ACCESS_TOKEN
|
24
|
+
# POST数据格式:JSON
|
25
|
+
# POST数据例子:
|
26
|
+
# {
|
27
|
+
# "openid":"oDF3iY9ffA-hqb2vVvbr7qxf6A0Q",
|
28
|
+
# "remark":"pangzi"
|
29
|
+
# }
|
30
|
+
def update_remark(openid, remark)
|
31
|
+
update_url = "/user/info/updateremark"
|
32
|
+
payload = {
|
33
|
+
openid: openid,
|
34
|
+
remark: remark
|
35
|
+
}
|
36
|
+
http_post(update_url, payload)
|
37
|
+
end
|
38
|
+
|
21
39
|
private
|
22
40
|
|
23
41
|
def user_base_url
|
@@ -10,27 +10,66 @@ module WeixinAuthorize
|
|
10
10
|
include Api::Groups
|
11
11
|
include Api::Qrcode
|
12
12
|
include Api::Media
|
13
|
+
include Api::Mass
|
14
|
+
include Api::Oauth
|
15
|
+
include Api::Template
|
13
16
|
|
14
17
|
attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in
|
15
18
|
attr_accessor :access_token, :redis_key
|
16
|
-
attr_accessor :
|
19
|
+
attr_accessor :jsticket, :jsticket_expired_at, :jsticket_redis_key
|
17
20
|
|
18
21
|
def initialize(app_id, app_secret, redis_key=nil)
|
19
22
|
@app_id = app_id
|
20
23
|
@app_secret = app_secret
|
21
|
-
@expired_at = Time.now.to_i
|
22
|
-
@redis_key = security_redis_key(
|
23
|
-
@
|
24
|
+
@jsticket_expired_at = @expired_at = Time.now.to_i
|
25
|
+
@redis_key = security_redis_key(redis_key || "weixin_#{app_id}")
|
26
|
+
@jsticket_redis_key = security_redis_key("js_sdk_#{app_id}")
|
24
27
|
end
|
25
28
|
|
26
29
|
# return token
|
27
30
|
def get_access_token
|
28
|
-
|
31
|
+
token_store.access_token
|
29
32
|
end
|
30
33
|
|
31
34
|
# 检查appid和app_secret是否有效。
|
32
35
|
def is_valid?
|
33
|
-
|
36
|
+
token_store.valid?
|
37
|
+
end
|
38
|
+
|
39
|
+
def token_store
|
40
|
+
Token::Store.init_with(self)
|
41
|
+
end
|
42
|
+
|
43
|
+
def jsticket_store
|
44
|
+
JsTicket::Store.init_with(self)
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_jsticket
|
48
|
+
jsticket_store.jsticket
|
49
|
+
end
|
50
|
+
|
51
|
+
# 获取js sdk 签名包
|
52
|
+
def get_jssign_package(url)
|
53
|
+
timestamp = Time.now.to_i
|
54
|
+
noncestr = SecureRandom.hex(16)
|
55
|
+
str = "jsapi_ticket=#{get_jsticket}&noncestr=#{noncestr}×tamp=#{timestamp}&url=#{url}";
|
56
|
+
signature = Digest::SHA1.hexdigest(str)
|
57
|
+
{
|
58
|
+
"appId" => app_id, "nonceStr" => noncestr,
|
59
|
+
"timestamp" => timestamp, "url" => url,
|
60
|
+
"signature" => signature, "rawString" => str
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
# 暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。
|
65
|
+
def http_get(url, headers={}, endpoint="plain")
|
66
|
+
headers = headers.merge(access_token_param)
|
67
|
+
WeixinAuthorize.http_get_without_token(url, headers, endpoint)
|
68
|
+
end
|
69
|
+
|
70
|
+
def http_post(url, payload={}, headers={}, endpoint="plain")
|
71
|
+
headers = access_token_param.merge(headers)
|
72
|
+
WeixinAuthorize.http_post_without_token(url, payload, headers, endpoint)
|
34
73
|
end
|
35
74
|
|
36
75
|
private
|
@@ -39,16 +78,6 @@ module WeixinAuthorize
|
|
39
78
|
{access_token: get_access_token}
|
40
79
|
end
|
41
80
|
|
42
|
-
def http_get(url, headers={}, endpoint="plain")
|
43
|
-
headers = headers.merge(access_token_param)
|
44
|
-
WeixinAuthorize.http_get_without_token(url, headers, endpoint)
|
45
|
-
end
|
46
|
-
|
47
|
-
def http_post(url, payload={}, headers={}, endpoint="plain")
|
48
|
-
headers = access_token_param.merge(headers)
|
49
|
-
WeixinAuthorize.http_post_without_token(url, payload, headers, endpoint)
|
50
|
-
end
|
51
|
-
|
52
81
|
def security_redis_key(key)
|
53
82
|
Digest::MD5.hexdigest(key.to_s).upcase
|
54
83
|
end
|