wechat_public_api 0.1.4 → 0.1.5

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.
@@ -5,105 +5,100 @@
5
5
  # Wechat number: zmx119966
6
6
  ####################################################
7
7
 
8
- require "wechat_public_api/access_token"
9
-
10
- module WechatPublicApi
8
+ class WechatPublicApi
11
9
  module Tp
12
- class << self
13
-
14
- ###
15
- # @param <JSON> message
16
- # @param <string> url_params -- 消息路径
17
- # 发送模板消息接口
18
- def post_template_message(message, url_params)
19
- # get access_token
20
- access_token = AccessToken.get()
10
+ ###
11
+ # @param <JSON> message
12
+ # @param <string> url_params -- 消息路径
13
+ # 发送模板消息接口
14
+ def post_template_message(message, url_params)
15
+ # get access_token
16
+ access_token = get_access_token()
21
17
 
22
- @access_token = Wxutils.get_access_token wx_account
23
- uri = URI.parse("https://api.weixin.qq.com/cgi-bin/message/template/#{url_params}?access_token=#{@access_token}")
24
- http = Net::HTTP.new(uri.host, uri.port)
25
- http.use_ssl = true
26
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
27
- request = Net::HTTP::Post.new("/cgi-bin/message/template/#{url_params}?access_token=#{@access_token}")
28
- request.add_field('Content-Type', 'application/json')
29
- # 部分字符转换为json后 成为unicode编码
30
- request.body = message.to_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")}
31
- response = http.request(request)
32
- JSON.parse(response.body)
33
- end
18
+ @access_token = Wxutils.get_access_token wx_account
19
+ uri = URI.parse("https://api.weixin.qq.com/cgi-bin/message/template/#{url_params}?access_token=#{@access_token}")
20
+ http = Net::HTTP.new(uri.host, uri.port)
21
+ http.use_ssl = true
22
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
23
+ request = Net::HTTP::Post.new("/cgi-bin/message/template/#{url_params}?access_token=#{@access_token}")
24
+ request.add_field('Content-Type', 'application/json')
25
+ # 部分字符转换为json后 成为unicode编码
26
+ request.body = message.to_json.gsub(/\\u([0-9a-z]{4})/) {|s| [$1.to_i(16)].pack("U")}
27
+ response = http.request(request)
28
+ JSON.parse(response.body)
29
+ end
34
30
 
35
- ###
36
- # @param <string> openid -- 用户的openid
37
- # @param <string> templet_id -- 微信公众号后台提供的模板ID
38
- # @param <string> url -- 模板跳转链接
39
- # @param <string> appid -- 小程序的appid
40
- # @param <string> pagepath -- 小程序页面路径 (eq: /index/index)
41
- # @param <JSON> data -- 模板数据
42
- # => data example
43
- # data = {
44
- # "first": {
45
- # "value":"恭喜你购买成功!",
46
- # "color":"#173177"
47
- # },
48
- # "keyword1":{
49
- # "value":"巧克力",
50
- # "color":"#173177"
51
- # },
52
- # "keyword2": {
53
- # "value":"39.8元",
54
- # "color":"#173177"
55
- # },
56
- # "keyword3": {
57
- # "value":"2014年9月22日",
58
- # "color":"#173177"
59
- # },
60
- # "remark":{
61
- # "value":"欢迎再次购买!",
62
- # "color":"#173177"
63
- # }
64
- # }
65
- # 小程序模板消息
66
- def miniprogram_message(openid, templet_id, url, appid, pagepath, data)
67
- message = {
68
- 'touser': openid,
69
- 'template_id': templet_id,
70
- 'url': url,
71
- "miniprogram":{
72
- "appid": appid,
73
- "pagepath": pagepath
74
- },
75
- 'data': data
76
- }
31
+ ###
32
+ # @param <string> openid -- 用户的openid
33
+ # @param <string> templet_id -- 微信公众号后台提供的模板ID
34
+ # @param <string> url -- 模板跳转链接
35
+ # @param <string> appid -- 小程序的appid
36
+ # @param <string> pagepath -- 小程序页面路径 (eq: /index/index)
37
+ # @param <JSON> data -- 模板数据
38
+ # => data example
39
+ # data = {
40
+ # "first": {
41
+ # "value":"恭喜你购买成功!",
42
+ # "color":"#173177"
43
+ # },
44
+ # "keyword1":{
45
+ # "value":"巧克力",
46
+ # "color":"#173177"
47
+ # },
48
+ # "keyword2": {
49
+ # "value":"39.8元",
50
+ # "color":"#173177"
51
+ # },
52
+ # "keyword3": {
53
+ # "value":"2014年9月22日",
54
+ # "color":"#173177"
55
+ # },
56
+ # "remark":{
57
+ # "value":"欢迎再次购买!",
58
+ # "color":"#173177"
59
+ # }
60
+ # }
61
+ # 小程序模板消息
62
+ def tp_miniprogram_message(openid, templet_id, url, appid, pagepath, data)
63
+ message = {
64
+ 'touser': openid,
65
+ 'template_id': templet_id,
66
+ 'url': url,
67
+ "miniprogram":{
68
+ "appid": appid,
69
+ "pagepath": pagepath
70
+ },
71
+ 'data': data
72
+ }
77
73
 
