Commit 13590ba2 by mingyuan

get js api

parent a3f50b73
...@@ -12,6 +12,10 @@ module Dingtalk::Api ...@@ -12,6 +12,10 @@ module Dingtalk::Api
@client.get 'gettoken', params: { appkey: app.app_key, appsecret: app.app_secret }, base: BASE @client.get 'gettoken', params: { appkey: app.app_key, appsecret: app.app_secret }, base: BASE
end end
def jsapi_ticket
get 'get_jsapi_ticket', base: BASE
end
def get(path, params: {}, headers: {}, base: nil, as: nil) def get(path, params: {}, headers: {}, base: nil, as: nil)
with_access_token(params) do |with_token_params| with_access_token(params) do |with_token_params|
@client.get path, headers: headers, params: with_token_params, base: base, as: as @client.get path, headers: headers, params: with_token_params, base: base, as: as
......
...@@ -10,6 +10,9 @@ module Dingtalk ...@@ -10,6 +10,9 @@ module Dingtalk
attribute :corp_id, :string attribute :corp_id, :string
attribute :access_token, :string attribute :access_token, :string
attribute :access_token_expires_at, :datetime attribute :access_token_expires_at, :datetime
attribute :jsapi_ticket, :string
attribute :oauth2_state, :string
attribute :jsapi_ticket_expires_at, :datetime
end end
def api def api
...@@ -37,5 +40,30 @@ module Dingtalk ...@@ -37,5 +40,30 @@ module Dingtalk
access_token_expires_at > Time.current access_token_expires_at > Time.current
end end
def jsapi_ticket
if jsapi_ticket_valid?
super
else
refresh_jsapi_ticket
end
end
def jsapi_ticket_valid?
return false unless jsapi_ticket_expires_at.acts_like?(:time)
jsapi_ticket_expires_at > Time.current
end
def refresh_jsapi_ticket
r = api.jsapi_ticket
store_jsapi_ticket(r)
jsapi_ticket
end
def store_jsapi_ticket(ticket_hash)
self.jsapi_ticket = ticket_hash['ticket']
self.jsapi_ticket_expires_at = Time.current + ticket_hash['expires_in'].to_i
self.save
end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment