Commit 973a187c by lanrion

refactor client.rb

parent 0986bddd
......@@ -10,19 +10,22 @@ module WeixinAuthorize
attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in
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_secret = app_secret
@expired_at = (expired_at.to_i || Time.now.to_i)
@access_token = access_token
yield self if block_given?
end
# return token
def get_access_token
# 如果当前token过期时间小于现在的时间,则重新获取一次
if @expired_at <= Time.now.to_i
authenticate
end
authenticate if token_expired?
@access_token
end
......@@ -33,26 +36,22 @@ module WeixinAuthorize
self.expired_at = Time.now.to_i + hash_infos["expires_in"]
end
def token_expired?
@expired_at <= Time.now.to_i
end
private
def authenticate_options
{grant_type: "client_credential", appid: app_id, secret: app_secret}
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
{access_token: get_access_token}
end
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))
end
......@@ -63,9 +62,21 @@ module WeixinAuthorize
# Refactor
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))
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
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