weixin_authorize 1.5.1 → 1.5.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +1 -1
- data/README.md +1 -144
- data/lib/weixin_authorize/api/media.rb +42 -0
- data/lib/weixin_authorize/api/qrcode.rb +40 -0
- data/lib/weixin_authorize/client.rb +3 -6
- data/lib/weixin_authorize/version.rb +1 -1
- data/lib/weixin_authorize.rb +2 -0
- data/spec/api/media_spec.rb +25 -0
- data/spec/api/medias/ruby-logo.jpg +0 -0
- data/spec/api/qrcode_spec.rb +16 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c7e75ae1a9b81d02ddaba6cf28d0ef9afe64a7c
|
4
|
+
data.tar.gz: 5aeca7b27d5a5a81c780c406ab520bff27835871
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18949cdd4dc1d47ca7123da41290422c6bc65a8ec5aa50556f890c657ddc525fa5970e222b50acea27e358c493e0558f05e8c84e254a779f94f3f8c70d7ddd7f
|
7
|
+
data.tar.gz: dd6135a09e0e930e441dfc886303c5d83e33129e105719c51a79927a264ba9a86c619b4599cb6ca45987b7696e2d8ad60221c6aa267f5dc3999750640f34a27c
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -4,149 +4,7 @@
|
|
4
4
|
|
5
5
|
Support using [Redis](http://redis.io) to store `access_token`
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
|
-
`gem 'weixin_authorize'`
|
12
|
-
|
13
|
-
Or
|
14
|
-
|
15
|
-
`gem 'weixin_authorize', git: "https://github.com/lanrion/weixin_authorize.git"`
|
16
|
-
|
17
|
-
And then execute:
|
18
|
-
|
19
|
-
`$ bundle`
|
20
|
-
|
21
|
-
Or install it yourself as:
|
22
|
-
|
23
|
-
`$ gem install weixin_authorize`
|
24
|
-
|
25
|
-
## Usage
|
26
|
-
|
27
|
-
### Init a `client`
|
28
|
-
|
29
|
-
```ruby
|
30
|
-
|
31
|
-
# new a client
|
32
|
-
$client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"])
|
33
|
-
|
34
|
-
# valid app_id and app_secret
|
35
|
-
$client.is_valid? # return true or false
|
36
|
-
```
|
37
|
-
|
38
|
-
### Option: use [Redis](http://redis.io) to store your access_token (Recommend)
|
39
|
-
|
40
|
-
**If you don't use Redis, it will send a request to get a new access_token everytime!**
|
41
|
-
|
42
|
-
* Added `redis-namespace` to you `Gemfile`
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
# Adds a Redis::Namespace class which can be used to namespace calls to Redis. This is useful when using a single instance of Redis with multiple, different applications.
|
46
|
-
# http://github.com/resque/redis-namespace
|
47
|
-
gem "redis-namespace", "~> 1.4.1"
|
48
|
-
|
49
|
-
```
|
50
|
-
|
51
|
-
* Create file in: `config/initializers/weixin_authorize.rb`
|
52
|
-
|
53
|
-
```ruby
|
54
|
-
|
55
|
-
# don't forget change namespace
|
56
|
-
namespace = "app_name_weixin:weixin_authorize"
|
57
|
-
redis = Redis.new(:host => "127.0.0.1", :port => "6379", :db => 15)
|
58
|
-
|
59
|
-
# cleanup keys in the current namespace when restart server everytime.
|
60
|
-
exist_keys = redis.keys("#{namespace}:*")
|
61
|
-
exist_keys.each{|key|redis.del(key)}
|
62
|
-
|
63
|
-
# Give a special namespace as prefix for Redis key, when your have more than one project used weixin_authorize, this config will make them work fine.
|
64
|
-
redis = Redis::Namespace.new("#{namespace}", :redis => redis)
|
65
|
-
|
66
|
-
WeixinAuthorize.configure do |config|
|
67
|
-
config.redis = redis
|
68
|
-
end
|
69
|
-
|
70
|
-
```
|
71
|
-
|
72
|
-
* You can also specify the `key`, but it is optionly.
|
73
|
-
|
74
|
-
```ruby
|
75
|
-
|
76
|
-
$client ||= WeixinAuthorize::Client.new(ENV["APPID"], ENV["APPSECRET"], "your_store_key")
|
77
|
-
```
|
78
|
-
**Note:** `your_store_key` should be unique for every account!
|
79
|
-
|
80
|
-
### 获取用户管理信息
|
81
|
-
|
82
|
-
* [获取用户基本信息](http://mp.weixin.qq.com/wiki/index.php?title=获取用户基本信息)
|
83
|
-
|
84
|
-
`user_info = $client.user(ENV["OPENID"])`
|
85
|
-
|
86
|
-
* [获取关注者列表](http://mp.weixin.qq.com/wiki/index.php?title=获取关注者列表)
|
87
|
-
|
88
|
-
`followers = $client.followers`
|
89
|
-
|
90
|
-
### [分组管理接口](http://mp.weixin.qq.com/wiki/index.php?title=分组管理接口)
|
91
|
-
|
92
|
-
* 创建分组:
|
93
|
-
|
94
|
-
`group = $client.create_group("test")`
|
95
|
-
|
96
|
-
* 查询所有分组:
|
97
|
-
|
98
|
-
`groups = $client.groups`
|
99
|
-
* 查询用户所在分组:
|
100
|
-
|
101
|
-
`group = $client.get_group_for(ENV["OPENID"])`
|
102
|
-
|
103
|
-
* 修改分组名:
|
104
|
-
|
105
|
-
`group = $client.update_group_name(ENV["OPENID"], "new_group_name")`
|
106
|
-
|
107
|
-
* 移动用户分组:
|
108
|
-
|
109
|
-
`group = $client.update_group_for_openid(ENV["OPENID"], "to_groupid")`
|
110
|
-
|
111
|
-
### 自定义菜单
|
112
|
-
|
113
|
-
* [自定义菜单创建接口](http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单创建接口)
|
114
|
-
|
115
|
-
`response = $client.create_menu(menu) # Hash or Json`
|
116
|
-
|
117
|
-
* [自定义菜单查询接口](http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单查询接口)
|
118
|
-
|
119
|
-
`response = $client.menu`
|
120
|
-
|
121
|
-
* [自定义菜单删除接口](http://mp.weixin.qq.com/wiki/index.php?title=自定义菜单删除接口)
|
122
|
-
|
123
|
-
`response = $client.delete_menu`
|
124
|
-
|
125
|
-
### [发送客服信息](http://mp.weixin.qq.com/wiki/index.php?title=发送客服信息)
|
126
|
-
|
127
|
-
* 发送文本信息:
|
128
|
-
|
129
|
-
`$client.send_text_custom(to_user, content)`
|
130
|
-
|
131
|
-
* 发送图片信息:
|
132
|
-
|
133
|
-
`$client.send_image_custom(to_user, media_id)`
|
134
|
-
|
135
|
-
* 发送语音消息:
|
136
|
-
|
137
|
-
`$client.send_voice_custom(to_user, media_id)`
|
138
|
-
|
139
|
-
* 发送视频消息:
|
140
|
-
|
141
|
-
`$client.send_video_custom(to_user, media_id, options)`
|
142
|
-
|
143
|
-
* 发送音乐消息:
|
144
|
-
|
145
|
-
`$client.send_music_custom(to_user, media_id, musicurl, hqmusicurl, options)`
|
146
|
-
|
147
|
-
* 发送图文消息:
|
148
|
-
|
149
|
-
`$client.send_news_custom(to_user, *articles)`
|
7
|
+
[Getting-Started](https://github.com/lanrion/weixin_authorize/wiki/Getting-Started)
|
150
8
|
|
151
9
|
## How to test
|
152
10
|
|
@@ -159,7 +17,6 @@ export OPENID="your weixin openid"
|
|
159
17
|
```
|
160
18
|
Last, you have to **open a new terminal tag (Reload bash_profile)** , and run `rspec .`
|
161
19
|
|
162
|
-
|
163
20
|
## Contributing
|
164
21
|
|
165
22
|
1. Fork it
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module WeixinAuthorize
|
3
|
+
module Api
|
4
|
+
module Media
|
5
|
+
|
6
|
+
# 上传多媒体文件
|
7
|
+
# http请求方式: POST/FORM
|
8
|
+
# http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
|
9
|
+
# 支持传路径或者文件类型
|
10
|
+
def upload_media(media, type)
|
11
|
+
file = media.is_a?(File) ? media : File.new(media)
|
12
|
+
upload_media_url = "#{media_base_url}/upload"
|
13
|
+
http_upload(upload_media_url, {media: file, media_type: type})
|
14
|
+
end
|
15
|
+
|
16
|
+
# 目前仅仅把下载链接返回给第三方开发者,由第三方开发者处理下载
|
17
|
+
# php重新写入文件方式:
|
18
|
+
# http://www.cnblogs.com/txw1958/p/weixin80-upload-download-media-file.html
|
19
|
+
def download_media_url(media_id)
|
20
|
+
download_media_url = file_endpoint + "#{media_base_url}/get"
|
21
|
+
download_media_url += "?access_token=#{get_access_token}"
|
22
|
+
download_media_url += "&media_id=#{media_id}"
|
23
|
+
download_media_url
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def media_base_url
|
29
|
+
"/media"
|
30
|
+
end
|
31
|
+
|
32
|
+
def http_upload(url, options)
|
33
|
+
media_type = options.delete(:media_type)
|
34
|
+
upload_url = file_endpoint + url
|
35
|
+
upload_url += "?access_token=#{get_access_token}"
|
36
|
+
upload_url += "&type=#{media_type}"
|
37
|
+
JSON.parse(RestClient.post(upload_url, options))
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module WeixinAuthorize
|
3
|
+
module Api
|
4
|
+
module Qrcode
|
5
|
+
# http://mp.weixin.qq.com/wiki/index.php?title=生成带参数的二维码
|
6
|
+
|
7
|
+
# 临时二维码
|
8
|
+
def create_qr_scene(scene_id, expire_seconds=1800)
|
9
|
+
post_url = "#{qrcode_base_url}/create"
|
10
|
+
qrcode_infos = {action_name: "QR_SCENE", expire_seconds: expire_seconds}
|
11
|
+
qrcode_infos = qrcode_infos.merge(action_info(scene_id))
|
12
|
+
http_post(post_url, qrcode_infos)
|
13
|
+
end
|
14
|
+
|
15
|
+
# 永久二维码
|
16
|
+
def create_qr_limit_scene(scene_id)
|
17
|
+
post_url = "#{qrcode_base_url}/create"
|
18
|
+
qrcode_infos = {action_name: "QR_LIMIT_SCENE"}
|
19
|
+
qrcode_infos = qrcode_infos.merge(action_info(scene_id))
|
20
|
+
http_post(post_url, qrcode_infos)
|
21
|
+
end
|
22
|
+
|
23
|
+
# 通过ticket换取二维码, 直接访问即可显示!
|
24
|
+
def qr_code_url(ticket)
|
25
|
+
"https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=#{ticket}"
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def qrcode_base_url
|
31
|
+
"/qrcode"
|
32
|
+
end
|
33
|
+
|
34
|
+
def action_info(scene_id)
|
35
|
+
{action_info: {scene: {scene_id: scene_id}}}
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -8,15 +8,12 @@ module WeixinAuthorize
|
|
8
8
|
include Api::Menu
|
9
9
|
include Api::Custom
|
10
10
|
include Api::Groups
|
11
|
+
include Api::Qrcode
|
12
|
+
include Api::Media
|
11
13
|
|
12
14
|
attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in
|
13
15
|
attr_accessor :access_token, :redis_key
|
14
16
|
|
15
|
-
# 对于多用户微信营销平台的对接,需要把每次的expired_at, access_token保存在Redis中
|
16
|
-
# 每次使用,则可以从Redis中获取expired_at和access_token,即
|
17
|
-
# @client = WeixinAuthorize::Client.new(appid, appsecret)
|
18
|
-
# 如果使用存在多个公众账号,请使用 务必传递: redis_key
|
19
|
-
# 获取access_token,则仍然是:@client.get_access_token 来获取
|
20
17
|
def initialize(app_id, app_secret, redis_key=nil)
|
21
18
|
@app_id = app_id
|
22
19
|
@app_secret = app_secret
|
@@ -74,7 +71,7 @@ module WeixinAuthorize
|
|
74
71
|
def set_access_token_for_client(access_token_infos=nil)
|
75
72
|
token_infos = access_token_infos || http_get_access_token
|
76
73
|
self.access_token = token_infos["access_token"]
|
77
|
-
self.expired_at = Time.now.to_i + token_infos["expires_in"]
|
74
|
+
self.expired_at = Time.now.to_i + token_infos["expires_in"].to_i
|
78
75
|
end
|
79
76
|
|
80
77
|
def http_get_access_token
|
data/lib/weixin_authorize.rb
CHANGED
@@ -5,6 +5,8 @@ require "weixin_authorize/api/user"
|
|
5
5
|
require "weixin_authorize/api/menu"
|
6
6
|
require "weixin_authorize/api/custom"
|
7
7
|
require "weixin_authorize/api/groups"
|
8
|
+
require "weixin_authorize/api/qrcode"
|
9
|
+
require "weixin_authorize/api/media"
|
8
10
|
require "weixin_authorize/client"
|
9
11
|
|
10
12
|
module WeixinAuthorize
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe WeixinAuthorize::Api::Media do
|
4
|
+
|
5
|
+
let(:image_path) do
|
6
|
+
"#{File.dirname(__FILE__)}/medias/ruby-logo.jpg"
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:image_file) do
|
10
|
+
File.new(image_path)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "can upload a image" do
|
14
|
+
response = $client.upload_media(image_file, "image")
|
15
|
+
expect(response.keys).to eq(["type", "media_id", "created_at"])
|
16
|
+
end
|
17
|
+
|
18
|
+
# it "#download_media_url" do
|
19
|
+
# image = $client.upload_media(image_file, "image")
|
20
|
+
# media_id = image["media_id"]
|
21
|
+
# response = $client.download_media_url(media_id)
|
22
|
+
# response
|
23
|
+
# end
|
24
|
+
|
25
|
+
end
|
Binary file
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe WeixinAuthorize::Api::Qrcode do
|
4
|
+
|
5
|
+
it "#create_qr_scene" do
|
6
|
+
response = $client.create_qr_scene("123")
|
7
|
+
expect(response.keys).to eq(["ticket", "expire_seconds"])
|
8
|
+
expect(response["expire_seconds"]).to eq(1800)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "#create_qr_limit_scene" do
|
12
|
+
response = $client.create_qr_limit_scene("1234")
|
13
|
+
expect(response.keys).to eq(["ticket"])
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weixin_authorize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lanrion
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rest-client
|
@@ -110,7 +110,9 @@ files:
|
|
110
110
|
- lib/weixin_authorize.rb
|
111
111
|
- lib/weixin_authorize/api/custom.rb
|
112
112
|
- lib/weixin_authorize/api/groups.rb
|
113
|
+
- lib/weixin_authorize/api/media.rb
|
113
114
|
- lib/weixin_authorize/api/menu.rb
|
115
|
+
- lib/weixin_authorize/api/qrcode.rb
|
114
116
|
- lib/weixin_authorize/api/user.rb
|
115
117
|
- lib/weixin_authorize/client.rb
|
116
118
|
- lib/weixin_authorize/config.rb
|
@@ -118,7 +120,10 @@ files:
|
|
118
120
|
- spec/1_fetch_access_token_spec.rb
|
119
121
|
- spec/api/custom_spec.rb
|
120
122
|
- spec/api/groups_spec.rb
|
123
|
+
- spec/api/media_spec.rb
|
124
|
+
- spec/api/medias/ruby-logo.jpg
|
121
125
|
- spec/api/menu_spec.rb
|
126
|
+
- spec/api/qrcode_spec.rb
|
122
127
|
- spec/api/user_spec.rb
|
123
128
|
- spec/spec_helper.rb
|
124
129
|
- weixin_authorize.gemspec
|
@@ -150,6 +155,9 @@ test_files:
|
|
150
155
|
- spec/1_fetch_access_token_spec.rb
|
151
156
|
- spec/api/custom_spec.rb
|
152
157
|
- spec/api/groups_spec.rb
|
158
|
+
- spec/api/media_spec.rb
|
159
|
+
- spec/api/medias/ruby-logo.jpg
|
153
160
|
- spec/api/menu_spec.rb
|
161
|
+
- spec/api/qrcode_spec.rb
|
154
162
|
- spec/api/user_spec.rb
|
155
163
|
- spec/spec_helper.rb
|