Class: WeixinAuthorize::Client

Inherits:
Object
  • Object
show all
Includes:
Api::Custom, Api::Groups, Api::Mass, Api::Media, Api::Menu, Api::Oauth, Api::Qrcode, Api::Template, Api::User
Defined in:
lib/weixin_authorize/client.rb

Constant Summary

Constants included from Api::Mass

Api::Mass::MSG_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Api::Template

#add_template, #send_template_msg, #set_template_industry

Methods included from Api::Oauth

#authorize_url, #get_oauth_access_token, #get_oauth_userinfo, #refresh_oauth2_token

Methods included from Api::Mass

#mass_delete_with_msgid, #mass_get_status, #mass_preview, #mass_with_group, #mass_with_openids

Methods included from Api::Media

#download_media_url, #upload_mass_news, #upload_mass_video, #upload_media

Methods included from Api::Qrcode

#create_qr_limit_scene, #create_qr_scene, #qr_code_url

Methods included from Api::Groups

#create_group, #get_group_for, #groups, #update_group_for_openid, #update_group_name

Methods included from Api::Custom

#send_image_custom, #send_music_custom, #send_news_custom, #send_text_custom, #send_video_custom, #send_voice_custom

Methods included from Api::Menu

#create_menu, #delete_menu, #menu

Methods included from Api::User

#followers, #update_remark, #user

Constructor Details

#initialize(app_id, app_secret, redis_key = nil) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
27
# File 'lib/weixin_authorize/client.rb', line 21

def initialize(app_id, app_secret, redis_key=nil)
  @app_id     = app_id
  @app_secret = app_secret
  @jsticket_expired_at = @expired_at = Time.now.to_i
  @redis_key  = security_redis_key(redis_key || "weixin_#{app_id}")
  @jsticket_redis_key = security_redis_key("js_sdk_#{app_id}")
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



18
19
20
# File 'lib/weixin_authorize/client.rb', line 18

def access_token
  @access_token
end

#app_idObject

Time.now + expires_in



17
18
19
# File 'lib/weixin_authorize/client.rb', line 17

def app_id
  @app_id
end

#app_secretObject

Time.now + expires_in



17
18
19
# File 'lib/weixin_authorize/client.rb', line 17

def app_secret
  @app_secret
end

#expired_atObject

Time.now + expires_in



17
18
19
# File 'lib/weixin_authorize/client.rb', line 17

def expired_at
  @expired_at
end

#jsticketObject

Returns the value of attribute jsticket.



19
20
21
# File 'lib/weixin_authorize/client.rb', line 19

def jsticket
  @jsticket
end

#jsticket_expired_atObject

Returns the value of attribute jsticket_expired_at.



19
20
21
# File 'lib/weixin_authorize/client.rb', line 19

def jsticket_expired_at
  @jsticket_expired_at
end

#jsticket_redis_keyObject

Returns the value of attribute jsticket_redis_key.



19
20
21
# File 'lib/weixin_authorize/client.rb', line 19

def jsticket_redis_key
  @jsticket_redis_key
end

#redis_keyObject

Returns the value of attribute redis_key.



18
19
20
# File 'lib/weixin_authorize/client.rb', line 18

def redis_key
  @redis_key
end

Instance Method Details

#get_access_tokenObject

return token



30
31
32
# File 'lib/weixin_authorize/client.rb', line 30

def get_access_token
  token_store.access_token
end

#get_jssign_package(url) ⇒ Object

获取js sdk 签名包



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/weixin_authorize/client.rb', line 52

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_jsticketObject



47
48
49
# File 'lib/weixin_authorize/client.rb', line 47

def get_jsticket
  jsticket_store.jsticket
end

#http_get(url, headers = {}, endpoint = "plain") ⇒ Object

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



65
66
67
68
# File 'lib/weixin_authorize/client.rb', line 65

def http_get(url, headers={}, endpoint="plain")
  headers = headers.merge(access_token_param)
  WeixinAuthorize.http_get_without_token(url, headers, endpoint)
end

#http_post(url, payload = {}, headers = {}, endpoint = "plain") ⇒ Object



70
71
72
73
# File 'lib/weixin_authorize/client.rb', line 70

def http_post(url, payload={}, headers={}, endpoint="plain")
  headers = access_token_param.merge(headers)
  WeixinAuthorize.http_post_without_token(url, payload, headers, endpoint)
end

#is_valid?Boolean

检查appid和app_secret是否有效。

Returns:

  • (Boolean)


35
36
37
# File 'lib/weixin_authorize/client.rb', line 35

def is_valid?
  token_store.valid?
end

#jsticket_storeObject



43
44
45
# File 'lib/weixin_authorize/client.rb', line 43

def jsticket_store
  JsTicket::Store.init_with(self)
end

#token_storeObject



39
40
41
# File 'lib/weixin_authorize/client.rb', line 39

def token_store
  Token::Store.init_with(self)
end