wx_miniprogram 0.1.4 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e158aeba888940eef54a56e502610e335c3ad3c4ba28ae063a5fd769496eed84
4
- data.tar.gz: a25e74d78c5e61d9c1c4de6070ca723953daebc75b8bfc400225130c1b42646b
3
+ metadata.gz: c356e172674e2672f56a637df09cd77b4f4e7a87c8c160c27d68247f8cbc4d56
4
+ data.tar.gz: 331c23f52f62e114ab829646fea2403513e10b33945633ba28474065b0b7e91e
5
5
  SHA512:
6
- metadata.gz: 895464d8c2de4d59c8a40fbc972b50fe0b482944f9730447d7a056def2fb0fb90c4705ca1908ce9a298780caa15bb6cec22e06280a320c6db5d67a7163dbce0b
7
- data.tar.gz: bc40e9eed51b450c44da10b548638f0f52a7522470563d6c5cdf7f17329f758a1446819eecf34737fa490a532dd630dddcea95b973e90e83cd926754ffd1564f
6
+ metadata.gz: 3012e61fdcc4fe72620edcade49e39186bc09ee4c27b84df4c43ad6c9f02ef9f995eef60f21aad43154cae4eb651204cffd0f9b4f12661d26c3784dc56366842
7
+ data.tar.gz: c6fc6777ef716da1e6ce80bceb3a8c9b76de70fbeb1794d0112b97d01f1e682074779df911d9db383eb47a5687732564ae67f8b37bc62b4dd5007db562cfe2e6
data/README.md CHANGED
@@ -2,13 +2,15 @@
2
2
 
3
3
  Ruby微信小程序服务端API
4
4
 
5
- DONE:接口调用凭据、openApi管理、小程序登陆、用户信息
5
+ DONE:接口调用凭据、openApi管理、小程序登陆、用户信息、小程序码与小程序链接
6
6
 
7
- TODO: 小程序码与小程序连接、小程序客服、消息相关、小程序安全、数据分析、硬件设备、直播、运维中心、插件管理...等
7
+ TODO: 小程序客服、消息相关、小程序安全、数据分析、硬件设备、直播、运维中心、插件管理...等
8
8
 
9
9
  ## Installation
10
10
 
11
+ ```shell
11
12
  gem install wx_miniprogram
13
+ ```
12
14
 
13
15
  ## Usage
14
16
 
@@ -16,19 +18,14 @@ gem install wx_miniprogram
16
18
 
17
19
  ```ruby
18
20
  client = WxMiniprogram::Client.new("appid", "secret")
19
-
20
- # Token
21
21
  # 获取Token
22
22
  client.get_access_token!
23
23
  # 获取稳定版Token
24
24
  client.get_stable_access_token!
25
25
  # 刷新Token
26
26
  client.refresh_access_token!
27
-
28
- # 用户信息
29
27
  # 获取插件用户openpid
30
28
  client.get_plugin_open_pid("openid")
31
-
32
29
  # 检查加密信息
33
30
  client.check_encrypted_data("encoded_str")
34
31
 
@@ -37,7 +34,7 @@ client.check_encrypted_data("encoded_str")
37
34
  ```
38
35
  ## Test
39
36
 
40
- Set environment variables WX_APPID and WX_SECRET and run tests.
37
+ TODO
41
38
 
42
39
  设置环境变量 WX_APPID 和 WX_SECRET
43
40
 
@@ -9,7 +9,6 @@ module WxMiniprogram
9
9
  include Login
10
10
  include UserInfo
11
11
 
12
-
13
12
  def initialize(appid, secret)
14
13
  @url = "https://api.weixin.qq.com/"
15
14
  @appid = appid
@@ -25,40 +24,42 @@ module WxMiniprogram
25
24
 
26
25
  def method_missing(method, *args)
27
26
  method_str = method.to_s
28
- unless method_str.end_with? "!"
27
+ if !method_str.end_with? "!"
29
28
  origin_method = (method_str + "!").to_sym
30
29
  if self.respond_to? origin_method
31
30
  begin
32
31
  send(origin_method, args)
33
32
  rescue ApiError => e
