Commit 5d73b474 by lanrion

added is_valid? method to valid app_id and app_secret

parent caa57a87
...@@ -37,12 +37,22 @@ module WeixinAuthorize ...@@ -37,12 +37,22 @@ module WeixinAuthorize
# authenticate access_token # authenticate access_token
def authenticate def authenticate
if is_weixin_redis_blank? if is_weixin_redis_blank?
http_get_access_token set_access_token_for_client
else else
authenticate_with_redis authenticate_with_redis
end end
end end
# 检查appid和app_secret是否有效。
def is_valid?
valid_result = http_get_access_token
if valid_result.keys.include?("access_token")
set_access_token_for_client(valid_result)
return true
end
false
end
def token_expired? def token_expired?
if is_weixin_redis_blank? if is_weixin_redis_blank?
# 如果当前token过期时间小于现在的时间,则重新获取一次 # 如果当前token过期时间小于现在的时间,则重新获取一次
...@@ -55,15 +65,20 @@ module WeixinAuthorize ...@@ -55,15 +65,20 @@ module WeixinAuthorize
private private
def authenticate_with_redis def authenticate_with_redis
http_get_access_token set_access_token_for_client
weixin_redis.hmset(redis_key, :access_token, access_token, :expired_at, expired_at) weixin_redis.hmset(redis_key, :access_token, access_token, :expired_at, expired_at)
weixin_redis.expireat(redis_key, expired_at.to_i-10) # 提前10秒超时 weixin_redis.expireat(redis_key, expired_at.to_i-10) # 提前10秒超时
end end
def set_access_token_for_client(access_token_infos=nil)
token_infos = access_token_infos || http_get_access_token
self.access_token = token_infos["access_token"]
self.expired_at = Time.now.to_i + token_infos["expires_in"]
end
def http_get_access_token def http_get_access_token
hash_infos = http_get_without_token("/token", authenticate_options) hash_infos = http_get_without_token("/token", authenticate_options)
self.access_token = hash_infos["access_token"] hash_infos
self.expired_at = Time.now.to_i + hash_infos["expires_in"]
end end
def authenticate_options def authenticate_options
......
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