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