easy_wechat 0.1.1 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -2
- data/README.md +15 -0
- data/easy_wechat.gemspec +1 -0
- data/lib/easy_wechat/client.rb +40 -10
- data/lib/easy_wechat/version.rb +1 -1
- data/lib/generators/easy_wechat/install_generator.rb +20 -0
- data/lib/generators/templates/initializer.rb +4 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67b8f2d0927212ebaf1237100f09192730b3e4f0bb58a071c9055067cbbf2ff6
|
4
|
+
data.tar.gz: ab9549350f751be8be52e20f4620be974a4ffd949e17f8193d7669987b6bb226
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 84146eeafed0226cc224dacb9fb53e5a219d89c16e16ad92e0ad356a368310a3141babdd767a740fff19f211622cb4c4310206efb61d2e428d87f9840a8f4c81
|
7
|
+
data.tar.gz: fe36caf79273e4d934bd2c9319038aba28226c98ad434528c7af884281d698e61313ecec193231d25d657de1e5909c54e916e4296bfe354b9d327478d7275553
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
easy_wechat (0.1.
|
4
|
+
easy_wechat (0.1.7)
|
5
5
|
activesupport
|
6
|
+
http-form_data
|
6
7
|
httpx
|
7
8
|
|
8
9
|
GEM
|
@@ -18,7 +19,8 @@ GEM
|
|
18
19
|
concurrent-ruby (1.1.9)
|
19
20
|
diff-lcs (1.4.4)
|
20
21
|
http-2-next (0.4.3)
|
21
|
-
|
22
|
+
http-form_data (2.3.0)
|
23
|
+
httpx (0.15.3)
|
22
24
|
http-2-next (>= 0.4.1)
|
23
25
|
timers
|
24
26
|
i18n (1.8.10)
|
data/README.md
CHANGED
@@ -23,12 +23,27 @@ Or install it yourself as:
|
|
23
23
|
## Usage
|
24
24
|
|
25
25
|
```
|
26
|
+
rails g easy_wechat:install
|
27
|
+
|
26
28
|
EasyWechat.configure do |config|
|
27
29
|
config.appid = ENV.fetch("APPID") || ""
|
28
30
|
config.app_secret = ENV.fetch("APP_SECRET") || ""
|
29
31
|
end
|
32
|
+
|
33
|
+
client = EasyWechat::Client.new
|
34
|
+
client.token
|
35
|
+
client.batchget_material
|
36
|
+
client.uploadimg(media)
|
37
|
+
client.add_news(articles)
|
38
|
+
client.get_user_summary(begin_date, end_date)
|
39
|
+
client.get_user_cumulate(begin_date, end_date)
|
30
40
|
```
|
31
41
|
|
42
|
+
## FAQ
|
43
|
+
1. 微信api,请求方式有问题,也会报错,所以要注意请求方式
|
44
|
+
2. 先搞清楚你的appid对应的公众号是什么,并且搞清楚你是什么类型的,查看接口权限是否获得,否则有可能会有类似48001的权限未开通错误
|
45
|
+
3. 调用api之前,请确保ip已经加入到服务器白名单
|
46
|
+
|
32
47
|
## Development
|
33
48
|
|
34
49
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/easy_wechat.gemspec
CHANGED
data/lib/easy_wechat/client.rb
CHANGED
@@ -9,6 +9,9 @@ module EasyWechat
|
|
9
9
|
BATCHGET_MATERIAL_URL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"
|
10
10
|
UPLOADIMG_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadimg"
|
11
11
|
ADD_NEWS_URL = "https://api.weixin.qq.com/cgi-bin/material/add_news"
|
12
|
+
GET_USER_SUMMARY_URL = "https://api.weixin.qq.com/datacube/getusersummary"
|
13
|
+
GET_USER_CUMULATE_URL = "https://api.weixin.qq.com/datacube/getusercumulate"
|
14
|
+
GET_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket"
|
12
15
|
|
13
16
|
def initialize
|
14
17
|
end
|
@@ -17,6 +20,26 @@ module EasyWechat
|
|
17
20
|
@access_token ||= token["access_token"]
|
18
21
|
end
|
19
22
|
|
23
|
+
def get_user_summary(begin_date, end_date)
|
24
|
+
payload = { begin_date: begin_date, end_date: end_date }
|
25
|
+
|
26
|
+
resp = HTTPX.post(GET_USER_SUMMARY_URL, params: { access_token: access_token }, json: payload)
|
27
|
+
|
28
|
+
r = ::JSON.parse(resp.body, quirks_mode: true)
|
29
|
+
yield r if block_given?
|
30
|
+
r
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_user_cumulate(begin_date, end_date)
|
34
|
+
payload = { begin_date: begin_date, end_date: end_date }
|
35
|
+
|
36
|
+
resp = HTTPX.post(GET_USER_CUMULATE_URL, params: { access_token: access_token }, json: payload)
|
37
|
+
|
38
|
+
r = ::JSON.parse(resp.body, quirks_mode: true)
|
39
|
+
yield r if block_given?
|
40
|
+
r
|
41
|
+
end
|
42
|
+
|
20
43
|
# 获取access_token
|
21
44
|
# https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
|
22
45
|
def token
|
@@ -31,13 +54,20 @@ module EasyWechat
|
|
31
54
|
r
|
32
55
|
end
|
33
56
|
|
34
|
-
#
|
57
|
+
# 新增永久图文素材
|
35
58
|
# https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
|
36
|
-
def
|
37
|
-
|
38
|
-
file = if media.is_a?(String) ? HTTP::FormData::File.new(media) : meida
|
59
|
+
def add_news(articles)
|
60
|
+
payload = { articles: articles }
|
39
61
|
|
40
|
-
resp = HTTPX.
|
62
|
+
resp = HTTPX.post(ADD_NEWS_URL, params: { access_token: access_token }, json: payload)
|
63
|
+
|
64
|
+
r = ::JSON.parse(resp.body, quirks_mode: true)
|
65
|
+
yield r if block_given?
|
66
|
+
r
|
67
|
+
end
|
68
|
+
|
69
|
+
def get_ticket(type)
|
70
|
+
resp = HTTPX.get(GET_TICKET_URL, params: { access_token: access_token, type: type })
|
41
71
|
|
42
72
|
r = ::JSON.parse(resp.body, quirks_mode: true)
|
43
73
|
yield r if block_given?
|
@@ -58,12 +88,12 @@ module EasyWechat
|
|
58
88
|
r
|
59
89
|
end
|
60
90
|
|
61
|
-
#
|
91
|
+
# 上传图文消息内的图片获取URL
|
62
92
|
# https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
|
63
|
-
def
|
64
|
-
|
65
|
-
|
66
|
-
resp = HTTPX.post(
|
93
|
+
def uploadimg(media)
|
94
|
+
# https://honeyryderchuck.gitlab.io/httpx/wiki/Multipart-Uploads.html#notes
|
95
|
+
file = media.is_a?(String) ? HTTP::FormData::File.new(media) : media.path
|
96
|
+
resp = HTTPX.plugin(:multipart).post(UPLOADIMG_URL, params: { access_token: access_token }, form: { media: file })
|
67
97
|
|
68
98
|
r = ::JSON.parse(resp.body, quirks_mode: true)
|
69
99
|
yield r if block_given?
|
data/lib/easy_wechat/version.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
module EasyWechat
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
desc "EasyWechat installation: initializer"
|
5
|
+
source_root File.expand_path("../../templates", __FILE__)
|
6
|
+
|
7
|
+
def install
|
8
|
+
puts "==> generate initializer file ..."
|
9
|
+
copy_initializer
|
10
|
+
puts "Done!"
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def copy_initializer
|
16
|
+
template "initializer.rb", "config/initializers/easy_wechat.rb"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_wechat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- 42up
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httpx
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: http-form_data
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,6 +130,8 @@ files:
|
|
116
130
|
- lib/easy_wechat/client.rb
|
117
131
|
- lib/easy_wechat/configuration.rb
|
118
132
|
- lib/easy_wechat/version.rb
|
133
|
+
- lib/generators/easy_wechat/install_generator.rb
|
134
|
+
- lib/generators/templates/initializer.rb
|
119
135
|
homepage: https://github.com/42up/easy_wechat
|
120
136
|
licenses:
|
121
137
|
- MIT
|