class WeixinAuthorize::Client

Attributes

access_token[RW]
app_id[RW]
app_secret[RW]
custom_access_token[RW]
expired_at[RW]
jsticket[RW]
jsticket_expired_at[RW]
jsticket_redis_key[RW]
redis_key[RW]

Public Class Methods

new(app_id, app_secret, options={}) click to toggle source

options: redis_key, custom_access_token

Calls superclass method
# File lib/weixin_authorize/client.rb, line 26
def initialize(app_id, app_secret, options={})
  @app_id = app_id
  @app_secret = app_secret
  @jsticket_expired_at = @expired_at = Time.now.to_i
  @redis_key = security_redis_key(options[:redis_key] || "weixin_#{app_id}")
  @jsticket_redis_key = security_redis_key("js_sdk_#{app_id}")
  @custom_access_token = options[:custom_access_token]
  super() # Monitor#initialize
end

Public Instance Methods

get_access_token() click to toggle source

return token

# File lib/weixin_authorize/client.rb, line 37
def get_access_token
  return custom_access_token if !custom_access_token.nil?
  synchronize{ token_store.access_token }
end
get_jssign_package(url) click to toggle source

获取js sdk 签名包

# File lib/weixin_authorize/client.rb, line 61
def get_jssign_package(url)
  timestamp = Time.now.to_i
  noncestr = SecureRandom.hex(16)
  str = "jsapi_ticket=#{get_jsticket}&noncestr=#{noncestr}&timestamp=#{timestamp}&url=#{url}";
  signature = Digest::SHA1.hexdigest(str)
  {
    "appId"     => app_id,    "nonceStr"  => noncestr,
    "timestamp" => timestamp, "url"       => url,
    "signature" => signature, "rawString" => str
  }
end
get_jsticket() click to toggle source
# File lib/weixin_authorize/client.rb, line 56
def get_jsticket
  jsticket_store.jsticket
end
http_get(url, url_params={}, endpoint="plain") click to toggle source

暴露出:http_get,http_post两个方法,方便第三方开发者扩展未开发的微信API。

# File lib/weixin_authorize/client.rb, line 74
def http_get(url, url_params={}, endpoint="plain")
  url_params = url_params.merge(access_token_param)
  WeixinAuthorize.http_get_without_token(url, url_params, endpoint)
end
http_post(url, post_body={}, url_params={}, endpoint="plain") click to toggle source
# File lib/weixin_authorize/client.rb, line 79
def http_post(url, post_body={}, url_params={}, endpoint="plain")
  url_params = access_token_param.merge(url_params)
  WeixinAuthorize.http_post_without_token(url, post_body, url_params, endpoint)
end
is_valid?() click to toggle source

检查appid和app_secret是否有效。

# File lib/weixin_authorize/client.rb, line 43
def is_valid?
  return true if !custom_access_token.nil?
  token_store.valid?
end
jsticket_store() click to toggle source
# File lib/weixin_authorize/client.rb, line 52
def jsticket_store
  JsTicket::Store.init_with(self)
end
token_store() click to toggle source
# File lib/weixin_authorize/client.rb, line 48
def token_store
  Token::Store.init_with(self)
end

Private Instance Methods

access_token_param() click to toggle source
# File lib/weixin_authorize/client.rb, line 86
def access_token_param
  {access_token: get_access_token}
end
security_redis_key(key) click to toggle source
# File lib/weixin_authorize/client.rb, line 90
def security_redis_key(key)
  Digest::MD5.hexdigest(key.to_s).upcase
end