UnicomObject.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. # # -*- encoding: utf-8 -*-
  2. # """
  3. # @File : UnicomObject.py
  4. # @Time : 2022/6/17 11:03
  5. # @Author : stephen
  6. # @Email : zhangdongming@asj6.wecom.work
  7. # @Software: PyCharm
  8. # """
  9. # import base64
  10. # import json
  11. #
  12. # import requests
  13. # from Crypto.Cipher import AES
  14. # from decimal import Decimal
  15. #
  16. # from pysmx.SM3 import hash_msg as sm3_msg
  17. #
  18. # from Ansjer.config import unicomAppUrl, unicomAppId, unicomAppSecret, unicomTenantId, \
  19. # unicomEncodeKey, unicomIvKey, unicomUserName, unicomPassword, unicomPushKey
  20. # from Object.utils.SymmetricCryptoUtil import AESencrypt
  21. #
  22. # """
  23. # 联通4Gapi
  24. # 具体参数查看接口文档
  25. # https://www.showdoc.com.cn/unicomJYHapi/8158648460007467
  26. # """
  27. #
  28. #
  29. # class UnicomObjeect:
  30. # def __init__(self):
  31. # self.appUrl = unicomAppUrl
  32. # self.appId = unicomAppId
  33. # self.appSecret = unicomAppSecret
  34. # self.tenantId = unicomTenantId
  35. # self.encodeKey = unicomEncodeKey
  36. # self.ivKey = unicomIvKey
  37. # self.username = unicomUserName
  38. # self.password = unicomPassword
  39. # self.pushKey = unicomPushKey
  40. # self.headers = {'Tenant': self.tenantId, 'content-type': 'application/x-www-form-urlencoded'}
  41. #
  42. # # pip install snowland-smx
  43. # def createSign(self, reverse=False, **sign_params):
  44. # """
  45. # 调用接口(API)时需要对请求参数进行签名(sign)验证,
  46. # 算法:
  47. # 根据参数名称将你的所有请求参数按照字母先后顺序排序: key = value & key = value,对除签名外的所有请求参数按
  48. # key
  49. # 做的升序排列。
  50. # 如:将
  51. # foo = 1, bar = 2, baz = 3
  52. # 排序为
  53. # bar = 2, baz = 3, foo = 1
  54. # 参数名和参数值链接后,得到拼装字符串(注意:参数名与参数值左右两边不能包含空格)
  55. # bar = 2 & baz = 3 & foo = 1
  56. # 将
  57. # pushkey拼接到参数字符尾部进行
  58. # SM3
  59. # 加密,再转化成大写,格式是
  60. # (SM3(key1=value1 & key2=value2 &...& key= pushKey)).upcase
  61. # @param reverse:
  62. # @param sign_params:
  63. # @return:
  64. # """
  65. # dict_2 = dict(sorted(sign_params.items(), key=lambda item: item[0], reverse=reverse))
  66. # data_list = []
  67. # for item in dict_2.items():
  68. # if item[0] and item[1]:
  69. # data_list.append("{}={}".format(item[0], item[1]))
  70. # val = '&'.join(data_list)
  71. # push_key = '&key={}'.format(self.pushKey)
  72. # val = val + push_key
  73. # return sm3_msg(val).upper()
  74. #
  75. # def get_login_authorization(self):
  76. # """
  77. # 获取登录授权
  78. # 注意登录认证Authorization和登录后的请求认证不一样
  79. # 算法:appId+":"+appSecret base64转码生成 前面加Basic
  80. # @return: "Basic " + base64_data
  81. # """
  82. # voucher = self.appId + ':' + self.appSecret
  83. # base64_data = str(base64.b64encode(voucher.encode("utf-8")), "utf-8")
  84. # return "Basic " + base64_data
  85. #
  86. # def get_encode_password(self):
  87. # """
  88. # 获取对称加密
  89. # @return: encrypt_pwd
  90. # """
  91. # aes = AESencrypt(self.encodeKey.encode('utf-8'), AES.MODE_CBC, self.ivKey.encode('utf-8'),
  92. # paddingMode="ZeroPadding",
  93. # characterSet='utf-8')
  94. # encrypt_pwd = aes.encryptFromString(self.password)
  95. # return encrypt_pwd
  96. #
  97. # def generate_token(self):
  98. # """
  99. # 生成令牌
  100. # @return: token
  101. # """
  102. # url = self.appUrl + '/auc/oauth/token'
  103. # pwd = self.get_encode_password()
  104. # body = {'username': self.username, 'password': pwd, 'grant_type': 'password', 'scope': 'server'}
  105. # headers = self.headers
  106. # headers['Authorization'] = self.get_login_authorization()
  107. # response_data = requests.post(url, data=body, headers=headers)
  108. # response_data = json.loads(response_data.text)
  109. # return response_data['access_token']
  110. #
  111. # def refresh_token(self, refresh_token):
  112. # """
  113. # 刷新令牌
  114. # @param refresh_token:
  115. # @return:
  116. # """
  117. # url = self.appUrl + '/auc/oauth/token?grant_type=refresh_token'
  118. # body = {'refresh_token': refresh_token, 'grant_type': 'refresh_token'}
  119. # headers = self.headers
  120. # headers['Authorization'] = self.get_login_authorization()
  121. # response_data = requests.post(url, data=body, headers=headers)
  122. # return response_data.text
  123. #
  124. # def business_unify_headers(self):
  125. # """
  126. # 业务统一headers
  127. # 在请求头中增加key为Authorization,value为"Bearer " + token
  128. # @return: headers
  129. # """
  130. # token = self.generate_token()
  131. # headers = self.headers
  132. # headers['Authorization'] = 'Bearer ' + token
  133. # return headers
  134. #
  135. # # 业务api 注意 get与post 请求数据类型不同 post使用:application/json
  136. # def verify_device(self, **re_params):
  137. # """
  138. # 验证设备
  139. # @param re_params:
  140. # @return:
  141. # """
  142. # url = self.appUrl + '/platform/api/device/verify-device'
  143. # return requests.get(url, params=re_params, headers=self.business_unify_headers())
  144. #
  145. # def query_device_status(self, **re_params):
  146. # """
  147. # 查询设备状态
  148. # @param re_params:
  149. # @return:
  150. # """
  151. # url = self.appUrl + '/platform/api/device/device-status'
  152. # return requests.get(url, params=re_params, headers=self.business_unify_headers())
  153. #
  154. # def update_device_state(self, **re_data):
  155. # """
  156. # 修改设备状态
  157. # @param re_data:
  158. # @return:
  159. # """
  160. # url = self.appUrl + '/platform/api/device/device-state'
  161. # headers = self.business_unify_headers()
  162. # headers['content-type'] = 'application/json'
  163. # return requests.post(url, data=json.dumps(re_data), headers=headers)
  164. #
  165. # def query_device_usage_history(self, **re_params):
  166. # """
  167. # 查询设备用量历史
  168. # @param re_params:
  169. # @return:
  170. # """
  171. # url = self.appUrl + '/platform/api/usage/device-usage-history'
  172. # return requests.get(url, params=re_params, headers=self.business_unify_headers())
  173. #
  174. # def query_current_renew_list_usage_details(self, **re_params):
  175. # """
  176. # 查询设备当前队列用量详情
  177. # @param re_params:
  178. # @return:
  179. # """
  180. # url = self.appUrl + '/platform/api/usage/current-renewlist-usage-details'
  181. # return requests.get(url, params=re_params, headers=self.business_unify_headers())
  182. #
  183. # def get_device_batch_detail(self, **re_data):
  184. # """
  185. # 查询设备当前队列用量详情
  186. # @return:
  187. # """
  188. # url = self.appUrl + '/platform/api/device/batch-detail'
  189. # headers = self.business_unify_headers()
  190. # headers['content-type'] = 'application/json'
  191. # return requests.post(url, data=json.dumps(re_data), headers=headers)
  192. #
  193. # def query_package_list(self, **re_params):
  194. # """
  195. # 查询套餐列表
  196. # @return:
  197. # """
  198. # url = self.appUrl + '/platform/api/package/list'
  199. # return requests.get(url, params=re_params, headers=self.business_unify_headers())
  200. #
  201. # def query_renewal_list(self, **re_params):
  202. # """
  203. # 续费套餐列表
  204. # @param re_params:
  205. # @return:
  206. # """
  207. # url = self.appUrl + '/platform/api/package/list'
  208. # return requests.get(url, params=re_params, headers=self.business_unify_headers())
  209. #
  210. # def async_buy_package(self, **re_data):
  211. # """
  212. # 查询设备当前队列用量详情
  213. # @return:
  214. # """
  215. # url = self.appUrl + '/platform/api/package/async-buy-package'
  216. # headers = self.business_unify_headers()
  217. # headers['content-type'] = 'application/json'
  218. # return requests.post(url, data=json.dumps(re_data), headers=headers)
  219. #
  220. #
  221. # if __name__ == '__main__':
  222. # price = '12.13'
  223. # print(float(price))
  224. # discount = '6'
  225. # dd = Decimal(price) - Decimal(discount)
  226. # print(dd.quantize(Decimal('0.00')))
  227. #
  228. # # data = {'foo': 1, 'bar': 2, 'baz': 3}
  229. # # print(unicom_api.createSign(**data))
  230. # unicom_api = UnicomObjeect()
  231. # # result = unicom_api.generate_token()
  232. # # result = unicom_api.refresh_token('5d0c0f30-99bd-4f17-9614-3524495b05d4')
  233. # params = {'iccids': '89860620180077842020'}
  234. # # response = unicom_api.verify_device(**params)
  235. # # response = unicom_api.query_device_status(**params)
  236. # # response = unicom_api.update_device_state(**params)
  237. # # response = unicom_api.query_device_usage_history(**params)
  238. # # response = unicom_api.query_current_renew_list_usage_details(**params)
  239. # # unicom_api.get_device_batch_detail()
  240. # response = unicom_api.query_package_list()
  241. # # response = unicom_api.query_renewal_list(**params)
  242. #
  243. # if response.status_code == 200:
  244. # res_dict = json.loads(response.text)
  245. # print(res_dict)
  246. # response_json = {
  247. # "success": True,
  248. # "msg": "操作成功",
  249. # "code": 0,
  250. # "data": {
  251. # "iccid": "8986062018007784202",
  252. # "completeIccid": "89860620180077842020",
  253. # "status": 1
  254. # }
  255. # }
  256. # print(response_json['data']['iccid'])
  257. # print(response.status_code)