34
- puts "[debug]: #{e}" if @debug
33
+ $stderr.puts "[debug]: #{e}" if @debug
35
34
  false
36
35
  end
37
36
  else
38
37
  raise Error, "undefined method #{method.to_s}"
39
38
  end
40
- else
41
- unless self.respond_to? method
42
- raise Error, "undefined method #{method.to_s}"
43
- end
39
+ elif !self.respond_to?(method)
40
+ raise Error, "undefined method #{method.to_s}"
44
41
  end
45
42
  end
46
43
 
44
+ def respond_to_missing?(method, include_private=false)
45
+ return (self.respond_to? (method.to_s + "!").to_sym) || super
46
+ end
47
+
47
48
  private
48
- def get(path, body: nil, query: nil, need_access_token: false)
49
+ def get(path, body: nil, query: nil, need_access_token: true)
49
50
  need_access_token && valid
50
51
  resp = request(path, body: body, query: query, method: "get")
51
52
  if !resp["errcode"].nil? && resp["errcode"] != 0
52
- raise ApiError, resp["errmsg"]
53
+ raise ApiError, resp.to_s
53
54
  end
54
55
  resp
55
56
  end
56
57
 
57
- def post(path, body: nil, query: nil, need_access_token: false)
58
+ def post(path, body: nil, query: nil, need_access_token: true)
58
59
  need_access_token && valid
59
60
  resp = request(path, body: body, query: query, method: "post")
60
61
  if !resp["errcode"].nil? && resp["errcode"] != 0
61
- raise ApiError, resp["errmsg"]
62
+ raise ApiError, resp.to_s
62
63
  end
63
64
  resp
64
65
  end
@@ -8,7 +8,9 @@ module WxMiniprogram
8
8
  :secret => @secret,
9
9
  :js_code => js_code,
10
10
  :grant_type => "authorization_code",
11
- })
11
+ },
12
+ need_access_token: false
13
+ )
12
14
  end
13
15
 
14
16
  def check_session_key!(openid, session_key)
@@ -16,7 +18,7 @@ module WxMiniprogram
16
18
  :access_token => @access_token,
17
19
  :openid => openid,
18
20
  :signature => hmac_sha256(session_key, ""),
19
- :sigmethod => "hmac_sha256"
21
+ :sig_method => "hmac_sha256"
20
22
  })
21
23
  end
22
24
 
@@ -26,7 +28,7 @@ module WxMiniprogram
26
28
  }, body: {
27
29
  :openid => openid,
28
30
  :signature => hmac_sha256(session_key, ""),
29
- :sigmethod => "hmac_sha256"
31
+ :sig_method => "hmac_sha256"
30
32
  })
31
33
  end
32
34
 
@@ -5,24 +5,27 @@ module WxMiniprogram
5
5
  def clear_quota!
6
6
  post("cgi-bin/clear_quota",
7
7
  body: {:appid => @appid},
8
- query: {:access_token => @access_token}, need_access_token: true
9
- )
8
+ query: {:access_token => @access_token})
10
9
  end
11
10
 
12
- def get_api_quota
11
+ def get_api_quota!(cgi_path)
13
12
  post("cgi-bin/openapi/quota/get",
14
- query: {:access_token => @access_token}, need_access_token: true)
13
+ query: {:access_token => @access_token},
14
+ body: {:cgi_path => cgi_path}
15
+ )
15
16
  end
16
17
 
17
- def get_rid_info(rid)
18
+ def get_rid_info!(rid)
18
19
  post("cgi-bin/openapi/rid/get",
19
20
  body: {:rid => rid},
20
- query: {:access_token => @access_token}, need_access_token: true)
21
+ query: {:access_token => @access_token})
21
22
  end
22
23
 
23
- def clear_quota_by_app_secret(appid, appsecret)
24
+ def clear_quota_by_app_secret!(appid, appsecret)
24
25
  post("cgi-bin/clear_quota/v2",
25
- body: {:appid => appid, :appsecret => appsecret})
26
+ body: {:appid => appid, :appsecret => appsecret},
27
+ need_access_token: false
28
+ )
26
29
  end
27
30
  end
28
31
  end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WxMiniprogram
