Merge pull request #515 from thsrite/main
This commit is contained in:
@ -215,9 +215,6 @@ class MessageForward(_PluginBase):
|
|||||||
"""
|
"""
|
||||||
获取并存储wechat token
|
获取并存储wechat token
|
||||||
"""
|
"""
|
||||||
# 查询历史
|
|
||||||
wechat_token_history = self.get_data("wechat_token") or {}
|
|
||||||
|
|
||||||
# 解析配置
|
# 解析配置
|
||||||
wechats = self._wechat.split("\n")
|
wechats = self._wechat.split("\n")
|
||||||
for index, wechat in enumerate(wechats):
|
for index, wechat in enumerate(wechats):
|
||||||
@ -229,34 +226,14 @@ class MessageForward(_PluginBase):
|
|||||||
corpid = wechat_config[1]
|
corpid = wechat_config[1]
|
||||||
appsecret = wechat_config[2]
|
appsecret = wechat_config[2]
|
||||||
|
|
||||||
# 查询历史是否存储token
|
|
||||||
wechat_config = wechat_token_history.get(appid)
|
|
||||||
access_token = None
|
|
||||||
expires_in = None
|
|
||||||
access_token_time = None
|
|
||||||
if wechat_config:
|
|
||||||
access_token_time = wechat_config['access_token_time']
|
|
||||||
expires_in = wechat_config['expires_in']
|
|
||||||
access_token = wechat_config['access_token']
|
|
||||||
# 判断token是否过期
|
|
||||||
if (datetime.now() - datetime.strptime(access_token_time, "%Y-%m-%d %H:%M:%S")).seconds >= expires_in:
|
|
||||||
# 已过期,重新获取token
|
# 已过期,重新获取token
|
||||||
access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid,
|
access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid,
|
||||||
appsecret=appsecret)
|
appsecret=appsecret)
|
||||||
if not access_token:
|
if not access_token:
|
||||||
# 没有token,获取token
|
# 没有token,获取token
|
||||||
access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid,
|
logger.error(f"wechat配置 appid = {appid} 获取token失败,请检查配置")
|
||||||
appsecret=appsecret)
|
continue
|
||||||
if access_token:
|
|
||||||
if isinstance(access_token_time, datetime):
|
|
||||||
access_token_time = access_token_time.strftime('%Y-%m-%d %H:%M:%S')
|
|
||||||
wechat_token_history[appid] = {
|
|
||||||
"access_token": access_token,
|
|
||||||
"expires_in": expires_in,
|
|
||||||
"access_token_time": access_token_time,
|
|
||||||
"corpid": corpid,
|
|
||||||
"appsecret": appsecret
|
|
||||||
}
|
|
||||||
self._pattern_token[index] = {
|
self._pattern_token[index] = {
|
||||||
"appid": appid,
|
"appid": appid,
|
||||||
"corpid": corpid,
|
"corpid": corpid,
|
||||||
@ -265,12 +242,6 @@ class MessageForward(_PluginBase):
|
|||||||
"expires_in": expires_in,
|
"expires_in": expires_in,
|
||||||
"access_token_time": access_token_time,
|
"access_token_time": access_token_time,
|
||||||
}
|
}
|
||||||
else:
|
|
||||||
logger.error(f"wechat配置 appid = {appid} 获取token失败,请检查配置")
|
|
||||||
|
|
||||||
# 保存wechat token
|
|
||||||
if wechat_token_history:
|
|
||||||
self.save_data("wechat_token", wechat_token_history)
|
|
||||||
|
|
||||||
def __flush_access_token(self, index: int, force: bool = False):
|
def __flush_access_token(self, index: int, force: bool = False):
|
||||||
"""
|
"""
|
||||||
@ -288,7 +259,7 @@ class MessageForward(_PluginBase):
|
|||||||
appsecret = wechat_token['appsecret']
|
appsecret = wechat_token['appsecret']
|
||||||
|
|
||||||
# 判断token有效期
|
# 判断token有效期
|
||||||
if force or (datetime.now() - datetime.strptime(access_token_time, "%Y-%m-%d %H:%M:%S")).seconds >= expires_in:
|
if force or (datetime.now() - access_token_time).seconds >= expires_in:
|
||||||
# 重新获取token
|
# 重新获取token
|
||||||
access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid,
|
access_token, expires_in, access_token_time = self.__get_access_token(corpid=corpid,
|
||||||
appsecret=appsecret)
|
appsecret=appsecret)
|
||||||
|
Reference in New Issue
Block a user