78
- self.post_template_message message, 'send'
79
- end
74
+ post_template_message message, 'send'
75
+ end
80
76
 
81
- # 普通的模板消息,不跳转小程序
82
- def general_message(openid, templet_id, url, data)
83
- message = {
84
- 'touser': openid,
85
- 'template_id': templet_id,
86
- 'url': url,
87
- 'data': data
88
- }
89
- self.post_template_message message, 'send'
90
- end
77
+ # 普通的模板消息,不跳转小程序
78
+ def tp_general_message(openid, templet_id, url, data)
79
+ message = {
80
+ 'touser': openid,
81
+ 'template_id': templet_id,
82
+ 'url': url,
83
+ 'data': data
84
+ }
85
+ post_template_message message, 'send'
86
+ end
91
87
 
92
- # 删除模板
93
- def delete(templet_id)
94
- message = {
95
- 'template_id': template_id
96
- }
97
- self.post_template_message message, 'del_private_template'
98
- end
88
+ # 删除模板
89
+ def tp_delete_template(templet_id)
90
+ message = {
91
+ 'template_id': template_id
92
+ }
93
+ post_template_message message, 'del_private_template'
94
+ end
99
95
 
100
- # 获得模板列表
101
- def get_all()
102
- access_token = AccessToken.get()
103
- response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=#{access_token}").body
104
- response_body = (JSON.parse response)
105
- response_body
106
- end
96
+ # 获得模板列表
97
+ def tp_get_all()
98
+ access_token = get_access_token()
99
+ response = HTTParty.get("https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=#{access_token}").body
100
+ response_body = (JSON.parse response)
101
+ response_body
107
102
  end
108
103
  end
109
104
  end
@@ -5,42 +5,37 @@
5
5
  # Wechat number: zmx119966
6
6
  ####################################################
7
7
 
8
- require "wechat_public_api/access_token"
9
-
10
- module WechatPublicApi
8
+ class WechatPublicApi
11
9
  module User
