From 0148905071c03e50b90425787b273ed869d183b3 Mon Sep 17 00:00:00 2001 From: youngS Date: Fri, 10 Sep 2021 17:00:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=8E=B7=E5=8F=96token?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fir_client/src/components/FirLogin.vue | 4 ++++ .../components/user/FirUserProfileInfo.vue | 4 ++++ fir_ser/api/utils/crontab/ctasks.py | 1 - fir_ser/api/utils/mp/wechat.py | 23 ++++++++++++------- fir_ser/api/views/login.py | 15 ++++++++---- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/fir_client/src/components/FirLogin.vue b/fir_client/src/components/FirLogin.vue index 40858bf..e02c2a7 100644 --- a/fir_client/src/components/FirLogin.vue +++ b/fir_client/src/components/FirLogin.vue @@ -181,6 +181,10 @@ this.wx_login_qr_url = data.data.qr; this.loop_flag = true; this.loop_get_wx_info(data.data.ticket); + }else { + this.$message.error(data.msg); + this.wx_visible = false; + this.loop_flag = false; } }, { "methods": "GET", diff --git a/fir_client/src/components/user/FirUserProfileInfo.vue b/fir_client/src/components/user/FirUserProfileInfo.vue index 11fd2fb..6c4d5ed 100644 --- a/fir_client/src/components/user/FirUserProfileInfo.vue +++ b/fir_client/src/components/user/FirUserProfileInfo.vue @@ -392,6 +392,10 @@ this.wx_login_qr_url = data.data.qr; this.loop_flag = true; this.loop_get_wx_info(data.data.ticket); + }else { + this.$message.error(data.msg); + this.wx_visible = false; + this.loop_flag = false; } }, { "methods": "POST", diff --git a/fir_ser/api/utils/crontab/ctasks.py b/fir_ser/api/utils/crontab/ctasks.py index d9097f1..89e1521 100644 --- a/fir_ser/api/utils/crontab/ctasks.py +++ b/fir_ser/api/utils/crontab/ctasks.py @@ -5,7 +5,6 @@ # date: 2020/4/7 from api.models import Apps, UserInfo, AppIOSDeveloperInfo -from api.utils.mp.wechat import make_wx_auth_obj from api.utils.storage.storage import Storage from api.utils.app.supersignutils import IosUtils from api.utils.utils import send_ios_developer_active_status diff --git a/fir_ser/api/utils/mp/wechat.py b/fir_ser/api/utils/mp/wechat.py index bea1f92..7c3cc72 100644 --- a/fir_ser/api/utils/mp/wechat.py +++ b/fir_ser/api/utils/mp/wechat.py @@ -20,7 +20,9 @@ wx_login_info = THIRDLOGINCONF.wx_official def format_req_json(j_data, func, *args, **kwargs): if j_data.get("errcode") == 40001 or 'invalid credential' in j_data.get('errmsg', ''): logger.error(f"error j_data {j_data}") - sync_wx_access_token(True) + status, result = sync_wx_access_token(True) + if not status: + return result return func(*args, **kwargs)[1] return j_data @@ -29,21 +31,25 @@ def sync_wx_access_token(force=False): wx_access_token_key = CACHE_KEY_TEMPLATE.get("wx_access_token_key") access_token_info = cache.get(wx_access_token_key) if not access_token_info or force: - access_token_info = make_wx_auth_obj().get_access_token() + access_token_info = WxOfficialBase.make_wx_auth_obj().get_access_token() + if access_token_info.get('errcode') in ['40013'] or 'invalid appid' in access_token_info.get('errmsg'): + return False, access_token_info expires_in = access_token_info.get('expires_in') if expires_in: cache.set(wx_access_token_key, access_token_info, expires_in - 60) - return access_token_info + return True, access_token_info def get_wx_access_token_cache(c_count=1, ): - if c_count > 10: + if c_count > 5: return '' wx_access_token_key = CACHE_KEY_TEMPLATE.get("wx_access_token_key") access_token = cache.get(wx_access_token_key) if access_token: return access_token.get('access_token') - sync_wx_access_token(True) + status, result = sync_wx_access_token(True) + if not status: + return result return get_wx_access_token_cache(c_count + 1) @@ -128,13 +134,14 @@ class WxOfficialBase(object): t_url = f'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={self.app_id}&secret={self.app_secret}' req = requests.get(t_url) if req.status_code == 200: + logger.info(f"get access token {req.status_code} {req.text}") return req.json() logger.error(f"get access token failed {req.status_code} {req.text}") return req.text - -def make_wx_auth_obj(): - return WxOfficialBase(**wx_login_info.get('auth')) + @classmethod + def make_wx_auth_obj(cls): + return cls(**wx_login_info.get('auth')) def check_signature(params): diff --git a/fir_ser/api/views/login.py b/fir_ser/api/views/login.py index c5418bc..64339d7 100644 --- a/fir_ser/api/views/login.py +++ b/fir_ser/api/views/login.py @@ -403,12 +403,17 @@ class RegistView(APIView): def wx_qr_code_response(ret, code, qr_info): if code: logger.info(f"微信登录码获取成功, {qr_info}") - ticket = qr_info.get('ticket') - if ticket: - set_wx_ticket_login_info_cache(ticket) - ret.data = {'qr': show_qrcode_url(ticket), 'ticket': ticket} + errmsg = qr_info.get('errmsg') + if errmsg: + ret.code = 1003 + ret.msg = "微信登录码获取失败,请稍后" + else: + ticket = qr_info.get('ticket') + if ticket: + set_wx_ticket_login_info_cache(ticket) + ret.data = {'qr': show_qrcode_url(ticket), 'ticket': ticket} else: - ret.code = code + ret.code = 1003 ret.msg = "微信登录码获取失败,请稍后" return Response(ret.dict)