From dc8de286c5614fd29a4c01d8afd69ff92ea003d7 Mon Sep 17 00:00:00 2001 From: youngS Date: Tue, 7 Sep 2021 20:27:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=8E=B7=E5=8F=96=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1token=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fir_ser/api/tasks.py | 3 ++- fir_ser/api/utils/crontab/ctasks.py | 11 -------- fir_ser/api/utils/mp/wechat.py | 39 ++++++++++++++++++++++++++--- fir_ser/api/utils/storage/caches.py | 9 +------ 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/fir_ser/api/tasks.py b/fir_ser/api/tasks.py index d94fcf4..f3c80d9 100644 --- a/fir_ser/api/tasks.py +++ b/fir_ser/api/tasks.py @@ -7,12 +7,13 @@ from celery import shared_task from django.core.cache import cache +from api.utils.mp.wechat import sync_wx_access_token from api.utils.storage.storage import get_local_storage from api.models import Apps from api.utils.app.supersignutils import IosUtils, resign_by_app_id from api.utils.crontab.ctasks import sync_download_times, auto_clean_upload_tmp_file, auto_delete_ios_mobile_tmp_file, \ - auto_check_ios_developer_active, sync_wx_access_token + auto_check_ios_developer_active from api.utils.geetest.geetest_utils import check_bypass_status from fir_ser.celery import app diff --git a/fir_ser/api/utils/crontab/ctasks.py b/fir_ser/api/utils/crontab/ctasks.py index 6eec392..3388bb2 100644 --- a/fir_ser/api/utils/crontab/ctasks.py +++ b/fir_ser/api/utils/crontab/ctasks.py @@ -83,14 +83,3 @@ def auto_check_ios_developer_active(): logger.error(msg) send_ios_developer_active_status(userinfo, MSGTEMPLATE.get('AUTO_CHECK_DEVELOPER') % ( userinfo.first_name, ios_developer.name)) - - -def sync_wx_access_token(): - 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: - access_token_info = make_wx_auth_obj().get_access_token() - 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 diff --git a/fir_ser/api/utils/mp/wechat.py b/fir_ser/api/utils/mp/wechat.py index dc5d98f..4c461d1 100644 --- a/fir_ser/api/utils/mp/wechat.py +++ b/fir_ser/api/utils/mp/wechat.py @@ -7,13 +7,44 @@ from hashlib import sha1 import requests import logging import json -from fir_ser.settings import THIRDLOGINCONF + +from django.core.cache import cache + +from fir_ser.settings import THIRDLOGINCONF, CACHE_KEY_TEMPLATE from api.utils.mp.utils import WxMsgCryptBase -from api.utils.storage.caches import get_wx_access_token_cache logger = logging.getLogger(__name__) 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) + return func(*args,**kwargs)[1] + return j_data + + +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() + 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 + + +def get_wx_access_token_cache(c_count=1,): + if c_count > 10: + 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) + return get_wx_access_token_cache(c_count + 1) + def create_menu(): menu_json = { @@ -70,7 +101,7 @@ def make_wx_login_qrcode(scene_str='web.login', expire_seconds=600): "action_info": {"scene": {"scene_str": scene_str}}} req = requests.post(t_url, json=data) if req.status_code == 200: - return True, req.json() + 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}") return False, req.text @@ -79,7 +110,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' req = requests.get(t_url) if req.status_code == 200: - return True, req.json() + 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}") return False, req.text diff --git a/fir_ser/api/utils/storage/caches.py b/fir_ser/api/utils/storage/caches.py index 0a08ba6..302b801 100644 --- a/fir_ser/api/utils/storage/caches.py +++ b/fir_ser/api/utils/storage/caches.py @@ -9,6 +9,7 @@ from api.models import Apps, UserInfo, AppReleaseInfo, AppUDID, APPToDeveloper, UserCertificationInfo, Order import time, os from django.utils import timezone + from fir_ser.settings import CACHE_KEY_TEMPLATE, SERVER_DOMAIN, SYNC_CACHE_TO_DATABASE, DEFAULT_MOBILEPROVISION, \ USER_FREE_DOWNLOAD_TIMES, AUTH_USER_FREE_DOWNLOAD_TIMES from api.utils.storage.storage import Storage, LocalStorage @@ -462,14 +463,6 @@ def check_app_permission(app_obj, res): return res -def get_wx_access_token_cache(): - 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') - return '' - - def set_wx_ticket_login_info_cache(ticket, data=None, expire_seconds=600): if data is None: data = {}