wechat-gate 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb3332818e8a45fd3e1ac100356619d8681e2747
4
- data.tar.gz: 4c485ae946db0bcf9c3359bcfce6bc4fa03136af
3
+ metadata.gz: 5a4121d04b0a2699cc4c307228db420f29571fdd
4
+ data.tar.gz: d5c8e6266d6576cf9c8a2c42e938a97d4f506ef5
5
5
  SHA512:
6
- metadata.gz: c9e1155c4aabd8805c90d534acdcea07966c87c6cdc30bb2fc809a7fb0c54093b2a8d41c4f63c2b04f03992d169d490f6ebf87b0ef727b0f5f4c023ce5fe5c5a
7
- data.tar.gz: ff2a0a08bbcfff3f269065323d44af7c887fe6d7a12088f70ac7a2cc362fd9e286c871b86b9ddc89ec6f7f0409ce4a2e1cea2f53efafa3f5ffd0915264f552ac
6
+ metadata.gz: a6b51cda2ecd7bc7ea992c959fff6cba31a202c8840d557ee9a54746fecd7105883cdb8ae9ce301b35a68c3a68791f54b00cfe64de493b817f44aaf5938ba27b
7
+ data.tar.gz: 0cd490487a0129ceaf2077976c5130c23d2b5c206b006953cac15a425e5f0baf435ff1b50778f6e03f561fd7899ecb2a6a9c1067df9b0679f5704653e2a129be
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- wechat-gate (0.1.1)
4
+ wechat-gate (0.1.2)
5
5
  activesupport (~> 5.0.1)
6
6
  rest-client (~> 1.8)
7
7
 
data/README.md CHANGED
@@ -2,6 +2,23 @@
2
2
 
3
3
  **微信公众平台开发库**
4
4
 
5
+ 支持的接口:
6
+
7
+ - access_token(后端API使用)
8
+ - 用户授权信息获取(OAuth2)
9
+ - JS-SDK
10
+ - 回复消息封装
11
+ - 菜单接口
12
+ - 素材接口
13
+
14
+ 功能特点:
15
+
16
+ - 自动管理access_token和JS-SDK的ticket刷新和过期
17
+ - 多微信公众号支持
18
+ - 多环境支持(development, production),方便本地测试
19
+ - Controler和helper方法(微信session管理等等)
20
+ - 接口简单,方便定制
21
+
5
22
  ## Installation
6
23
 
7
24
  Add this line to your application's Gemfile:
@@ -18,7 +35,7 @@ Or install it yourself as:
18
35
 
19
36
  $ gem install wechat-gate
20
37
 
21
- ## Settings
38
+ ## 公众号
22
39
 
23
40
  在开工之前你需要在微信公众账号平台做以下配置:
24
41
 
@@ -27,24 +44,88 @@ Or install it yourself as:
27
44
  3. 在“接口权限” - “网页授权获取用户基本信息”中设置你的授权回调页面域名,这个用于OAuth2的回调域名认证
28
45
  4. 在“基本配置”中查看并配置你的AppID和AppSecret
29
46
 
30
- ## Usage
47
+ ## 配置
31
48
 
32
- 1.在Rails项目config目录下建立文件wechat.yml,并配置你的公众号信息.
49
+ Rails项目config目录下建立文件wechat.yml,并配置你的公众号信息.
33
50
 
34
51
  ```
35
- app_name:
36
- app_id: idstring
37
- app_secret: secret
38
- oauth2_redirect_uri: "http://www.example.com/api/wechat_oauth/callback"
52
+ # 区分不同的环境
53
+ eggman:
54
+ development:
55
+ host: http://wechat-test1.eggman.tv
56
+
57
+ wechat_id: xxxxxxxxxx
58
+ app_id: xxxxxxxxxx
59
+ app_secret: xxxxxxxxxx
60
+
61
+ oauth2_redirect_uri: "http://wechat-test1.eggman.tv/wechat/users/callback"
62
+
63
+ push_url: "http://wechat-test1.eggman.tv/wechat/push"
64
+ push_token: xxxxxxxxxxxxxxxxxxxx
65
+ production:
66
+ host: https://eggman.tv
67
+
68
+ wechat_id: xxxxxxxxxx
69
+ app_id: xxxxxxxxxxxxxxxxxxxx
70
+ app_secret: xxxxxxxxxxxxxxxxxxxxxxxxxx
71
+
72
+ # 如果不需要多环境支持,也可以这样
73
+ app_name:
74
+ app_id: <%= ENV['WECHAT_APP_NAME_APP_ID'] %>
75
+ app_secret: <%= ENV['WECHAT_APP_NAME_APP_SECRET'] %>
76
+ oauth2_redirect_uri: <%= ENV['WECHAT_APP_NAME_OAUTH2_REDIRECT_URI'] %>
39
77
  ```
40
78
 
