Browse Source

api接口

lyh 1 year ago
parent
commit
0dcefa3ae6

+ 1 - 0
apps/account/models.py

@@ -60,6 +60,7 @@ class UserManager(BaseUserManager):
         user = self.model(username=username, **extra_fields)
         user.set_password(password)
         user.type = type
+        user.name = username
         user.save(using=self._db)
         return user
 

+ 10 - 0
apps/customer/models.py

@@ -147,5 +147,15 @@ class CustomerAddress(models.Model):
         verbose_name = u'客户地址'
         ordering = ['-id']
         default_permissions = ()
+
+    def destory(self, queryset):
+        if self.default:
+            self.default = False
+            default = queryset.first()
+            if default:
+                default.default = True
+                default.save()
+        self.delete = True
+        self.save()
     
     

+ 2 - 0
apps/wechat/customer/urls.py

@@ -13,6 +13,8 @@ urlpatterns = [
     url(r'^product_order/$', ProductOrderViewSet.as_view()),
     url(r'^maint_reserve/$', MaintOrderReserveViewSet.as_view()),
     url(r'^maint_reserve/options/$', MaintReserveOptionsView.as_view()),
+    url(r'^usedvehicle/brands/$', UsedVehicleBrandsView.as_view()),
+    url(r'^plan_product_order/$', PlanProductOrderView.as_view()),
     url(r'^vehicle/$', CustomerVehicleViewSet.as_view()),
 ]
 

+ 87 - 0
apps/wechat/customer/views.py

@@ -17,6 +17,7 @@ from apps.customer.filters import *
 from apps.vehicle_order.filters import *
 from apps.product_order.filters import *
 from apps.maint_order.filters import *
+from .xgj import XGJ
 
 
 class VehicleDriveReserveViewSet(generics.ListCreateAPIView):
@@ -43,6 +44,13 @@ class VehicleDriveReserveViewSet(generics.ListCreateAPIView):
             serializer.save()
             instance = serializer.instance
             validated_data = serializer.validated_data
+            # 同步销管佳
+            abutment_xgj = Config.getConfigValue(Config.KEY_ABUTMENT_XGJ) or ''
+            if abutment_xgj and abutment_xgj == 'true':
+                try:
+                    XGJ.drive_reserve(instance)
+                except Exception as e:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'试驾预约同步销管佳失败[%s],id=%d' % (str(e), instance.id))
             tenant_log(instance.customer.user, BizLog.INSERT, u'客户添加预约试驾,id=%d' % instance.id, validated_data)
 
         return response_ok()
@@ -72,6 +80,15 @@ class VehicleInquiryViewSet(generics.ListCreateAPIView):
             serializer.save()
             instance = serializer.instance
             validated_data = serializer.validated_data
+
+            #同步销管佳
+            abutment_xgj = Config.getConfigValue(Config.KEY_ABUTMENT_XGJ) or ''
+            if abutment_xgj and abutment_xgj == 'true':
+                try:
+                    XGJ.vehicle_inquiry(instance)
+                except Exception as e:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'新车询价同步销管佳失败[%s],id=%d' % (str(e), instance.id))
+
             tenant_log(instance.customer.user, BizLog.INSERT, u'客户添加新车询价,id=%d' % instance.id, validated_data)
 
         return response_ok()
@@ -128,6 +145,14 @@ class UsedVehicleEstimateViewSet(generics.ListCreateAPIView):
             serializer.save()
             instance = serializer.instance
             validated_data = serializer.validated_data
+
+            # 同步销管佳
+            abutment_xgj = Config.getConfigValue(Config.KEY_ABUTMENT_XGJ) or ''
+            if abutment_xgj and abutment_xgj == 'true':
+                try:
+                    XGJ.vehicle_estimate(instance)
+                except Exception as e:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'置换咨询同步销管佳失败[%s],id=%d' % (str(e), instance.id))
             tenant_log(instance.customer.user, BizLog.INSERT, u'客户添加二手车预估,id=%d' % instance.id, validated_data)
 
         return response_ok()
@@ -273,11 +298,73 @@ class CustomerAddressViewSet(CustomModelViewSet):
             tenant_log(instance.customer.user, BizLog.INSERT, u'客户删除收货地址,id=%d' % instance.id)
         return response_ok()
 
+    @action(methods=['get'], detail=True)
+    def set_default(self, request, *args, **kwargs):
+        with transaction.atomic():
+            status = request.GET.get('status')
+            instance = self.get_object()
+            if status == 'true':
+                self.get_queryset().filter(customer=self.request.customer, default=True).update(default=False)
+                instance.default = True
+                tenant_log(instance.customer.user, BizLog.UPDATE, u'客户设置默认收货地址,id=%d' % instance.id)
+            else:
+                instance.default = False
+                tenant_log(instance.customer.user, BizLog.UPDATE, u'客户取消默认收货地址,id=%d' % instance.id)
+            instance.save()
+        return response_ok()
+
 
 class MaintReserveOptionsView(APIView):
