Commit 053755de by lanrion

fixed bug when invoke is_valid? twice

parent 869dfd9e
...@@ -33,6 +33,8 @@ module WeixinAuthorize ...@@ -33,6 +33,8 @@ module WeixinAuthorize
# 检查appid和app_secret是否有效。 # 检查appid和app_secret是否有效。
def is_valid? def is_valid?
# 重新验证,清除存储在Redis的access_token
weixin_redis.del(redis_key) if !is_weixin_redis_blank?
valid_result = http_get_access_token valid_result = http_get_access_token
if valid_result.keys.include?("access_token") if valid_result.keys.include?("access_token")
set_access_token_for_client(valid_result) set_access_token_for_client(valid_result)
...@@ -55,15 +57,13 @@ module WeixinAuthorize ...@@ -55,15 +57,13 @@ module WeixinAuthorize
# authenticate access_token # authenticate access_token
def authenticate def authenticate
raise "APPID or APPSECRET is invalid" if !is_valid? raise "APPID or APPSECRET is invalid" if !is_valid?
if is_weixin_redis_blank? set_access_token_for_client
set_access_token_for_client if !is_weixin_redis_blank?
else
authenticate_with_redis authenticate_with_redis
end end
end end
def authenticate_with_redis def authenticate_with_redis
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
......
...@@ -7,7 +7,16 @@ describe WeixinAuthorize::Api::User do ...@@ -7,7 +7,16 @@ describe WeixinAuthorize::Api::User do
end end
it "can get followers infos" do it "can get followers infos" do
valid_info = $client.is_valid?
expect(valid_info).to eq(true)
followers = $client.followers followers = $client.followers
expect(followers.keys).to eq(["total", "count", "data", "next_openid"]) expect(followers.keys).to eq(["total", "count", "data", "next_openid"])
puts valid_info
valid_info = $client.is_valid?
expect(valid_info).to eq(true)
followers = $client.followers
expect(followers.keys).to eq(["total", "count", "data", "next_openid"])
puts valid_info
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