Commit 623b779cf4c04afbc119a129f01497cd9f43fc69

Authored by lanrion
1 parent 8c578b66
Exists in master

添加js api

lib/qy_wechat_api/api/base.rb
... ... @@ -26,12 +26,12 @@ module QyWechatApi
26 26 end
27 27  
28 28 def request_url(url, params={})
29   - use_base_url = params.delete(:use_base_url)
30   - if !use_base_url
  29 + waive_base_url = params.delete(:waive_base_url)
  30 + if waive_base_url
  31 + url
  32 + else
31 33 # 使用基础 +base_url+进行拼接
32 34 "#{base_url}/#{url}"
33   - else
34   - url
35 35 end
36 36 end
37 37  
... ...
lib/qy_wechat_api/api/js.rb 0 → 100644
... ... @@ -0,0 +1,27 @@
  1 +module QyWechatApi
  2 + module Api
  3 + class Js < Base
  4 + def sign_package(url)
  5 + timestamp = Time.now.to_i
  6 + noncestr = SecureRandom.hex(16)
  7 + str = "jsapi_ticket=#{get_jsticket}&noncestr=#{noncestr}&timestamp=#{timestamp}&url=#{url}";
  8 + signature = Digest::SHA1.hexdigest(str)
  9 + {
  10 + "appId" => corp_id, "nonceStr" => noncestr,
  11 + "timestamp" => timestamp, "url" => url,
  12 + "signature" => signature, "rawString" => str
  13 + }
  14 + end
  15 +
  16 + # https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKE
  17 + def get_jsticket
  18 + cache_key = "jsticket-#{client.redis_key}"
  19 + Rails.cache.fetch(cache_key, expires_in: 7100.seconds) do
  20 + res = http_get(get_jsapi_ticket, {waive_base_url: true})
  21 + res.result["ticket"]
  22 + end
  23 + end
  24 +
  25 + end
  26 + end
  27 +end
... ...
lib/qy_wechat_api/api/user.rb
... ... @@ -66,7 +66,7 @@ module QyWechatApi
66 66 # 邀请成员关注
67 67 def send_invitation(user_id, tips="")
68 68 payload = {userid: user_id, invite_tips: tips}
69   - http_post("/invite/send", payload, {use_base_url: false})
  69 + http_post("/invite/send", payload, {waive_base_url: true})
70 70 end
71 71  
72 72 private
... ...
lib/qy_wechat_api/client.rb
... ... @@ -54,6 +54,10 @@ module QyWechatApi
54 54 Api::Oauth.new(get_access_token, corp_id)
55 55 end
56 56  
  57 + def js
  58 + Api::Js.new(get_access_token, corp_id)
  59 + end
  60 +
57 61 private
58 62  
59 63 def security_redis_key(key)
... ...