Commit 973a187c by lanrion

refactor client.rb

parent 0986bddd
...@@ -10,19 +10,22 @@ module WeixinAuthorize ...@@ -10,19 +10,22 @@ module WeixinAuthorize
attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in
attr_accessor :access_token attr_accessor :access_token
def initialize(app_id="", app_secret="", expired_at=nil) # 对于多用户微信营销平台的对接,需要把每次的expired_at, access_token保存在Redis中
# 每次使用,则可以从Redis中获取expired_at和access_token,即
# @client = WeixinAuthorize::Client.new(appid, appsecret, expired_at, access_token)
# 获取access_token,则仍然是:@client.get_access_token 来获取
def initialize(app_id="", app_secret="", expired_at=nil, access_token=nil)
@app_id = app_id @app_id = app_id
@app_secret = app_secret @app_secret = app_secret
@expired_at = (expired_at.to_i || Time.now.to_i) @expired_at = (expired_at.to_i || Time.now.to_i)
@access_token = access_token
yield self if block_given? yield self if block_given?
end end
# return token # return token
def get_access_token def get_access_token
# 如果当前token过期时间小于现在的时间,则重新获取一次 # 如果当前token过期时间小于现在的时间,则重新获取一次
if @expired_at <= Time.now.to_i authenticate if token_expired?
authenticate
end
@access_token @access_token
end end
...@@ -33,26 +36,22 @@ module WeixinAuthorize ...@@ -33,26 +36,22 @@ module WeixinAuthorize
self.expired_at = Time.now.to_i + hash_infos["expires_in"] self.expired_at = Time.now.to_i + hash_infos["expires_in"]
end end
def token_expired?
@expired_at <= Time.now.to_i
end
private private
def authenticate_options def authenticate_options
{grant_type: "client_credential", appid: app_id, secret: app_secret} {grant_type: "client_credential", appid: app_id, secret: app_secret}
end end
def plain_endpoint
"https://api.weixin.qq.com/cgi-bin"
end
def file_endpoint
"http://file.api.weixin.qq.com/cgi-bin"
end
def access_token_param def access_token_param
{access_token: get_access_token} {access_token: get_access_token}
end end
def http_get_without_token(url, options={}, endpoint="plain") def http_get_without_token(url, options={}, endpoint="plain")
get_api_url = send("#{endpoint}_endpoint") + url get_api_url = endpoint_url(endpoint) + url
JSON.parse(RestClient.get(get_api_url, :params => options)) JSON.parse(RestClient.get(get_api_url, :params => options))
end end
...@@ -63,9 +62,21 @@ module WeixinAuthorize ...@@ -63,9 +62,21 @@ module WeixinAuthorize
# Refactor # Refactor
def http_post(url, options={}, endpoint="plain") def http_post(url, options={}, endpoint="plain")
post_api_url = send("#{endpoint}_endpoint") + url + "?access_token=#{get_access_token}" post_api_url = endpoint_url(endpoint) + url + "?access_token=#{get_access_token}"
JSON.parse(RestClient.post(post_api_url ,options)) JSON.parse(RestClient.post(post_api_url ,options))
end end
def endpoint_url(endpoint)
send("#{endpoint}_endpoint")
end
def plain_endpoint
"https://api.weixin.qq.com/cgi-bin"
end
def file_endpoint
"http://file.api.weixin.qq.com/cgi-bin"
end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment