Browse Source

no message

libushang 1 month ago
parent
commit
76e446de07
2 changed files with 104 additions and 11 deletions
  1. 58 0
      common/YzyApi.py
  2. 46 11
      routers/api/qrcode/__init__.py

+ 58 - 0
common/YzyApi.py

@@ -67,6 +67,33 @@ def get_user_info(code: str):
     url = "{}/ebus/yzyapi/cgi-bin/user/getuserinfo?access_token={}&code={}".format(settings.YZY_API_ROOT, access_token, code)
     return __post_url__(url, {})
 
+def generate_deskey(secret):
+    # 使用SHA256哈希然后取前8字节
+    return hashlib.sha256(secret.encode()).digest()[:8]
+
+def generate_signature(appid, secret, curtime):
+    # 拼接字符串
+    sign_str = appid + secret + str(curtime)
+    # 计算SHA256哈希值
+    sha256_hash = hashlib.sha256(sign_str.encode('utf-8')).hexdigest()
+    # 转换为大写
+    signature = sha256_hash.upper()
+    return signature
+
+# 2.开通用户工作台可见及第三方应用获取用户基本信息接口说明V1.3.pdf
+def getuserbycode(code: str):
+    url = f"{settings.YZY_API_ROOT}/ebus/applicationsyn/getuserbycode"
+    
+    curtime = get_datetime_str(datetime.now())
+    signature = generate_signature(settings.YZY_AGENTID, settings.YZY_CORPSECRET, curtime)
+    data = {
+        "appid": settings.YZY_AGENTID,
+        "curtime": curtime,
+        "code": code,
+        "signature": signature
+    }
+    return __post_url__(url, data)
+
 def send_text_message(users, content: str):
     access_token = get_cache_access_token()
     url = "{}/ebus/yzyapi/cgi-bin/message/send?access_token={}".format(settings.YZY_API_ROOT, access_token)
@@ -194,3 +221,34 @@ def add_to_msg_queue(db: Session, data: dict) -> None:
     new_msg = YzyMsgQueue(**data, sent_status = 0, create_time = datetime.now())
     db.add(new_msg)
     db.commit()
+
+# 辅助类
+# 调用JAVA编写的密评接口
+
+def desDecryptValue(appSecret: str, value: str) -> any:
+    data = {}
+    data['appSecret'] = appSecret
+    data['value'] = value
+    headers = {'Content-Type': 'application/json;charset=UTF-8'}
+
+    response = requests.post(url="http://127.0.0.1:8052/yzy" + "/DecryptValue", headers=headers, json=data, timeout=60)
+    if response.status_code == 200:
+        result = response.json()
+        print(result)
+        if result['errcode'] == 0:
+            return result['data']
+    return None
+
+def desEncryptValue(appSecret: str, value: str) -> any:
+    data = {}
+    data['appSecret'] = appSecret
+    data['value'] = value
+    headers = {'Content-Type': 'application/json;charset=UTF-8'}
+
+    response = requests.post(url="http://127.0.0.1:8052/yzy" + "/EncryptValue", headers=headers, json=data, timeout=60)
+    if response.status_code == 200:
+        result = response.json()
+        print(result)
+        if result['errcode'] == 0:
+            return result['data']
+    return None

+ 46 - 11
routers/api/qrcode/__init__.py

@@ -71,8 +71,12 @@ async def get_qrcode2(
         
         redirect_uri = quote(f"{settings.YZY_WEB_ROOT}/api/qrcode/event/callback?event_id={event_id}")
         state = "signin"
-        detail_url = f"https://open.weixin.qq.com/connect/Oauth2/authorize?appid=wl2bee594e73&redirect_uri={redirect_uri}&response_type=code&scope=snsapi_base&agentid=1004000&state={state}#wechat_redirect"
         
+        # detail_url = f"https://open.weixin.qq.com/connect/Oauth2/authorize?appid=wl2bee594e73&redirect_uri={redirect_uri}&response_type=code&scope=snsapi_base&agentid=1004000&state={state}#wechat_redirect"
+        
+        # 粤政易用户授权页面
+        detail_url = f"https://xtbg.gdzwfw.gov.cn/zwwxgzt/pf/userpermit/index.html?redirect_uri={redirect_uri}&response_type=code&appid={settings.YZY_AGENTID}&state={state}"
+
         # redirect_url = "/signPage?event_id={}".format(event_id)
         # detail_url = YzyApi.format_redirect_url(redirect_url)
     else:
@@ -90,17 +94,18 @@ async def get_qrcode2(
     code: str,
     db: Session = Depends(get_db)
 ):    
-    resp = YzyApi.get_user_info(code)
-    if resp['errcode'] != 0:
-        return {
-            "code": 500,
-            "msg": "Code异常"
-        }
+    # 获取用户的userId
+    # resp = YzyApi.get_user_info(code)
+    #if resp['errcode'] != 0:
+    #    return {
+    #        "code": 500,
+    #        "msg": "Code异常"
+    #    }
+    #user_id = resp['UserId']
 
     uuid_str = new_guid()
-    user_id = resp['UserId']
+    # 默认空值
     redis_val = {
-        "user_id": user_id,
         "event_id": event_id,
         "nick_name": '',
         "dept_name": '',
@@ -108,6 +113,36 @@ async def get_qrcode2(
         "duties": '',
         "sign_time": ''
     }
+
+    # 管理中心通过授权码获取用户信息接口
+    # 获取用户基本信息
+    result = YzyApi.getuserbycode(code)
+    errcode = int(result['errcode'])
+    if errcode == 0:
+        data = result['data']
+        depts = data['depts']
+        dept_name = ''
+        if len(depts) > 0:
+
+            phone = ''    
+            try:
+                # 敏感数据加密算法(DES 对称加密)
+                phone = YzyApi.desDecryptValue(settings.YZY_CORPSECRET, data['mobile'])
+            except:
+                traceback.print_exc()
+
+            dept_name = depts[0]['deptname']
+            duties = depts[0]['position']
+
+        redis_val = {
+            "event_id": event_id,
+            "user_id": data['userid'],
+            "nick_name": data['username'],
+            "dept_name": dept_name,
+            "phone": phone,
+            "duties": duties,
+            "sign_time": ''
+        }
     redis_set_json(f"yzy_user_{uuid_str}", redis_val, 60)
     redirect_url = f"/yjxp/#/signPage?event_id={event_id}&uuid={uuid_str}" # 业务页面
     detail_url = f"{settings.YZY_WEB_ROOT}{redirect_url}"
@@ -156,7 +191,7 @@ async def check(
         # 签名
         row = db.query(EventCheckin).filter(and_(EventCheckin.event_id == event_id, EventCheckin.phone == phone)).first()
         if row is None:
-            event_checkin = EventCheckin(
+            row = EventCheckin(
                 event_id = event_id,
                 user_id = 0,
                 user_name = '',
@@ -169,7 +204,7 @@ async def check(
                 phone = phone,
                 del_flag = '0'
             )
-            db.add(event_checkin)
+            db.add(row)
             db.commit()
             db.refresh(row)
         else: