libushang 8 mesiacov pred
rodič
commit
d08480ec12
3 zmenil súbory, kde vykonal 83 pridanie a 9 odobranie
  1. 73 6
      common/YzyApi.py
  2. 8 1
      exceptions.py
  3. 2 2
      main.py

+ 73 - 6
common/YzyApi.py

@@ -6,18 +6,26 @@ import hashlib
 import time
 import base64
 import json
+from utils.redis_util import *
 import requests
+from exceptions import YzyException
 
-# YZY_HOST = "http://19.15.0.128:8080"
-YZY_HOST = "https://xtbg.digitalgd.com.cn"
+# 应用名称:茂名市智慧应急平台
+# https://open.weixin.qq.com/connect/Oauth2/authorize?appid=wld341060039&redirect_uri=http://19.155.220.206/#/&response_type=code&scope=snsapi_base&agentid=1004302&state=xxxxxx#wechat_redirect
+
+YZY_HOST = "http://19.15.0.128:8080"
+# YZY_HOST = "https://xtbg.digitalgd.com.cn"
+
+YZY_AGENTID = 1004302
 YZY_CORPID = "wld341060039"
 YZY_CORPSECRET = "5_8aOBBjioNbP7KDwjyBKmwnJ05-y1WbaJlt4irM1eA"
 
+YZY_ACCESS_TOKEN_REDIS_KEY = "YZY_ACCESS_TOKEN_REDIS_KEY"
+
 YZH_PASSID = "yzy_demo"
 YZH_PASSTOKEN = "WjKat55cv6PrJtpCHld0trrHsv1mbCqL"
-dgd_pre_release = 1
 
-def get_access_token():
+def __get_access_token():
     url = "{}/ebus/yzyapi/cgi-bin/gettoken?corpid={}&corpsecret={}".format(YZY_HOST, YZY_CORPID, YZY_CORPSECRET) 
 
     timestamp = str(int(time.time()))
@@ -29,12 +37,71 @@ def get_access_token():
         "x-tif-signature": signature,
         "x-tif-timestamp": timestamp,
         "x-tif-nonce": nonce,
-        "x-tif-paasid": YZH_PASSID,
-        "dgd-pre-release": str(dgd_pre_release)
+        "x-tif-paasid": YZH_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['access_token']
+        else:
+            raise YzyException(errcode=errcode, errmsg=result['errmsg'])
+
+def get_cache_access_token():
+    access_token = redis_get(YZY_ACCESS_TOKEN_REDIS_KEY)
+    if access_token is None:
+        access_token = __get_access_token()
+        redis_set_with_time(YZY_ACCESS_TOKEN_REDIS_KEY, access_token, 3600)
+    
+    return access_token
+
+def send_textcard_message(users, title: str, description: str, detail_url: str):
+    access_token = get_cache_access_token()
+    url = "{}/ebus/yzyapi/cgi-bin/message/send?access_token={}".format(YZY_HOST, access_token)
+    
+    data = {
+        "touser": "|".join(users),
+        "msgtype" : "textcard",
+        "agentid" : YZY_AGENTID,
+        "textcard": {
+            "title": title,
+            "description": description,
+            "url": detail_url
+        }
+    }
+
+    return __post_url__(url, data)
+
+def __post_url__(url, data):
+    print('yzy url:', url)
+    print('yzy data:', data)
+    json_str = json.dumps(data, ensure_ascii=False)
+    data = json_str.encode('utf-8')
+    
+    timestamp = str(int(time.time()))
+    nonce = ranstr(20)
+    signature = calcResponseSign(timestamp, YZH_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": YZH_PASSID
+    }
+    response = requests.post(url, data=data,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 True
+        else:
+            raise YzyException(errcode=errcode, errmsg=result['errmsg'])
+        
 
 def ranstr(num):
     salt = ''.join(random.sample(

+ 8 - 1
exceptions.py

@@ -32,4 +32,11 @@ class AlertException(Exception):
 
 class XssException(Exception):
     def __init__(self):
-        super().__init__()
+        super().__init__()
+
+
+class YzyException(Exception):
+    def __init__(self, errcode: int, errmsg: str):
+        super().__init__()
+        self.errcode = errcode
+        self.errmsg = errmsg

+ 2 - 2
main.py

@@ -72,8 +72,8 @@ async def exception_handler(request: Request, exc: AlertException):
     
 if __name__ == '__main__':
     from common import YzyApi
-    YzyApi.get_access_token()
-    
+    YzyApi.send_textcard_message(["c3o4t44nb2e1oipbs80paw"], "title", "事件进度", "https://www.baidu.com/")
+
     import uvicorn
     sys = platform.system()
     if sys == "Windows":