YzyApi.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import random
  4. import string
  5. import hashlib
  6. import time
  7. import base64
  8. import json
  9. from utils.redis_util import *
  10. import requests
  11. from exceptions import YzyException
  12. # 应用名称:茂名市智慧应急平台
  13. # 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
  14. YZY_HOST = "http://19.15.0.128:8080"
  15. # YZY_HOST = "https://xtbg.digitalgd.com.cn"
  16. YZY_AGENTID = 1004302
  17. YZY_CORPID = "wld341060039"
  18. YZY_CORPSECRET = "5_8aOBBjioNbP7KDwjyBKmwnJ05-y1WbaJlt4irM1eA"
  19. YZY_ACCESS_TOKEN_REDIS_KEY = "YZY_ACCESS_TOKEN_REDIS_KEY"
  20. YZH_PASSID = "yzy_demo"
  21. YZH_PASSTOKEN = "WjKat55cv6PrJtpCHld0trrHsv1mbCqL"
  22. def __get_access_token():
  23. url = "{}/ebus/yzyapi/cgi-bin/gettoken?corpid={}&corpsecret={}".format(YZY_HOST, YZY_CORPID, YZY_CORPSECRET)
  24. timestamp = str(int(time.time()))
  25. nonce = ranstr(20)
  26. signature = calcResponseSign(timestamp, YZH_PASSTOKEN, nonce)
  27. headers = {
  28. 'Content-Type': 'application/json;charset=UTF-8',
  29. "x-tif-signature": signature,
  30. "x-tif-timestamp": timestamp,
  31. "x-tif-nonce": nonce,
  32. "x-tif-paasid": YZH_PASSID
  33. }
  34. response = requests.get(url, headers=headers, timeout=15)
  35. print('yzy return:', response.text)
  36. if response.status_code == 200 :
  37. result = response.json()
  38. errcode = int(result['errcode'])
  39. if errcode == 0:
  40. return result['access_token']
  41. else:
  42. raise YzyException(errcode=errcode, errmsg=result['errmsg'])
  43. def get_cache_access_token():
  44. access_token = redis_get(YZY_ACCESS_TOKEN_REDIS_KEY)
  45. if access_token is None:
  46. access_token = __get_access_token()
  47. redis_set_with_time(YZY_ACCESS_TOKEN_REDIS_KEY, access_token, 3600)
  48. return access_token
  49. def send_textcard_message(users, title: str, description: str, detail_url: str):
  50. access_token = get_cache_access_token()
  51. url = "{}/ebus/yzyapi/cgi-bin/message/send?access_token={}".format(YZY_HOST, access_token)
  52. data = {
  53. "touser": "|".join(users),
  54. "msgtype" : "textcard",
  55. "agentid" : YZY_AGENTID,
  56. "textcard": {
  57. "title": title,
  58. "description": description,
  59. "url": detail_url
  60. }
  61. }
  62. return __post_url__(url, data)
  63. def __post_url__(url, data):
  64. print('yzy url:', url)
  65. print('yzy data:', data)
  66. json_str = json.dumps(data, ensure_ascii=False)
  67. data = json_str.encode('utf-8')
  68. timestamp = str(int(time.time()))
  69. nonce = ranstr(20)
  70. signature = calcResponseSign(timestamp, YZH_PASSTOKEN, nonce)
  71. headers = {
  72. 'Content-Type': 'application/json;charset=UTF-8',
  73. "x-tif-signature": signature,
  74. "x-tif-timestamp": timestamp,
  75. "x-tif-nonce": nonce,
  76. "x-tif-paasid": YZH_PASSID
  77. }
  78. response = requests.post(url, data=data,headers=headers, timeout=15)
  79. print('yzy return:', response.text)
  80. if response.status_code == 200 :
  81. result = response.json()
  82. errcode = int(result['errcode'])
  83. if errcode == 0:
  84. return True
  85. else:
  86. raise YzyException(errcode=errcode, errmsg=result['errmsg'])
  87. def ranstr(num):
  88. salt = ''.join(random.sample(
  89. string.ascii_letters + string.digits, num))
  90. return salt
  91. #
  92. #
  93. # 生成校验码
  94. #
  95. #
  96. def authentication(timestamp, token, nonce, uid, uinfo, ext, signature):
  97. sign_data_sha256 = calcRequestSign(
  98. timestamp, token, nonce, uid, uinfo, ext)
  99. return sign_data_sha256 == signature.upper()
  100. #
  101. #
  102. # 计算校验码
  103. #
  104. #
  105. def calcResponseSign(timestamp, token, nonce):
  106. sign_data = "{}{}{}{}".format(timestamp, token, nonce, timestamp)
  107. return hashlib.sha256(
  108. sign_data.encode("utf8")
  109. ).hexdigest().upper()
  110. #
  111. #
  112. # 计算校验码
  113. #
  114. #
  115. def calcRequestSign(timestamp, token, nonce, uid, uinfo, ext):
  116. sign_data = "{}{}{},{},".format(
  117. timestamp, token, nonce, uid)
  118. if len(uinfo) == 0:
  119. sign_data = sign_data + ","
  120. else:
  121. sign_data = sign_data + uinfo + ","
  122. if len(ext) == 0:
  123. sign_data = sign_data
  124. else:
  125. sign_data = sign_data + ext
  126. sign_data = sign_data + timestamp
  127. return hashlib.sha256(
  128. sign_data.encode("utf8")
  129. ).hexdigest().upper()