easy_wechat 0.1.0 → 0.1.6
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 +4 -4
- data/Gemfile.lock +4 -2
- data/README.md +21 -1
- data/easy_wechat.gemspec +3 -2
- data/lib/easy_wechat.rb +27 -2
- data/lib/easy_wechat/client.rb +107 -0
- data/lib/easy_wechat/configuration.rb +10 -0
- 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 +22 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccc93ec4d83d3f2b3b45a514b9297b76ee395fbe8aebbceef0387cd4a422d491
|
4
|
+
data.tar.gz: 9a89ebd838cb835625acb977c1145ef2edfb62250de62adbfa22527e15a5bd97
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24929f3e1b1ed83e6f57fc167a4c2aa76404b968e6c73f64cf42b99124b35d185ef67d59ba5cee23fcc7a0fe40bc2e1790bfe354c16fb8e06eaee433aa4d7397
|
7
|
+
data.tar.gz: aa1b2c8ae2679b0b3d080c8389e71d562275e612f0bebc617b59f323ad051e3e823745db403ab9d15a4175fc6e21a99adfb62f280b4e78864f9cec50da73fea6
|
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.6)
|
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
@@ -22,7 +22,27 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
```
|
26
|
+
rails g easy_wechat:install
|
27
|
+
|
28
|
+
EasyWechat.configure do |config|
|
29
|
+
config.appid = ENV.fetch("APPID") || ""
|
30
|
+
config.app_secret = ENV.fetch("APP_SECRET") || ""
|
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)
|
40
|
+
```
|
41
|
+
|
42
|
+
## FAQ
|
43
|
+
1. 微信api,请求方式有问题,也会报错,所以要注意请求方式
|
44
|
+
2. 先搞清楚你的appid对应的公众号是什么,并且搞清楚你是什么类型的,查看接口权限是否获得,否则有可能会有类似48001的权限未开通错误
|
45
|
+
3. 调用api之前,请确保ip已经加入到服务器白名单
|
26
46
|
|
27
47
|
## Development
|
28
48
|
|
data/easy_wechat.gemspec
CHANGED
@@ -6,8 +6,8 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.authors = ["42up"]
|
7
7
|
spec.email = ["foobar@v2up.com"]
|
8
8
|
|
9
|
-
spec.summary = "easy wechat sdk"
|
10
|
-
spec.description = "easy wechat sdk"
|
9
|
+
spec.summary = "An unofficial easy wechat sdk"
|
10
|
+
spec.description = "An unofficial easy wechat sdk"
|
11
11
|
spec.homepage = "https://github.com/42up/easy_wechat"
|
12
12
|
spec.license = "MIT"
|
13
13
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
|
|
29
29
|
|
30
30
|
spec.add_dependency "httpx"
|
31
31
|
spec.add_dependency "activesupport"
|
32
|
+
spec.add_dependency "http-form_data"
|
32
33
|
|
33
34
|
spec.add_development_dependency "bundler"
|
34
35
|
spec.add_development_dependency "rake"
|
data/lib/easy_wechat.rb
CHANGED
@@ -1,7 +1,32 @@
|
|
1
1
|
require "easy_wechat/version"
|
2
|
+
require "easy_wechat/client"
|
3
|
+
require "easy_wechat/configuration"
|
4
|
+
|
5
|
+
require "httpx"
|
2
6
|
|
3
7
|
module EasyWechat
|
4
8
|
class Error < StandardError; end
|
5
|
-
|
6
|
-
|
9
|
+
def self.configuration
|
10
|
+
@configuration ||= Configuration.new
|
11
|
+
end
|
12
|
+
|
13
|
+
class << self
|
14
|
+
attr_writer :configuration
|
15
|
+
|
16
|
+
def configuration
|
17
|
+
@configuration ||= Configuration.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def configure
|
21
|
+
yield(configuration)
|
22
|
+
end
|
23
|
+
|
24
|
+
def app_id
|
25
|
+
configuration.app_id
|
26
|
+
end
|
27
|
+
|
28
|
+
def app_secret
|
29
|
+
configuration.app_secret
|
30
|
+
end
|
31
|
+
end
|
7
32
|
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require "httpx"
|
2
|
+
require "http/form_data"
|
3
|
+
require "json"
|
4
|
+
require "active_support"
|
5
|
+
|
6
|
+
module EasyWechat
|
7
|
+
class Client
|
8
|
+
TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token"
|
9
|
+
BATCHGET_MATERIAL_URL = "https://api.weixin.qq.com/cgi-bin/material/batchget_material"
|
10
|
+
UPLOADIMG_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadimg"
|
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"
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
end
|
18
|
+
|
19
|
+
def access_token
|
20
|
+
@access_token ||= token["access_token"]
|
21
|
+
end
|
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
|
+
|
43
|
+
# 获取access_token
|
44
|
+
# https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
|
45
|
+
def token
|
46
|
+
resp = HTTPX.get(TOKEN_URL, params: {
|
47
|
+
appid: EasyWechat.app_id,
|
48
|
+
secret: EasyWechat.app_secret,
|
49
|
+
grant_type: "client_credential",
|
50
|
+
})
|
51
|
+
|
52
|
+
r = ::JSON.parse(resp.body, quirks_mode: true)
|
53
|
+
yield r if block_given?
|
54
|
+
r
|
55
|
+
end
|
56
|
+
|
57
|
+
# 新增永久图文素材
|
58
|
+
# https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
|
59
|
+
def add_news(articles)
|
60
|
+
payload = { articles: articles }
|
61
|
+
|
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 })
|
71
|
+
|
72
|
+
r = ::JSON.parse(resp.body, quirks_mode: true)
|
73
|
+
yield r if block_given?
|
74
|
+
r
|
75
|
+
end
|
76
|
+
|
77
|
+
# 获取永久素材的列表
|
78
|
+
# https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_materials_list.html
|
79
|
+
def batchget_material(type = "image", offset = 0, count = 20)
|
80
|
+
# TODO: 检查参数完整性
|
81
|
+
# TODO: 检查参数合法性
|
82
|
+
payload = { type: type, offset: offset, count: count }
|
83
|
+
|
84
|
+
resp = HTTPX.post(BATCHGET_MATERIAL_URL, params: { access_token: access_token }, json: payload)
|
85
|
+
|
86
|
+
r = ::JSON.parse(resp.body, quirks_mode: true)
|
87
|
+
yield r if block_given?
|
88
|
+
r
|
89
|
+
end
|
90
|
+
|
91
|
+
# 上传图文消息内的图片获取URL
|
92
|
+
# https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Adding_Permanent_Assets.html
|
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) : meida
|
96
|
+
resp = HTTPX.plugin(:multipart).post(UPLOADIMG_URL, params: { access_token: access_token }, form: { media: file })
|
97
|
+
|
98
|
+
r = ::JSON.parse(resp.body, quirks_mode: true)
|
99
|
+
yield r if block_given?
|
100
|
+
r
|
101
|
+
end
|
102
|
+
|
103
|
+
def menu_create
|
104
|
+
raise NotImplementedError
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
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.6
|
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-20 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
|
@@ -94,7 +108,7 @@ dependencies:
|
|
94
108
|
- - ">="
|
95
109
|
- !ruby/object:Gem::Version
|
96
110
|
version: '0'
|
97
|
-
description: easy wechat sdk
|
111
|
+
description: An unofficial easy wechat sdk
|
98
112
|
email:
|
99
113
|
- foobar@v2up.com
|
100
114
|
executables: []
|
@@ -113,7 +127,11 @@ files:
|
|
113
127
|
- bin/setup
|
114
128
|
- easy_wechat.gemspec
|
115
129
|
- lib/easy_wechat.rb
|
130
|
+
- lib/easy_wechat/client.rb
|
131
|
+
- lib/easy_wechat/configuration.rb
|
116
132
|
- lib/easy_wechat/version.rb
|
133
|
+
- lib/generators/easy_wechat/install_generator.rb
|
134
|
+
- lib/generators/templates/initializer.rb
|
117
135
|
homepage: https://github.com/42up/easy_wechat
|
118
136
|
licenses:
|
119
137
|
- MIT
|
@@ -139,5 +157,5 @@ requirements: []
|
|
139
157
|
rubygems_version: 3.1.4
|
140
158
|
signing_key:
|
141
159
|
specification_version: 4
|
142
|
-
summary: easy wechat sdk
|
160
|
+
summary: An unofficial easy wechat sdk
|
143
161
|
test_files: []
|