+    permission_classes = [IsCustomerUser, ]
     def get(self, request):
         data = {
             'maint_types': Option.objects.filter(type=Option.MAINT_TYPE, delete=False, enable=True).values('id', 'name'), #品牌
             'vehicles': CustomerVehicle.objects.filter(customer=request.customer, delete=False).values('id', 'number') #车辆
         }
+        return response_ok(data)
+
+
+class UsedVehicleBrandsView(APIView):
+    def get(self, request):
+        rows = Option.objects.filter(delete=False, enable=True, type=Option.USED_VEHICLE_BRAND)
+        data = rows.values('id', 'name')
+        return response_ok(data)
+
+
+class PlanProductOrderView(APIView):
+    permission_classes = [IsCustomerUser, ]
+
+    def get(self, request):
+        product_id = request.GET.get('id')
+
+        product_order = Product.objects.filter(id=product_id).first()
+
+        address_order = CustomerAddress.objects.filter(customer=request.customer, default=True, delete=False).first()
+        if not address_order:
+            address_order = CustomerAddress.objects.filter(customer=request.customer, delete=False).first()
+
+        product_item = {}
+        if product_order:
+            product_item = {
+                'name': product_order.name,
+                'describe': product_order.describe,
+                'notes': product_order.notes,
+                'cover': product_order.cover_id and '{}{}'.format(settings.MEDIA_URL, product_order.cover.picture) or '',
+                'price': Formater.formatPriceShow(product_order.price),
+            }
+
+        address_item = {}
+        if address_order:
+            address_item = {
+                'name': address_order.name,
+                'tel': address_order.tel,
+                'area': address_order.area,
+                'addr': address_order.addr,
+                'default': address_order.default,
+            }
+
+        data = {
+            'product': product_item,
+            'address': address_item,
+        }
         return response_ok(data)

+ 138 - 3
apps/wechat/customer/xgj.py

@@ -1,5 +1,11 @@
 # coding=utf-8
-
+import json
+import requests
+from django.utils import timezone
+from django.conf import settings
+from hashlib import md5
+from utils.exceptions import CustomError
+from apps.base import Formater
 from django.db import transaction
 from rest_framework.views import APIView
 from rest_framework.decorators import action
@@ -10,11 +16,140 @@ from apps.foundation.models import BizLog, Config
 from rest_framework.exceptions import NotFound
 
 
+
+def gender_sign(gateway_key):
+    ts = timezone.now().strftime('%Y%m%d%H%M%S')
+    token = gateway_key + ts
+
+    m = md5()
+    m.update(token.encode("utf8"))
+    sign = m.hexdigest()
+    return ts, sign
+
+
 class XGJ():
     @staticmethod
