year_limit_query.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # 年度额度查询
  5. # author: Scott Chen
  6. # date: 2019-12-13
  7. import json
  8. import time
  9. import requests
  10. from apps.limit.biz.signature import Signature
  11. with open('apps/limit/biz/config.json', 'r', encoding='utf-8') as json_file:
  12. config_json = json.load(json_file)
  13. class YearLimitQuery:
  14. def __init__(self, name, id_no):
  15. self.__name = name
  16. self.__id_no = id_no
  17. self.__secret_key = ""
  18. def __build_merch_info(self):
  19. merch_param = {}
  20. merch_param["merchId"] = config_json["merchId"]
  21. merch_param["merchName"] = config_json["merchName"]
  22. merch_param["thirdPartyMerchCode"] = config_json["thirdPartyMerchCode"]
  23. merch_param["thirdPartyMerchName"] = config_json["thirdPartyMerchName"]
  24. merch_param["name"] = self.__name
  25. merch_param["idCard"] = self.__id_no
  26. merch_param["isCheckIdCard"] = config_json["isCheckIdCard"]
  27. self.__secret_key = config_json["secret_key"]
  28. print("原始参数集:{}".format(merch_param))
  29. return merch_param
  30. def __build_req_params(self):
  31. merch_param = self.__build_merch_info()
  32. req_params = {
  33. "merchId": merch_param["merchId"],
  34. "data": str(merch_param),
  35. "timestamp": str(int(time.time()))
  36. }
  37. return req_params
  38. def __sign(self):
  39. req_params = self.__build_req_params()
  40. instance = Signature(req_params, self.__secret_key)
  41. ret_sign = instance.sign_with_md5()
  42. return ret_sign
  43. # 额度查询请求并返回结果
  44. def __req(self, req_params):
  45. result = {}
  46. try:
  47. req_url = config_json["year_limit_url"]
  48. # GET方法,必须是params=str(dict or list or tuple or bytes)
  49. # POST方法,必须是data=dict or list or tuple or bytes or file
  50. response = requests.request("POST", req_url, data=req_params, timeout=20)
  51. if response.text:
  52. result = json.loads(response.text)
  53. print("API返回数据:{}".format(result))
  54. result = self.__parse_response(result)
  55. # 返回额度查询结果
  56. return result
  57. else:
  58. print("response.text is empty")
  59. result['error'] = '[20000]请求查询API失败'
  60. return result
  61. except Exception as e:
  62. print(e)
  63. result['error'] = '[20001]请求查询API异常'
  64. return result
  65. # 额度查询数据处理
  66. def __parse_response(self, data):
  67. result = {}
  68. # 处理返回数据
  69. if data["code"] == '0':
  70. limit_data = data["data"]["rows"][0]
  71. if limit_data["success"] == "true":
  72. result["totalAmount"] = limit_data["result"]["totalAmount"]
  73. result["innerbalance"] = limit_data["result"]["innerbalance"]
  74. print("【{},{}】查询额度为:{}".format(self.__name, self.__id_no, data))
  75. else:
  76. print("海关返回【{},{}】额度数据不成功".format(self.__name, self.__id_no))
  77. result['error'] = '[20002]查询额度不成功'
  78. else:
  79. print("【{},{}】额度查询失败:{}".format(self.__name, self.__id_no, data["msg"]))
  80. result['error'] = '[20003]额度查询失败'
  81. return result
  82. def query(self):
  83. ret = self.__sign()
  84. return self.__req(ret)
  85. # 单元测试
  86. if __name__ == '__main__':
  87. yq = YearLimitQuery("张三", "xxxxx")
  88. yq.query()