123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- # coding=utf-8
- from django.db import transaction
- from rest_framework.views import APIView
- from rest_framework import generics
- from rest_framework_jwt.views import VerifyJSONWebToken,RefreshJSONWebToken
- from rest_framework.serializers import ValidationError
- from utils import response_ok, response_error
- from utils.wx.WXBizDataCrypt import WXBizDataCrypt
- from utils.permission import IsCustomerUser
- from .serializers import *
- class CustomerLoginView(APIView):
- serializer_class = CustomerMobileSignSerializer
- def post(self, request, *args, **kwargs):
- ser = self.serializer_class(data=request.data)
- if ser.is_valid():
- return response_ok(ser.validated_data)
- else:
- return response_error('参数错误')
- class CustomerRefreshTokenView(RefreshJSONWebToken):
- def post(self, request, *args, **kwargs):
- try:
- ser = self.serializer_class(data=request.data)
- if ser.is_valid(raise_exception=True):
- return response_ok({'token': ser.validated_data['token']})
- except ValidationError as e:
- return response_error(u'登录状态失效,请重新登录[' + e.detail['error'][0] + ']')
- class CustomerVerifyTokenView(VerifyJSONWebToken):
- def post(self, request, *args, **kwargs):
- try:
- ser = self.serializer_class(data=request.data)
- if ser.is_valid(raise_exception=True):
- return response_ok({'token': ser.validated_data['token']})
- except ValidationError as e:
- return response_error(u'登录状态失效,请重新登录[' + e.detail['error'][0] + ']')
- class WxLoginView(APIView):
- serializer_class = WechatLoginSerializer
- def post(self, request, *args, **kwargs):
- ser = self.serializer_class(data=request.data)
- if ser.is_valid():
- return response_ok(ser.validated_data)
- else:
- return response_error('参数错误')
- class WxBindView(APIView):
- serializer_class = WechatBindSerializer
- def post(self, request, *args, **kwargs):
- ser = self.serializer_class(data=request.data)
- if ser.is_valid():
- return response_ok(ser.validated_data)
- else:
- return response_error('参数错误')
- class SetUserInfoView(APIView):
- permission_classes = [IsCustomerUser, ]
- def post(self, request, *args, **kwargs):
- openid = request.POST.get('openid')
- encryptedData = request.POST.get('encryptedData')
- iv = request.POST.get('iv')
- customer_wechat = CustomerWechat.objects.filter(openid=openid).first()
- if not customer_wechat:
- raise CustomError(u'未找到相应的微信客户!')
- customer = request.customer
- if customer_wechat.customer and customer_wechat.customer.id != customer.id:
- raise CustomError(u'该微信已同步其他客户!')
- if not customer_wechat.customer:
- customer_wechat.customer = customer
- customer_wechat.save()
- pc = WXBizDataCrypt(customer_wechat.session_key)
- result = pc.decrypt(encryptedData, iv)
- with transaction.atomic():
- customer.setInfo(result['nickName'], result['gender'], result['avatarUrl'])
- tenant_log(customer.user, BizLog.INSERT, u'客户设置信息,id=%d' % customer.id, result)
- return response_ok()
- class CustomerInfoView(generics.RetrieveAPIView):
- permission_classes = [IsCustomerUser, ]
- # queryset = Customer.objects.filter()
- serializer_class = CustomerSerializer
- def get_object(self):
- # queryset = self.filter_queryset(self.get_queryset())
- # obj = queryset.filter(id=self.request.customer.id).first()
- obj = self.request.customer
- return obj
- def retrieve(self, request, *args, **kwargs):
- instance = self.get_object()
- serializer = self.get_serializer(instance)
- return response_ok(serializer.data)
|