Class: WeixinAuthorize::Client

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin, 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

Constants included from Api::Custom

Api::Custom::CUSTOM_RECORD_URL, Api::Custom::CUSTOM_SERVICE, Api::Custom::KF_SESSION_URL

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, #qrcode_authorize_url, #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_image, #upload_mass_news, #upload_mass_video, #upload_media

Methods included from Api::Qrcode

#create_qr_limit_scene, #create_qr_limit_str_scene, #create_qr_scene, #create_qr_str_scene, #qr_code_url

Methods included from Api::Groups

#batch_update_group_for_openids, #create_group, #delete_group, #get_group_for, #groups, #update_group_for_openid, #update_group_name

Methods included from Api::Custom

#create_kf_session, #get_custom_msg_record, #send_image_custom, #send_mpnews_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, #users

Constructor Details

#initialize(app_id, app_secret, options = {}) ⇒ Client

options: redis_key, custom_access_token



26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



22
23
24
# File 'lib/weixin_authorize/client.rb', line 22

def access_token
  @access_token
end

#app_idObject

Time.now + expires_in



21
22
23
# File 'lib/weixin_authorize/client.rb', line 21

def app_id
  @app_id
end

#app_secretObject

Time.now + expires_in



21
22
23
# File 'lib/weixin_authorize/client.rb', line 21

def app_secret
  @app_secret
end

#custom_access_tokenObject

Returns the value of attribute custom_access_token.



22
23
24
# File 'lib/weixin_authorize/client.rb', line 22

def custom_access_token
  @custom_access_token
end

#expired_atObject

Time.now + expires_in



21
22
23
# File 'lib/weixin_authorize/client.rb', line 21

def expired_at
  @expired_at
end

#jsticketObject

Returns the value of attribute jsticket.



23
24
25
# File 'lib/weixin_authorize/client.rb', line 23

def jsticket
  @jsticket
end

#jsticket_expired_atObject

Returns the value of attribute jsticket_expired_at.



23
24
25
# File 'lib/weixin_authorize/client.rb', line 23

def jsticket_expired_at
  @jsticket_expired_at
end

#jsticket_redis_keyObject

Returns the value of attribute jsticket_redis_key.



23
24
25
# File 'lib/weixin_authorize/client.rb', line 23

def jsticket_redis_key
  @jsticket_redis_key
end

#redis_keyObject

Returns the value of attribute redis_key.



22
23
24
# File 'lib/weixin_authorize/client.rb', line 22

def redis_key
  @redis_key
end

Instance Method Details

#get_access_tokenObject

return token



37
38
39
40
# 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) ⇒ Object

获取js sdk 签名包



61
62
63
64
65
66
67
68
69
70
71
# 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_jsticketObject



56
57
58
# File 'lib/weixin_authorize/client.rb', line 56

def get_jsticket
  jsticket_store.jsticket
end

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

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



74
75
76
77
# 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") ⇒ Object



79
80
81
82
# 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?Boolean

检查appid和app_secret是否有效。

Returns:

  • (Boolean)


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

def is_valid?
  return true if !custom_access_token.nil?
  token_store.valid?
end

#jsticket_storeObject



52
53
54
# File 'lib/weixin_authorize/client.rb', line 52

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

#token_storeObject



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

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