优化获取token逻辑

dependabot/npm_and_yarn/fir_admin/tmpl-1.0.5
youngS 3 years ago
parent bab6e083bf
commit 0148905071
  1. 4
      fir_client/src/components/FirLogin.vue
  2. 4
      fir_client/src/components/user/FirUserProfileInfo.vue
  3. 1
      fir_ser/api/utils/crontab/ctasks.py
  4. 23
      fir_ser/api/utils/mp/wechat.py
  5. 7
      fir_ser/api/views/login.py

@ -181,6 +181,10 @@
this.wx_login_qr_url = data.data.qr; this.wx_login_qr_url = data.data.qr;
this.loop_flag = true; this.loop_flag = true;
this.loop_get_wx_info(data.data.ticket); this.loop_get_wx_info(data.data.ticket);
}else {
this.$message.error(data.msg);
this.wx_visible = false;
this.loop_flag = false;
} }
}, { }, {
"methods": "GET", "methods": "GET",

@ -392,6 +392,10 @@
this.wx_login_qr_url = data.data.qr; this.wx_login_qr_url = data.data.qr;
this.loop_flag = true; this.loop_flag = true;
this.loop_get_wx_info(data.data.ticket); this.loop_get_wx_info(data.data.ticket);
}else {
this.$message.error(data.msg);
this.wx_visible = false;
this.loop_flag = false;
} }
}, { }, {
"methods": "POST", "methods": "POST",

@ -5,7 +5,6 @@
# date: 2020/4/7 # date: 2020/4/7
from api.models import Apps, UserInfo, AppIOSDeveloperInfo 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.storage.storage import Storage
from api.utils.app.supersignutils import IosUtils from api.utils.app.supersignutils import IosUtils
from api.utils.utils import send_ios_developer_active_status from api.utils.utils import send_ios_developer_active_status

@ -20,7 +20,9 @@ wx_login_info = THIRDLOGINCONF.wx_official
def format_req_json(j_data, func, *args, **kwargs): def format_req_json(j_data, func, *args, **kwargs):
if j_data.get("errcode") == 40001 or 'invalid credential' in j_data.get('errmsg', ''): if j_data.get("errcode") == 40001 or 'invalid credential' in j_data.get('errmsg', ''):
logger.error(f"error j_data {j_data}") 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 func(*args, **kwargs)[1]
return j_data 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") wx_access_token_key = CACHE_KEY_TEMPLATE.get("wx_access_token_key")
access_token_info = cache.get(wx_access_token_key) access_token_info = cache.get(wx_access_token_key)
if not access_token_info or force: 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') expires_in = access_token_info.get('expires_in')
if expires_in: if expires_in:
cache.set(wx_access_token_key, access_token_info, expires_in - 60) 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, ): def get_wx_access_token_cache(c_count=1, ):
if c_count > 10: if c_count > 5:
return '' return ''
wx_access_token_key = CACHE_KEY_TEMPLATE.get("wx_access_token_key") wx_access_token_key = CACHE_KEY_TEMPLATE.get("wx_access_token_key")
access_token = cache.get(wx_access_token_key) access_token = cache.get(wx_access_token_key)
if access_token: if access_token:
return access_token.get('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) 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}' 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) req = requests.get(t_url)
if req.status_code == 200: if req.status_code == 200:
logger.info(f"get access token {req.status_code} {req.text}")
return req.json() return req.json()
logger.error(f"get access token failed {req.status_code} {req.text}") logger.error(f"get access token failed {req.status_code} {req.text}")
return req.text return req.text
@classmethod
def make_wx_auth_obj(): def make_wx_auth_obj(cls):
return WxOfficialBase(**wx_login_info.get('auth')) return cls(**wx_login_info.get('auth'))
def check_signature(params): def check_signature(params):

@ -403,12 +403,17 @@ class RegistView(APIView):
def wx_qr_code_response(ret, code, qr_info): def wx_qr_code_response(ret, code, qr_info):
if code: if code:
logger.info(f"微信登录码获取成功, {qr_info}") logger.info(f"微信登录码获取成功, {qr_info}")
errmsg = qr_info.get('errmsg')
if errmsg:
ret.code = 1003
ret.msg = "微信登录码获取失败,请稍后"
else:
ticket = qr_info.get('ticket') ticket = qr_info.get('ticket')
if ticket: if ticket:
set_wx_ticket_login_info_cache(ticket) set_wx_ticket_login_info_cache(ticket)
ret.data = {'qr': show_qrcode_url(ticket), 'ticket': ticket} ret.data = {'qr': show_qrcode_url(ticket), 'ticket': ticket}
else: else:
ret.code = code ret.code = 1003
ret.msg = "微信登录码获取失败,请稍后" ret.msg = "微信登录码获取失败,请稍后"
return Response(ret.dict) return Response(ret.dict)

Loading…
Cancel
Save