优化获取微信token代码

dependabot/npm_and_yarn/fir_admin/tmpl-1.0.5
youngS 3 years ago
parent dc8de286c5
commit 19709c7da3
  1. 13
      fir_ser/api/utils/mp/wechat.py

@ -16,11 +16,12 @@ from api.utils.mp.utils import WxMsgCryptBase
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
wx_login_info = THIRDLOGINCONF.wx_official 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',''): 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}") logger.error(f"error j_data {j_data}")
sync_wx_access_token(True) sync_wx_access_token(True)
return func(*args,**kwargs)[1] return func(*args, **kwargs)[1]
return j_data return j_data
@ -35,7 +36,7 @@ def sync_wx_access_token(force=False):
return access_token_info return 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 > 10:
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")
@ -101,7 +102,7 @@ def make_wx_login_qrcode(scene_str='web.login', expire_seconds=600):
"action_info": {"scene": {"scene_str": scene_str}}} "action_info": {"scene": {"scene_str": scene_str}}}
req = requests.post(t_url, json=data) req = requests.post(t_url, json=data)
if req.status_code == 200: if req.status_code == 200:
return True, format_req_json(req.json(),make_wx_login_qrcode,scene_str,expire_seconds) return True, format_req_json(req.json(), make_wx_login_qrcode, scene_str, expire_seconds)
logger.error(f"make wx login qrcode failed {req.status_code} {req.text}") logger.error(f"make wx login qrcode failed {req.status_code} {req.text}")
return False, req.text return False, req.text
@ -110,7 +111,7 @@ def get_userinfo_from_openid(open_id):
t_url = f'https://api.weixin.qq.com/cgi-bin/user/info?access_token={get_wx_access_token_cache()}&openid={open_id}&lang=zh_CN' t_url = f'https://api.weixin.qq.com/cgi-bin/user/info?access_token={get_wx_access_token_cache()}&openid={open_id}&lang=zh_CN'
req = requests.get(t_url) req = requests.get(t_url)
if req.status_code == 200: if req.status_code == 200:
return True, format_req_json(req.json(),get_userinfo_from_openid,open_id) return True, format_req_json(req.json(), get_userinfo_from_openid, open_id)
logger.error(f"get userinfo from openid failed {req.status_code} {req.text}") logger.error(f"get userinfo from openid failed {req.status_code} {req.text}")
return False, req.text return False, req.text

Loading…
Cancel
Save