Commit ab519ba1ddf6ab885cd5ba83c1dcc1e4adf6222f
1 parent
4808be66
Exists in
master
修复 suite base
Showing
4 changed files
with
62 additions
and
61 deletions
Show diff stats
lib/qy_wechat_api/api.rb
lib/qy_wechat_api/api/service/base.rb
@@ -1,60 +0,0 @@ | @@ -1,60 +0,0 @@ | ||
1 | -# encoding: utf-8 | ||
2 | -module QyWechatApi | ||
3 | - module Api | ||
4 | - module Service | ||
5 | - class Base < Api::Base | ||
6 | - | ||
7 | - attr_accessor :suite_id, :suite_secret, :suite_ticket | ||
8 | - | ||
9 | - def initialize(suite_id, suite_secret, suite_ticket, options={}) | ||
10 | - @suite_id = suite_id | ||
11 | - @suite_secret = suite_secret | ||
12 | - @suite_ticket = suite_ticket | ||
13 | - end | ||
14 | - | ||
15 | - private | ||
16 | - # 获取应用套件令牌 | ||
17 | - # 获取suite_access_token时,还额外需要suite_ticket参数(请永远使用最新接收到的suite_ticket)。suite_ticket由企业号后台定时推送给应用套件,并每十分钟更新。 | ||
18 | - # 注2:通过本接口获取的accesstoken不会自动续期,每次获取都会自动更新。 | ||
19 | - def get_suite_token | ||
20 | - params = { | ||
21 | - suite_ticket: suite_ticket, | ||
22 | - suite_id: suite_id, | ||
23 | - suite_secret: suite_secret | ||
24 | - } | ||
25 | - Rails.cache.fetch(suite_id, expires_in: 7100.seconds) do | ||
26 | - Rails.logger.info("Invoke #{suite_id} get_suite_token to refresh") | ||
27 | - res = QyWechatApi.http_post_without_token( | ||
28 | - request_url("get_suite_token", params), | ||
29 | - params | ||
30 | - ) | ||
31 | - Rails.logger.info(res) | ||
32 | - token = res.result["suite_access_token"] | ||
33 | - if token.blank? | ||
34 | - Rails.cache.delete(suite_id) | ||
35 | - raise res.errors | ||
36 | - else | ||
37 | - token | ||
38 | - end | ||
39 | - end | ||
40 | - end | ||
41 | - | ||
42 | - def http_get(url, params={}) | ||
43 | - params.merge!({suite_id: suite_id}) | ||
44 | - params.merge!({suite_access_token: get_suite_token}) | ||
45 | - QyWechatApi.http_get_without_token(request_url(url, params), params) | ||
46 | - end | ||
47 | - | ||
48 | - def http_post(url, payload={}, params={}) | ||
49 | - params.merge!({suite_id: suite_id}) | ||
50 | - # 获取suite_token时不需要 | ||
51 | - if !params.keys.include?(:suite_ticket) | ||
52 | - params.merge!({suite_access_token: get_suite_token}) | ||
53 | - end | ||
54 | - QyWechatApi.http_post_without_token(request_url(url, params), payload, params) | ||
55 | - end | ||
56 | - end | ||
57 | - | ||
58 | - end | ||
59 | - end | ||
60 | -end |
@@ -0,0 +1,60 @@ | @@ -0,0 +1,60 @@ | ||
1 | +# encoding: utf-8 | ||
2 | +module QyWechatApi | ||
3 | + module Api | ||
4 | + module Service | ||
5 | + class ServiceBase < Api::Base | ||
6 | + | ||
7 | + attr_accessor :suite_id, :suite_secret, :suite_ticket | ||
8 | + | ||
9 | + def initialize(suite_id, suite_secret, suite_ticket, options={}) | ||
10 | + @suite_id = suite_id | ||
11 | + @suite_secret = suite_secret | ||
12 | + @suite_ticket = suite_ticket | ||
13 | + end | ||
14 | + | ||
15 | + private | ||
16 | + # 获取应用套件令牌 | ||
17 | + # 获取suite_access_token时,还额外需要suite_ticket参数(请永远使用最新接收到的suite_ticket)。suite_ticket由企业号后台定时推送给应用套件,并每十分钟更新。 | ||
18 | + # 注2:通过本接口获取的accesstoken不会自动续期,每次获取都会自动更新。 | ||
19 | + def get_suite_token | ||
20 | + params = { | ||
21 | + suite_ticket: suite_ticket, | ||
22 | + suite_id: suite_id, | ||
23 | + suite_secret: suite_secret | ||
24 | + } | ||
25 | + Rails.cache.fetch(suite_id, expires_in: 7100.seconds) do | ||
26 | + Rails.logger.info("Invoke #{suite_id} get_suite_token to refresh") | ||
27 | + res = QyWechatApi.http_post_without_token( | ||
28 | + request_url("get_suite_token", params), | ||
29 | + params | ||
30 | + ) | ||
31 | + Rails.logger.info(res) | ||
32 | + token = res.result["suite_access_token"] | ||
33 | + if token.blank? | ||
34 | + Rails.cache.delete(suite_id) | ||
35 | + raise res.errors | ||
36 | + else | ||
37 | + token | ||
38 | + end | ||
39 | + end | ||
40 | + end | ||
41 | + | ||
42 | + def http_get(url, params={}) | ||
43 | + params.merge!({suite_id: suite_id}) | ||
44 | + params.merge!({suite_access_token: get_suite_token}) | ||
45 | + QyWechatApi.http_get_without_token(request_url(url, params), params) | ||
46 | + end | ||
47 | + | ||
48 | + def http_post(url, payload={}, params={}) | ||
49 | + params.merge!({suite_id: suite_id}) | ||
50 | + # 获取suite_token时不需要 | ||
51 | + if !params.keys.include?(:suite_ticket) | ||
52 | + params.merge!({suite_access_token: get_suite_token}) | ||
53 | + end | ||
54 | + QyWechatApi.http_post_without_token(request_url(url, params), payload, params) | ||
55 | + end | ||
56 | + end | ||
57 | + | ||
58 | + end | ||
59 | + end | ||
60 | +end |
lib/qy_wechat_api/api/service/suite.rb