Explorar el Código

粤政易JSSDK

libushang hace 1 semana
padre
commit
4ab4323289
Se han modificado 2 ficheros con 77 adiciones y 1 borrados
  1. 35 0
      common/YzyApi.py
  2. 42 1
      routers/prod_api/auth.py

+ 35 - 0
common/YzyApi.py

@@ -19,6 +19,7 @@ from sqlalchemy.orm import Session
 # https://open.weixin.qq.com/connect/Oauth2/authorize?appid=wld341060039&redirect_uri=https://www.baidu.com/#/&response_type=code&scope=snsapi_base&agentid=1004302&state=xxxxxx#wechat_redirect
 
 YZY_ACCESS_TOKEN_REDIS_KEY = "YZY_ACCESS_TOKEN_REDIS_KEY"
+YZY_JSAPI_TICKET_REDIS_KEY = "YZY_JSAPI_TICKET_REDIS_KEY"
 
 '''
 YZY_API_ROOT = "http://19.15.0.128:8080"
@@ -62,6 +63,40 @@ def get_cache_access_token():
     
     return access_token
 
+def __get_jsapi_ticket():
+    access_token = get_cache_access_token()
+    url = "{}/ebus/yzyapi/cgi-bin/get_jsapi_ticket?access_token={}".format(settings.YZY_API_ROOT, access_token)
+    print('yzy url:', url)
+
+    timestamp = str(int(time.time()))
+    nonce = ranstr(20)
+    signature = calcResponseSign(timestamp, settings.YZY_PASSTOKEN, nonce)
+
+    headers = {
+        'Content-Type': 'application/json;charset=UTF-8',
+        "x-tif-signature": signature,
+        "x-tif-timestamp": timestamp,
+        "x-tif-nonce": nonce,
+        "x-tif-paasid": settings.YZY_PASSID
+    }
+    response = requests.get(url, headers=headers, timeout=15)
+    print('yzy return:', response.text)
+    if response.status_code == 200 :
+        result = response.json()
+        errcode = int(result['errcode'])
+        if errcode == 0:
+            return (result['ticket'], result['expires_in'])
+        else:
+            raise YzyException(errcode=errcode, errmsg=result['errmsg'])
+
+def get_cache_jsapi_ticket():
+    ticket = redis_get(YZY_JSAPI_TICKET_REDIS_KEY)
+    if ticket is None:
+        ticket, expires_in = __get_jsapi_ticket()
+        redis_set_with_time(YZY_JSAPI_TICKET_REDIS_KEY, ticket, expires_in - 600)
+    
+    return ticket
+
 def get_user_info(code: str):
     access_token = get_cache_access_token()
     url = "{}/ebus/yzyapi/cgi-bin/user/getuserinfo?access_token={}&code={}".format(settings.YZY_API_ROOT, access_token, code)

+ 42 - 1
routers/prod_api/auth.py

@@ -25,6 +25,7 @@ from exceptions import *
 import traceback
 from common.db import db_czrz
 from common.enc import mpfun, sys_user_data
+import hashlib
 
 router = APIRouter()
 
@@ -526,4 +527,44 @@ def login_with_usbkey(
         return {
             "code": 500,
             "msg": "帐号或者密码错误"
-        }
+        }
+    
+@router.post("/yzy/wxconfig")
+def wxconfig(
+    request: Request,
+    page: str = None
+):
+    timestamp = int(time.time())
+    noncestr = YzyApi.ranstr(20)
+    jsapi_ticket = YzyApi.get_cache_jsapi_ticket()
+
+    params = {
+        'timestamp': timestamp,
+        'noncestr': noncestr,
+        'jsapi_ticket': jsapi_ticket,
+        'url': page
+    }
+
+    # 1. 按 key 的 ASCII 码(字典序)升序排序
+    sorted_params = sorted(params.items(), key=lambda x: x[0])
+
+    # 2. 拼接成 key=value 形式
+    string1_parts = []
+    for key, value in sorted_params:
+        string1_parts.append(f"{key}={value}")
+
+    # 3. 用 & 拼接所有键值对
+    string1 = "&".join(string1_parts)
+    signature = hashlib.sha1(string1.encode('utf-8')).hexdigest()
+
+    return {
+        "code": 200,
+        "msg": "操作成功",
+        "data": {
+            "appId": settings.YZY_CORPID,
+            "timestamp": params['timestamp'],
+            "nonceStr": params['noncestr'],
+            "signature": signature,
+            "jsApiList": ["getLocation", "hideOptionMenu"]
+        }
+    }