Commit 77af8d4d by wikimo

[wikimo]add config for token key expired

parent 555ac834
......@@ -88,6 +88,10 @@ module WeixinAuthorize
"https://open.weixin.qq.com#{url}"
end
def calculate_expire(expires_in)
Time.now + expires_in.to_i - key_expired
end
end
end
......@@ -13,6 +13,10 @@ module WeixinAuthorize
@redis ||= config.redis
end
def key_expired
config.key_expired || 100
end
# 可选配置: RestClient timeout, etc.
# key 必须是符号
# 如果出现 RestClient::SSLCertificateNotVerified Exception: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed
......@@ -26,6 +30,6 @@ module WeixinAuthorize
end
class Config
attr_accessor :redis, :rest_client_options
attr_accessor :redis, :rest_client_options, :key_expired
end
end
......@@ -32,7 +32,7 @@ module WeixinAuthorize
def set_jsticket
result = client.http_get("/ticket/getticket", {type: 1}).result
client.jsticket = result["ticket"]
client.jsticket_expired_at = result["expires_in"] + Time.now.to_i
client.jsticket_expired_at = WeixinAuthorize.calculate_expire(result["expires_in"])
end
end
......
......@@ -19,7 +19,7 @@ module WeixinAuthorize
client.access_token, "expired_at",
client.expired_at
)
weixin_redis.expireat(client.redis_key, client.expired_at.to_i-100) # 提前10秒超时
weixin_redis.expireat(client.redis_key, client.expired_at.to_i)
end
def access_token
......
......@@ -47,7 +47,7 @@ module WeixinAuthorize
def set_access_token(access_token_infos=nil)
token_infos = access_token_infos || http_get_access_token.result
client.access_token = token_infos["access_token"]
client.expired_at = Time.now.to_i + token_infos["expires_in"].to_i
client.expired_at = WeixinAuthorize.calculate_expire(token_infos["expires_in"])
end
def http_get_access_token
......
......@@ -55,6 +55,7 @@ redis_with_ns = Redis::Namespace.new("#{namespace}", :redis => redis)
WeixinAuthorize.configure do |config|
config.redis = redis_with_ns
# config.key_expired = 200
config.rest_client_options = {timeout: 10, open_timeout: 10, verify_ssl: true}
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