From 377b6e4571c869f11a02c1f921e05c8af350a244 Mon Sep 17 00:00:00 2001 From: newrain001 Date: Mon, 29 May 2023 23:51:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'api/wechat3.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/wechat3.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/api/wechat3.py b/api/wechat3.py index e86a191..8f7f41f 100644 --- a/api/wechat3.py +++ b/api/wechat3.py @@ -37,10 +37,26 @@ class Tencent(object): # 获取tokent def get_token(self): token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (self.url, CorpID, Secret) - r = requests.get(token_url) - r = r.json() - token = r['access_token'] - return token + if not os.path.isfile('./token.json'): + r = requests.get(token_url) + r = r.json() + token = r['access_token'] + with open('./token.json', 'w') as f: + json.dump({'timestamp': time.time(), 'token': token}, f) + return token + else: + with open('./token.json', 'r') as f: + token_data = json.load(f) + print(time.time() - int(token_data.get('timestamp'))) + if time.time() - int(token_data.get('timestamp')) > 7000: + r = requests.get(token_url) + r = r.json() + token = r['access_token'] + with open('./token.json', 'w') as f: + json.dump({'timestamp': time.time(), 'token': token}, f) + return token + else: + return token_data['token'] # 发送信息 def send_message(self):