12
- class << self
13
-
14
- # 获取用户信息
15
- # @return <JSON>
16
- # {
17
- # "subscribe": 1,
18
- # "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
19
- # "nickname": "Band",
20
- # "sex": 1,
21
- # "language": "zh_CN",
22
- # "city": "广州",
23
- # "province": "广东",
24
- # "country": "中国",
25
- # "headimgurl":"http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
26
- # "subscribe_time": 1382694957,
27
- # "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
28
- # "remark": "",
29
- # "groupid": 0,
30
- # "tagid_list":[128,2],
31
- # "subscribe_scene": "ADD_SCENE_QR_CODE",
32
- # "qr_scene": 98765,
33
- # "qr_scene_str": ""
34
- # }
35
- #
36
- # if failed
37
- # {"errcode":40013,"errmsg":"invalid appid"}
38
- #
39
- def get_userinfo(openid)
40
- # request access_token
41
- access_token = AccessToken.get()
42
- JSON.parse HTTParty.get("https://api.weixin.qq.com/cgi-bin/user/info?access_token=#{access_token}&openid=#{openid}&lang=zh_CN").body
43
- end
10
+ # 获取用户信息
11
+ # @return <JSON>
12
+ # {
13
+ # "subscribe": 1,
14
+ # "openid": "o6_bmjrPTlm6_2sgVt7hMZOPfL2M",
15
+ # "nickname": "Band",
16
+ # "sex": 1,
17
+ # "language": "zh_CN",
18
+ # "city": "广州",
19
+ # "province": "广东",
20
+ # "country": "中国",
21
+ # "headimgurl":"http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/0",
22
+ # "subscribe_time": 1382694957,
23
+ # "unionid": " o6_bmasdasdsad6_2sgVt7hMZOPfL"
24
+ # "remark": "",
25
+ # "groupid": 0,
26
+ # "tagid_list":[128,2],
27
+ # "subscribe_scene": "ADD_SCENE_QR_CODE",
28
+ # "qr_scene": 98765,
29
+ # "qr_scene_str": ""
30
+ # }
31
+ #
32
+ # if failed
33
+ # {"errcode":40013,"errmsg":"invalid appid"}
34
+ #
35
+ def get_userinfo(openid)
36
+ # request access_token
37
+ access_token = get_access_token()
38
+ JSON.parse HTTParty.get("https://api.weixin.qq.com/cgi-bin/user/info?access_token=#{access_token}&openid=#{openid}&lang=zh_CN").body
44
39
  end
45
40
  end
46
41
  end
@@ -5,11 +5,8 @@
5
5
  # Wechat number: zmx119966
6
6
  ####################################################
7
7
 
8
- require "wechat_public_api/access_token"
9
- module WechatPublicApi
8
+ class WechatPublicApi
10
9
  module Utils
11
- class << self
12
- # Todo
13
- end
10
+ # todo
14
11
  end
15
12
  end
@@ -1,3 +1,3 @@
1
- module WechatPublicApi
2
- VERSION = "0.1.4"
1
+ class WechatPublicApi
2
+ VERSION = "0.1.5"
3
3
  end
@@ -9,10 +9,30 @@ require "wechat_public_api/utils"
9
9
  require "wechat_public_api/user"
10
10
  require "wechat_public_api/material"
11
11
 
12
- module WechatPublicApi
13
- class << self
14
- # 默认不缓存 access_token, access_token_cache = True 缓存
15
- # @param <Boolearn> access_token_cache
16
- attr_accessor :app_id, :app_secret, :access_token_cache
12
+ class WechatPublicApi
13
+ include AccessToken
14
+ include Account
15
+ include Aes
16
+ include Kf
17
+ include Material
18
+ include Menu
19
+ include Tp
20
+ include User
21
+ include Utils
22
+
23
+ # 默认不缓存 access_token, access_token_cache = True 缓存
24
+ # @param <hash> aoptions
25
+ # => example
26
+ # wechat_api = WechatPublicApi.new appid: 'xx', app_secret: 'xx', access_token_cache: true
27
+ # wechat_api.app_id -- get appid
28
+ #
29
+ # api.get_access_token
30
+ #
31
+
32
+ attr_accessor :app_id, :app_secret, :access_token_cache
33
+ def initialize(options={})
34
+ @app_id = options[:app_id]
35
+ @app_secret = options[:app_secret]
36
+ @access_token_cache = options[:access_token_cache]
17
37
  end
18
38
  end
Binary file
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wechat_public_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - '张明鑫 wechat number: zmx119966'
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-18 00:00:00.000000000 Z
11
+ date: 2018-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -137,6 +137,8 @@ files:
137
137
  - lib/wechat_public_api/version.rb
138
138
  - wechat_public_api-0.1.1.gem
139
139
  - wechat_public_api-0.1.2.gem
140
+ - wechat_public_api-0.1.3.gem
141
+ - wechat_public_api-0.1.4.gem
140
142
  - wechat_public_api.gemspec
141
143
  homepage: https://github.com/Diyilou/wechat_public_api.git
142
144
  licenses: