Sha256: e49efa49a259e58743fe08e50edc9efb513fbde8cf70932551b2a015f8c0e82e

Contents?: true

Size: 1.9 KB

Versions: 5

Compression:

Stored size: 1.9 KB

Contents

module EricWeixin
  class AccessToken < ActiveRecord::Base
    require "rest-client"
    self.table_name = "weixin_access_tokens"


    # 获取有效的Token
    # 参数为: weixin_secret_key
    #  ::EricWeixin::AccessToken.get_valid_access_token weixin_secret_key: 'bba7ba32bf3e1a60edd8bd8903ce33e5'
    def self.get_valid_access_token options
      ::EricWeixin::AccessToken.transaction do
        access_token = ::EricWeixin::AccessToken.where(weixin_secret_key: options[:weixin_secret_key]).first
        if access_token.blank?
          public_account = ::EricWeixin::PublicAccount.find_by_weixin_secret_key(options[:weixin_secret_key]).first
          access_token = ::EricWeixin::AccessToken.new :weixin_secret_key => options[:weixin_secret_key],
                                                   :access_token => get_new_token(options[:weixin_secret_key]),
                                                   :expired_at => Time.now.to_i + 2*60*60,
                                                   :public_account_id => public_account.id
          access_token.save!
        end

        if Time.now.to_i > (access_token.expired_at.to_i - 30)
          access_token.access_token = get_new_token(options[:weixin_secret_key])
          access_token.expired_at = Time.now.to_i + 2*60*60
          access_token.save!
        end
        access_token.reload
        access_token.access_token
      end
    end


    private
    #::EricWeixin::AccessToken.get_new_token 'bba7ba32bf3e1a60edd8bd8903ce33e5'
    def self.get_new_token secret_key
      account = ::EricWeixin::PublicAccount.where(:weixin_secret_key => secret_key).first
      BusinessException.raise 'account 不存在' if account.blank?
      response = RestClient.get "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{account.weixin_app_id}&secret=#{account.weixin_secret_key}"
      JSON.parse(response)["access_token"]
    end
  end
end



Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
eric_weixin-0.0.6 lib/eric_weixin/app/model/access_token.rb
eric_weixin-0.0.5 lib/eric_weixin/app/model/access_token.rb
eric_weixin-0.0.4 lib/eric_weixin/app/model/access_token.rb
eric_weixin-0.0.3 lib/eric_weixin/app/model/access_token.rb
eric_weixin-0.0.2 lib/eric_weixin/app/model/access_token.rb