41
- 2.在Controller中调用(用于微信JS-SDK)
79
+ 然后在ApplicationController中指定当前要读取的配置:
80
+
81
+ self.wechat_gate_app_name = 'eggman'
82
+
83
+ ## 后端调用
84
+
85
+ 后台API操作(比如微信用户信息获取等等操作)。
86
+
87
+ 默认情况下在controller中已经初始化了配置,方法为**wechat_gate_config**,直接使用就行。
88
+
89
+
90
+ ```ruby
91
+ wechat_gate_config.users # 获取用户列表
92
+ wechat_gate_config.user('ONE_OPEN_ID') # 获取一个用户的详细信息
93
+ wechat_gate_config.access_token # 获取当前access_token
94
+
95
+ # OAuth 2
96
+ wechat_gate_config.oauth2_entrance_url(scope: "snsapi_userinfo", state: "CURENT_STATE") # 获取当前OAuth2授权入口URL
97
+ wechat_gate_config.oauth2_access_token("TOKEN") # 根据OAuth2返回的TOKEN获取access_token
98
+ wechat_gate_config.oauth2_user("ACCESS_TOKEN", "ONE_OPEN_ID") # 获取一个用户的信息
99
+
100
+ wechat_gate_config.medias # 获取素材列表, 参数type: image | video | voice | news (图文)
101
+
102
+ wechat_gate_config.menu_get # 获取菜单
103
+ wechat_gate_config.menu_create(MENU_HASH) # 创建菜单
104
+
105
+ wechat_gate_config.generate_js_request_params(REFERER_URL) # 返回JS-SDK的验证参数,供前端JS-SDK使用
106
+ ```
107
+
108
+ access_token和JS_SDK中ticket都有过期时间和刷新次数限制,这里已经考虑了,你可以不用管,如果你想手工刷新,可以这样:
109
+
110
+ ```
111
+ config.refresh_access_token
112
+ config.refresh_jsapi_ticket
113
+ ```
114
+
115
+ 当然你也可以手工来初始化配置,甚至指定配置文件的路径:
116
+
117
+ ```
118
+ config = WechatGate::Config.new('app_name', '/path/to/what/ever/you/want.yml')
119
+ ```
120
+
121
+ **配置文件支持erb**
122
+
123
+ ## JS-SDK
42
124
 
43
125
  ```ruby
44
126
  def ticket
45
127
  url = CGI.unescape(params[:url]) # 微信中用户访问的页面
46
- config = WechatGate::Config.new('app_name')
47
- @data = config.generate_js_request_params(url) # 生成微信JS-SDK所需的jsapi_ticket,signature等等参数供前段js使用
128
+ @data = wechat_gate_config.generate_js_request_params(url) # 生成微信JS-SDK所需的jsapi_ticket,signature等等参数供前段js使用
48
129
  render content_type: "application/javascript"
49
130
  end
50
131
  ```
@@ -56,7 +137,7 @@ var wxServerConfig = <%= @data.to_json.html_safe %>;
56
137
  <%= params[:callback] %>();
57
138
  ```
58
139
 
59
- 然后在微信端页面引入一下代码:
140
+ 然后在微信端页面引入以下代码:
60
141
 
61
142
  ```js
62
143
  (function() {
@@ -67,15 +148,6 @@ var wxServerConfig = <%= @data.to_json.html_safe %>;
67
148
  })();
68
149
  ```
69
150
 
70
-
71
- **如果不是利用JS-SDK,而是后台API操作(比如微信用户信息获取等等操作),可以直接利用以下方法来获取access_token:**
72
- ```ruby
73
- config = WechatGate::Config.new('app_name')
74
- config.refresh_access_token
75
- config.refresh_jsapi_ticket
76
- ```
77
-
78
-
79
151
  ## TODO
80
152
 
81
153
  添加测试
@@ -3,7 +3,7 @@ module WechatGate
3
3
  #
4
4
  # http://mp.weixin.qq.com/wiki/17/f298879f8fb29ab98b2f2971d42552fd.html
5
5
  #
6
- # 消息大发送只能是被动的,就是微信会把用户的聊天数据推送到服务器端,然后服务器利用返回值作出相应
6
+ # 消息发送只能是被动的,就是微信会把用户的聊天数据推送到服务器端,然后服务器利用返回值作出相应
7
7
  #
8
8
 
9
9
  def message_body(type, to, body)
@@ -12,13 +12,10 @@ module WechatGate
12
12
  noncestr = []
13
13
  16.times { noncestr << word_creator.call }
14
14
 
15
- jsapi_tic = self.jsapi_ticket
16
- timestamp = Time.now.to_i
17
-
18
15
  params = {
19
- "jsapi_ticket" => jsapi_tic,
16
+ "jsapi_ticket" => self.jsapi_ticket,
20
17
  "noncestr" => noncestr.join,
21
- "timestamp" => timestamp,
18
+ "timestamp" => Time.now.to_i,
22
19
  "url" => current_page_url
23
20
  }
24
21
 
@@ -1,3 +1,3 @@
1
1
  module WechatGate
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wechat-gate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lei Lee