优化获取微信token代码

dependabot/npm_and_yarn/fir_admin/tmpl-1.0.5
youngS 3 years ago
parent 9b8412304f
commit dc8de286c5
  1. 3
      fir_ser/api/tasks.py
  2. 11
      fir_ser/api/utils/crontab/ctasks.py
  3. 39
      fir_ser/api/utils/mp/wechat.py
  4. 9
      fir_ser/api/utils/storage/caches.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

@ -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

@ -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

@ -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 = {}

Loading…
Cancel
Save