xgj.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # coding=utf-8
  2. import requests
  3. import json
  4. from django.utils import timezone
  5. from django.conf import settings
  6. from hashlib import md5
  7. from utils.exceptions import CustomError
  8. from apps.WechatApplet.models import WechatApplet
  9. def gender_sign(gateway_key):
  10. ts = timezone.now().strftime('%Y%m%d%H%M%S')
  11. token = gateway_key + ts
  12. m = md5()
  13. m.update(token.encode("utf8"))
  14. sign = m.hexdigest()
  15. return ts, sign
  16. def get_xgj_params(appid):
  17. applet = WechatApplet.getByAppid(appid)
  18. tenant = applet.tenant
  19. if not tenant.xgj_address:
  20. raise CustomError(u'小程序设置错误!')
  21. if not tenant.xgj_gateway_key:
  22. raise CustomError(u'小程序设置错误!')
  23. ts, sign = gender_sign(tenant.xgj_gateway_key)
  24. return tenant.xgj_address, ts, sign
  25. class XGJ(object):
  26. @staticmethod
  27. def get_company(appid):
  28. xgj_addr, ts, sign = get_xgj_params(appid)
  29. url = xgj_addr + 'wechat/applet/member/company/?ts=' + ts + '&sign=' + sign
  30. param = {}
  31. result = requests.post(url=url, data=json.dumps(param))
  32. result = result.json()
  33. if not result['success']:
  34. if 'errors' in result:
  35. raise CustomError(u'请求失败--' + str(result['errors']))
  36. else:
  37. raise CustomError(u'请求失败')
  38. return result
  39. @staticmethod
  40. def get_member_card(appid, company_id, tel, number):
  41. xgj_addr, ts, sign = get_xgj_params(appid)
  42. url = xgj_addr + 'wechat/applet/member/select/?ts=' + ts + '&sign=' + sign
  43. param = {
  44. 'company_id': company_id,
  45. 'tel': tel,
  46. 'number': number
  47. }
  48. result = requests.post(url=url, data=json.dumps(param))
  49. result = result.json()
  50. if not result['success']:
  51. if 'errors' in result:
  52. raise CustomError(u'请求失败--' + str(result['errors']))
  53. else:
  54. raise CustomError(u'请求失败')
  55. return result
  56. @staticmethod
  57. def get_member_info(appid, member_id):
  58. xgj_addr, ts, sign = get_xgj_params(appid)
  59. url = xgj_addr + 'wechat/applet/member/seacrh/?ts=' + ts + '&sign=' + sign + '&id=' + member_id
  60. result = requests.get(url)
  61. result = result.json()
  62. if not result['success']:
  63. if 'errors' in result:
  64. raise CustomError(u'请求失败--' + str(result['errors']))
  65. else:
  66. raise CustomError(u'请求失败')
  67. return result
  68. @staticmethod
  69. def get_member_points(appid, member_id, pageNum, pageSize):
  70. xgj_addr, ts, sign = get_xgj_params(appid)
  71. url = xgj_addr + 'wechat/applet/member/points/?ts=' + ts + '&sign=' + sign + '&id=' + member_id + '&page=' + pageNum + '&rows=' + pageSize
  72. result = requests.get(url)
  73. result = result.json()
  74. if not result['success']:
  75. if 'errors' in result:
  76. raise CustomError(u'请求失败--' + str(result['errors']))
  77. else:
  78. raise CustomError(u'请求失败')
  79. return result
  80. @staticmethod
  81. def get_member_record(appid, member_id, pageNum, pageSize):
  82. xgj_addr, ts, sign = get_xgj_params(appid)
  83. url = xgj_addr + 'wechat/applet/member/consume/?ts=' + ts + '&sign=' + sign + '&id=' + member_id + '&page=' + pageNum + '&rows=' + pageSize
  84. result = requests.get(url)
  85. result = result.json()
  86. if not result['success']:
  87. if 'errors' in result:
  88. raise CustomError(u'请求失败--' + str(result['errors']))
  89. else:
  90. raise CustomError(u'请求失败')
  91. return result
  92. @staticmethod
  93. def get_member_balance(appid, member_id):
  94. xgj_addr, ts, sign = get_xgj_params(appid)
  95. url = xgj_addr + 'wechat/applet/member/balance/?ts=' + ts + '&sign=' + sign + '&member_id=' + member_id
  96. result = requests.get(url)
  97. result = result.json()
  98. if not result['success']:
  99. if 'errors' in result:
  100. raise CustomError(u'请求失败--' + str(result['errors']))
  101. else:
  102. raise CustomError(u'请求失败')
  103. return result
  104. @staticmethod
  105. def refresh_member_card(tenant, member_id, amount, points, discount_amount):
  106. if not tenant.xgj_gateway_key:
  107. raise CustomError(u'小程序设置错误')
  108. if not tenant.xgj_address:
  109. raise CustomError(u'小程序设置错误')
  110. ts, sign = gender_sign(tenant.xgj_gateway_key)
  111. url = tenant.xgj_address + 'wechat/applet/member/use/?ts=' + ts + '&sign=' + sign + '&member_id=' + member_id
  112. param = {
  113. 'amount': amount,
  114. 'points': points,
  115. 'discount_amount': discount_amount
  116. }
  117. result = requests.post(url=url, data=json.dumps(param))
  118. result = result.json()
  119. res = ''
  120. if not result['success']:
  121. if 'errors' in result:
  122. res = u'请求失败--' + str(result['errors'])
  123. else:
  124. res = u'请求失败'
  125. return res