-    def drive_reserve(instance): #试驾预约单
+    def vehicle_inquiry(instance): #询价单
+        xgj_ip = Config.getConfigValue(Config.KEY_XGJ_IP)
+        xgj_session_key = Config.getConfigValue(Config.KEY_XGJ_SESSION_KEY)
+
+        if xgj_ip and xgj_session_key:
+            ts, sign = gender_sign(xgj_session_key)
+            url = xgj_ip + 'api/wechat_applet/vehicle_floor_add/?ts=' + ts + '&sign=' + sign
+
+            param = {
+                'company': instance.shop.xgj_id,
+                'tel': instance.tel,
+                'name': instance.name,
+                'model': instance.model.series.brand.name + '-' + instance.model.series.name + '-' + instance.model.name,
+                'guide_price': Formater.formatPriceShow(instance.model.price),
+                'shop_price': Formater.formatPriceShow(instance.model.sale_price),
+                'notes': instance.notes
+            }
+            result = requests.post(url=url, data=json.dumps(param))
+            result = result.json()
+
+            if not result['success']:
+                if 'errors' in result:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'新车询价同步销管佳失败[%s],id=%d' % (str(result['errors']), instance.id))
+                else:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'新车询价同步销管佳失败,id=%d' % (instance.id))
+            else:
+                tenant_log(instance.customer.user, BizLog.INSERT, u'新车询价同步销管佳成功,id=%d' % (instance.id))
+        else:
+            tenant_log(instance.customer.user, BizLog.INSERT, u'新车询价同步销管佳失败,没有设置销管佳地址或密钥,id=%d' % (instance.id))
+
+    @staticmethod
+    def drive_reserve(instance): #试驾单
+        xgj_ip = Config.getConfigValue(Config.KEY_XGJ_IP)
+        xgj_session_key = Config.getConfigValue(Config.KEY_XGJ_SESSION_KEY)
+
+        if xgj_ip and xgj_session_key:
+            ts, sign = gender_sign(xgj_session_key)
+            url = xgj_ip + 'api/wechat_applet/vehicle_drive_add/?ts=' + ts + '&sign=' + sign
+
+            param = {
+                'company': instance.shop.xgj_id,
+                'tel': instance.tel,
+                'name': instance.name,
+                'model': instance.model.series.brand.name + '-' + instance.model.series.name + '-' + instance.model.name,
+                'date': instance.date,
+                'notes': instance.notes
+            }
+            result = requests.post(url=url, data=json.dumps(param))
+            result = result.json()
+
+            if not result['success']:
+                if 'errors' in result:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'试驾预约同步销管佳失败[%s],id=%d' % (str(result['errors']), instance.id))
+                else:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'试驾预约同步销管佳失败,id=%d' % (instance.id))
+            else:
+                tenant_log(instance.customer.user, BizLog.INSERT, u'试驾预约同步销管佳成功,id=%d' % (instance.id))
+        else:
+            tenant_log(instance.customer.user, BizLog.INSERT, u'试驾预约同步销管佳失败,没有设置销管佳地址或密钥,id=%d' % (instance.id))
+
+
+    @staticmethod
+    def vehicle_estimate(instance): #置换咨询
+        xgj_ip = Config.getConfigValue(Config.KEY_XGJ_IP)
+        xgj_session_key = Config.getConfigValue(Config.KEY_XGJ_SESSION_KEY)
+
+        if xgj_ip and xgj_session_key:
+            ts, sign = gender_sign(xgj_session_key)
+            url = xgj_ip + 'api/wechat_applet/vehicle_exchange_add/?ts=' + ts + '&sign=' + sign
+
+            param = {
+                'company': instance.shop.xgj_id,
+                'tel': instance.customer.tel,
+                'name': instance.customer.name,
+                'model': instance.name + '-' + instance.model,
+                'plate_date': instance.plate_date,
+                'mileage': instance.mileage,
+                'notes': instance.notes
+            }
+            result = requests.post(url=url, data=json.dumps(param))
+            result = result.json()
+
+            if not result['success']:
+                if 'errors' in result:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'置换咨询同步销管佳失败[%s],id=%d' % (str(result['errors']), instance.id))
+                else:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'置换咨询同步销管佳失败,id=%d' % (instance.id))
+            else:
+                tenant_log(instance.customer.user, BizLog.INSERT, u'置换咨询同步销管佳成功,id=%d' % (instance.id))
+        else:
+            tenant_log(instance.customer.user, BizLog.INSERT, u'置换咨询同步销管佳失败,没有设置销管佳地址或密钥,id=%d' % (instance.id))
+
+    @staticmethod
+    def maint_reserve(instance):  # 售后预约
         xgj_ip = Config.getConfigValue(Config.KEY_XGJ_IP)
         xgj_session_key = Config.getConfigValue(Config.KEY_XGJ_SESSION_KEY)
 
         if xgj_ip and xgj_session_key:
-            pass
+            ts, sign = gender_sign(xgj_session_key)
+            url = xgj_ip + 'api/wechat_applet/maint_reserve_add/?ts=' + ts + '&sign=' + sign
+
+            param = {
+                'company': instance.shop.xgj_id,
+                'tel': instance.tel,
+                'name': instance.name,
+                'model': instance.vehicle.name,
+                'number': instance.vehicle.number,
+                'vin': instance.vehicle.vin,
+                'date': instance.date,
+                'notes': instance.notes
+            }
+            result = requests.post(url=url, data=json.dumps(param))
+            result = result.json()
+
+            if not result['success']:
+                if 'errors' in result:
+                    tenant_log(instance.customer.user, BizLog.INSERT,
+                               u'售后预约同步销管佳失败[%s],id=%d' % (str(result['errors']), instance.id))
+                else:
+                    tenant_log(instance.customer.user, BizLog.INSERT, u'售后预约同步销管佳失败,id=%d' % (instance.id))
+            else:
+                tenant_log(instance.customer.user, BizLog.INSERT, u'售后预约同步销管佳成功,id=%d' % (instance.id))
+        else:
+            tenant_log(instance.customer.user, BizLog.INSERT, u'售后预约同步销管佳失败,没有设置销管佳地址或密钥,id=%d' % (instance.id))