xgj.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # coding=utf-8
  2. import json
  3. import requests
  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.base import Formater
  9. from django.db import transaction
  10. from rest_framework.views import APIView
  11. from rest_framework.decorators import action
  12. from rest_framework import generics
  13. from utils import response_ok
  14. from apps.account import tenant_log
  15. from apps.foundation.models import BizLog, Config
  16. from rest_framework.exceptions import NotFound
  17. def gender_sign(gateway_key):
  18. ts = timezone.now().strftime('%Y%m%d%H%M%S')
  19. token = gateway_key + ts
  20. m = md5()
  21. m.update(token.encode("utf8"))
  22. sign = m.hexdigest()
  23. return ts, sign
  24. class XGJ():
  25. @staticmethod
  26. def vehicle_inquiry(instance): #询价单
  27. xgj_ip = Config.getConfigValue(Config.KEY_XGJ_IP)
  28. xgj_session_key = Config.getConfigValue(Config.KEY_XGJ_SESSION_KEY)
  29. if xgj_ip and xgj_session_key:
  30. ts, sign = gender_sign(xgj_session_key)
  31. url = xgj_ip + 'api/wechat_applet/vehicle_floor_add/?ts=' + ts + '&sign=' + sign
  32. param = {
  33. 'company': instance.shop.xgj_id,
  34. 'tel': instance.tel,
  35. 'name': instance.name,
  36. 'model': instance.model.series.brand.name + '-' + instance.model.series.name + '-' + instance.model.name,
  37. 'guide_price': Formater.formatPriceShow(instance.model.price),
  38. 'shop_price': Formater.formatPriceShow(instance.model.sale_price),
  39. 'notes': instance.notes
  40. }
  41. result = requests.post(url=url, data=json.dumps(param))
  42. result = result.json()
  43. if not result['success']:
  44. if 'errors' in result:
  45. tenant_log(instance.customer.user, BizLog.INSERT, u'新车询价同步销管佳失败[%s],id=%d' % (str(result['errors']), instance.id))
  46. else:
  47. tenant_log(instance.customer.user, BizLog.INSERT, u'新车询价同步销管佳失败,id=%d' % (instance.id))
  48. else:
  49. tenant_log(instance.customer.user, BizLog.INSERT, u'新车询价同步销管佳成功,id=%d' % (instance.id))
  50. else:
  51. tenant_log(instance.customer.user, BizLog.INSERT, u'新车询价同步销管佳失败,没有设置销管佳地址或密钥,id=%d' % (instance.id))
  52. @staticmethod
  53. def drive_reserve(instance): #试驾单
  54. xgj_ip = Config.getConfigValue(Config.KEY_XGJ_IP)
  55. xgj_session_key = Config.getConfigValue(Config.KEY_XGJ_SESSION_KEY)
  56. if xgj_ip and xgj_session_key:
  57. ts, sign = gender_sign(xgj_session_key)
  58. url = xgj_ip + 'api/wechat_applet/vehicle_drive_add/?ts=' + ts + '&sign=' + sign
  59. param = {
  60. 'company': instance.shop.xgj_id,
  61. 'tel': instance.tel,
  62. 'name': instance.name,
  63. 'model': instance.model.series.brand.name + '-' + instance.model.series.name + '-' + instance.model.name,
  64. 'date': instance.date,
  65. 'notes': instance.notes
  66. }
  67. result = requests.post(url=url, data=json.dumps(param))
  68. result = result.json()
  69. if not result['success']:
  70. if 'errors' in result:
  71. tenant_log(instance.customer.user, BizLog.INSERT, u'试驾预约同步销管佳失败[%s],id=%d' % (str(result['errors']), instance.id))
  72. else:
  73. tenant_log(instance.customer.user, BizLog.INSERT, u'试驾预约同步销管佳失败,id=%d' % (instance.id))
  74. else:
  75. tenant_log(instance.customer.user, BizLog.INSERT, u'试驾预约同步销管佳成功,id=%d' % (instance.id))
  76. else:
  77. tenant_log(instance.customer.user, BizLog.INSERT, u'试驾预约同步销管佳失败,没有设置销管佳地址或密钥,id=%d' % (instance.id))
  78. @staticmethod
  79. def vehicle_estimate(instance): #置换咨询
  80. xgj_ip = Config.getConfigValue(Config.KEY_XGJ_IP)
  81. xgj_session_key = Config.getConfigValue(Config.KEY_XGJ_SESSION_KEY)
  82. if xgj_ip and xgj_session_key:
  83. ts, sign = gender_sign(xgj_session_key)
  84. url = xgj_ip + 'api/wechat_applet/vehicle_exchange_add/?ts=' + ts + '&sign=' + sign
  85. param = {
  86. 'company': instance.shop.xgj_id,
  87. 'tel': instance.customer.tel,
  88. 'name': instance.customer.name,
  89. 'model': instance.name + '-' + instance.model,
  90. 'plate_date': instance.plate_date,
  91. 'mileage': instance.mileage,
  92. 'notes': instance.notes
  93. }
  94. result = requests.post(url=url, data=json.dumps(param))
  95. result = result.json()
  96. if not result['success']:
  97. if 'errors' in result:
  98. tenant_log(instance.customer.user, BizLog.INSERT, u'置换咨询同步销管佳失败[%s],id=%d' % (str(result['errors']), instance.id))
  99. else:
  100. tenant_log(instance.customer.user, BizLog.INSERT, u'置换咨询同步销管佳失败,id=%d' % (instance.id))
  101. else:
  102. tenant_log(instance.customer.user, BizLog.INSERT, u'置换咨询同步销管佳成功,id=%d' % (instance.id))
  103. else:
  104. tenant_log(instance.customer.user, BizLog.INSERT, u'置换咨询同步销管佳失败,没有设置销管佳地址或密钥,id=%d' % (instance.id))
  105. @staticmethod
  106. def maint_reserve(instance): # 售后预约
  107. xgj_ip = Config.getConfigValue(Config.KEY_XGJ_IP)
  108. xgj_session_key = Config.getConfigValue(Config.KEY_XGJ_SESSION_KEY)
  109. if xgj_ip and xgj_session_key:
  110. ts, sign = gender_sign(xgj_session_key)
  111. url = xgj_ip + 'api/wechat_applet/maint_reserve_add/?ts=' + ts + '&sign=' + sign
  112. param = {
  113. 'company': instance.shop.xgj_id,
  114. 'tel': instance.tel,
  115. 'name': instance.name,
  116. 'model': instance.vehicle.name,
  117. 'number': instance.vehicle.number,
  118. 'vin': instance.vehicle.vin,
  119. 'date': instance.date,
  120. 'notes': instance.notes
  121. }
  122. result = requests.post(url=url, data=json.dumps(param))
  123. result = result.json()
  124. if not result['success']:
  125. if 'errors' in result:
  126. tenant_log(instance.customer.user, BizLog.INSERT,
  127. u'售后预约同步销管佳失败[%s],id=%d' % (str(result['errors']), instance.id))
  128. else:
  129. tenant_log(instance.customer.user, BizLog.INSERT, u'售后预约同步销管佳失败,id=%d' % (instance.id))
  130. else:
  131. tenant_log(instance.customer.user, BizLog.INSERT, u'售后预约同步销管佳成功,id=%d' % (instance.id))
  132. else:
  133. tenant_log(instance.customer.user, BizLog.INSERT, u'售后预约同步销管佳失败,没有设置销管佳地址或密钥,id=%d' % (instance.id))