UnicomObject.py 9.3 KB

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