Class: EricWeixin::JsapiTicket
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EricWeixin::JsapiTicket
- Defined in:
- app/models/eric_weixin/jsapi_ticket.rb
Class Method Summary collapse
-
.get_new_ticket(public_account_id) ⇒ Object
根据微信公众号从微信服务器获取最新的 AccessToken.
-
.get_valid_jsapi_ticket_by_app_id(options) ⇒ Object
获取 jsapi ticket 方法一。根据 APPID 查到微信公众号 PublicAccount.id ,然后 调用 ::EricWeixin::JsapiTicket.get_valid_ticket 获取 jsapi ticket 并作为返回值返回.
-
.get_valid_ticket(options) ⇒ Object
获取 jsapi ticket 方法二。根据微信公众号 PublicAccount.id 获取 jsapi ticket .
Class Method Details
.get_new_ticket(public_account_id) ⇒ Object
根据微信公众号从微信服务器获取最新的 AccessToken.
参数说明
-
public_account_id #公众账号 ID
调用示例
::EricWeixin::AccessToken.get_new_token ‘5e3b98ca0000959946657212739fd535’ api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=12&secret=23
53 54 55 56 57 58 59 60 61 62 |
# File 'app/models/eric_weixin/jsapi_ticket.rb', line 53 def self.get_new_ticket public_account_id account = ::EricWeixin::PublicAccount.find_by_id public_account_id BusinessException.raise 'account 不存在' if account.blank? # url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{account.weixin_app_id}&secret=#{account.weixin_secret_key}" access_token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: account.id url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=#{access_token}&type=jsapi" response = RestClient.get url pp response JSON.parse(response)["ticket"] end |
.get_valid_jsapi_ticket_by_app_id(options) ⇒ Object
获取 jsapi ticket 方法一。根据 APPID 查到微信公众号 PublicAccount.id ,然后 调用 ::EricWeixin::JsapiTicket.get_valid_ticket 获取 jsapi ticket 并作为返回值返回.
参数说明
-
app_id #微信公众号的 app_id
调用示例
::EricWeixin::JsapiTicket.get_valid_jsapi_ticket_by_app_id app_id: 'wx51729870d9012531'
15 16 17 18 |
# File 'app/models/eric_weixin/jsapi_ticket.rb', line 15 def self.get_valid_jsapi_ticket_by_app_id pa = ::EricWeixin::PublicAccount.find_by_weixin_app_id [:app_id] ::EricWeixin::JsapiTicket.get_valid_ticket public_account_id: pa.id end |
.get_valid_ticket(options) ⇒ Object
获取 jsapi ticket 方法二。根据微信公众号 PublicAccount.id 获取 jsapi ticket . 若微信公众号未存在 jsapi ticket 或者 jsapi ticket 过期都立即获取新的并返回。
参数说明
-
public_account_id #公众账号 ID
调用示例
::EricWeixin::JsapiTicket.get_valid_ticket public_account_id: 1
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/eric_weixin/jsapi_ticket.rb', line 26 def self.get_valid_ticket self.transaction do ticket = ::EricWeixin::JsapiTicket.find_by_public_account_id [:public_account_id] if ticket.blank? public_account = ::EricWeixin::PublicAccount.find_by_id [:public_account_id] ticket = ::EricWeixin::JsapiTicket.new jsapi_ticket: get_new_ticket(public_account.id), expired_at: Time.now.to_i + 2*60*60, public_account_id: public_account.id ticket.save! end if Time.now.to_i > (ticket.expired_at.to_i - 30) ticket.jsapi_ticket = get_new_ticket [:public_account_id] ticket.expired_at = Time.now.to_i + 2*60*60 ticket.save! end ticket.reload ticket.jsapi_ticket end end |