4
+ module QRCode
5
+
6
+ # 必填 path: 扫码进入的小程序页面路径
7
+ # 非必填 {width: number, auto_color: boolean, line_color: {r: "", g: "", b: ""}}
8
+ def get_qrcode!(path, width=nil, auto_color=false, line_color=nil)
9
+ body = {}
10
+ body[:width] = width unless width.nil?
11
+ body[:auto_color] = auto_color if auto_color
12
+ body[:line_color] = line_color unless line_color.nil?
13
+ post("wxa/getwxacode",
14
+ query: {:access_token => @access_token},
15
+ body: body
16
+ )
17
+ end
18
+
19
+ # 必填 scene
20
+ # 非必填 {page: string, check_path: boolean, env_version: string, width: number,
21
+ # auto_color: boolean, line_color: {r: string, g: string, b: string}, is_hyaline: boolean}
22
+ def get_unlimited_qrcode!(scene, path=nil, check_path=nil, env_version=nil, width=nil, auto_color=nil, line_color=nil, is_hyaline=nil)
23
+ body = {}
24
+ body[:path] = path unless path.nil?
25
+ body[:check_path] = check_path unless check_path.nil?
26
+ body[:env_version] = env_version unless env_version.nil?
27
+ body[:width] = width unless width.nil?
28
+ body[:auto_color] = auto_color unless auto_color.nil?
29
+ body[:line_color] = line_color unless line_color.nil?
30
+ body[:is_hyaline] = is_hyaline unless is_hyaline.nil?
31
+ post("wxa/getwxacodeunlimit",
32
+ query: {:access_token => @access_token},
33
+ body: body
34
+ )
35
+ end
36
+
37
+ def create_qrcode!(path, width=430)
38
+ post("cgi-bin/wxaapp/createwxaqrcode",
39
+ query: {:access_token => @access_token, :path => path},
40
+ body: {:width => width}
41
+ )
42
+ end
43
+
44
+ end
45
+ end
@@ -3,11 +3,14 @@
3
3
  module WxMiniprogram
4
4
  module Token
5
5
  def get_access_token!
6
- resp = get("cgi-bin/token", query: {
7
- :grant_type => "client_credential",
8
- :appid => @appid,
9
- :secret => @secret
10
- })
6
+ resp = get("cgi-bin/token",
7
+ query: {
8
+ :grant_type => "client_credential",
9
+ :appid => @appid,
10
+ :secret => @secret
11
+ },
12
+ need_access_token: false
13
+ )
11
14
  unless resp["access_token"].nil?
12
15
  @access_token = resp["access_token"]
13
16
  @expire = resp["expires_in"] + Time.now.to_i
@@ -16,12 +19,15 @@ module WxMiniprogram
16
19
  end
17
20
 
18
21
  def get_stable_access_token!(force_refresh=false)
19
- resp = post("cgi-bin/stable_token", body: {
20
- :grant_type => "client_credential",
21
- :appid => @appid,
22
- :secret => @secret,
23
- :force_refresh => force_refresh
24
- })
22
+ resp = post("cgi-bin/stable_token",
23
+ body: {
24
+ :grant_type => "client_credential",
25
+ :appid => @appid,
26
+ :secret => @secret,
27
+ :force_refresh => force_refresh
28
+ },
29
+ need_access_token: false
30
+ )
25
31
  unless resp["access_token"].nil?
26
32
  @access_token = resp["access_token"]
27
33
  @expire = resp["expires_in"] + Time.now.to_i
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WxMiniprogram
4
+ module UrlScheme
5
+ def generate_nfc_scheme!(model_id, jump_wxa={}, sn=nil)
6
+ body = {:jump_wxa => jump_wxa, :model_id => model_id}
7
+ body[:sn] = sn unless sn.nil?
8
+ post("wxa/generatenfcscheme",
9
+ query: {:access_token => @access_token},
10
+ body: body
11
+ )
12
+ end
13
+
14
+ def generate_scheme!(jump_wxa={}, expire_time=nil, expire_type=nil, expire_interval=nil)
15
+ body = {:jump_wxa => jump_wxa}
16
+ body[:expire_time] = expire_time unless expire_time.nil?
17
+ body[:expire_type] = expire_type unless expire_type.nil?
18
+ body[:expire_interval] = expire_interval unless expire_interval.nil?
19
+ post("wxa/generatescheme",
20
+ query: {:access_token => @access_token},
21
+ body: body
22
+ )
23
+ end
24
+
25
+ def query_scheme!(scheme=nil, query_type=nil)
26
+ body = {}
27
+ body[:scheme] = scheme unless scheme.nil?
28
+ body[:query_type] = query_type unless query_type.nil?
29
+ post("wxa/queryscheme",
30
+ query: {:access_token => @access_token},
31
+ body: body
32
+ )
33
+
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WxMiniprogram
4
+ module UrlLink
5
+ def generate_url_link!(path=nil, query=nil, expire_type=nil, expire_time=nil, expire_interval=nil, cloud_base=nil, env_version=nil)
6
+ body = {}
7
+ local_variables.each do |var_sym|
8
+ v = binding.local_variable_get(var_sym)
9
+ body[var_sym] = v if !v.nil? && var_sym != :body
10
+ end
11
+ post("wxa/generate_urllink",
12
+ query: {:access_token => @access_token},
13
+ body: body
14
+ )
15
+ end
16
+
17
+ def query_url_link!(url_link=nil, query_type=nil)
18
+ body = {}
19
+ body[:url_link] = url_link unless url_link.nil?
20
+ body[:query_type] = query_type unless query_type.nil?
21
+ post("wxa/query_urllink",
22
+ query: {:access_token => @access_token},
23
+ body: body
24
+ )
25
+ end
26
+
27
+ def generate_short_link!(page_url, page_title=nil, is_permanent=nil)
28
+ body = {:page_url => page_url}
29
+ body[:page_title] = page_title unless page_title.nil?
30
+ body[:is_permanent] = is_permanent unless is_permanent.nil?
31
+ post("wxa/genwxashortlink",
32
+ query: {:access_token => @access_token},
33
+ body: body
34
+ )
35
+ end
36
+ end
37
+ end
@@ -19,10 +19,14 @@ module WxMiniprogram
19
19
  })
20
20
  end
21
21
 
22
- def get_paid_unionid!(options)
22
+ def get_paid_unionid!(transaction_id=nil, mch_id=nil, out_trade_no=nil)
23
+ body = {}
24
+ body[:transaction_id] = transaction_id unless transaction_id.nil?
25
+ body[:mch_id] = mch_id unless mch_id.nil?
26
+ body[:out_trade_no] = out_trade_no unless out_trade_no.nil?
23
27
  get("wxa/getpaidunionid", query: {
24
28
  :access_token => @access_token
25
- }, body: options)
29
+ }, body: body)
26
30
  end
27
31
 
28
32
  def get_user_encryptKey!(openid, session_key)
@@ -2,7 +2,7 @@
2
2
 
3
3
  module WxMiniprogram
4
4
  module Util
5
- def hmac_sha256(data, key)
5
+ def hmac_sha256(key, data)
6
6
  OpenSSL::HMAC.hexdigest("SHA256", key, data)
7
7
  end
8
8
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WxMiniprogram
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.6"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wx_miniprogram
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - ranjiayu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-07-03 00:00:00.000000000 Z
11
+ date: 2024-07-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: wechat mini-program api client
14
14
  email:
@@ -17,7 +17,6 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
- - ".rspec"
21
20
  - LICENSE.txt
22
21
  - README.md
23
22
  - Rakefile
@@ -26,7 +25,10 @@ files:
26
25
  - lib/wx_miniprogram/client.rb
27
26
  - lib/wx_miniprogram/login.rb
28
27
  - lib/wx_miniprogram/open_api.rb
28
+ - lib/wx_miniprogram/qrcode.rb
29
29
  - lib/wx_miniprogram/token.rb
30
+ - lib/wx_miniprogram/url.rb
31
+ - lib/wx_miniprogram/url_link.rb
30
32
  - lib/wx_miniprogram/user_info.rb
31
33
  - lib/wx_miniprogram/util.rb
32
34
  - lib/wx_miniprogram/version.rb
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper