views.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # coding=utf-8
  2. from django.db import transaction
  3. from rest_framework.views import APIView
  4. from rest_framework import generics
  5. from rest_framework_jwt.views import VerifyJSONWebToken,RefreshJSONWebToken
  6. from rest_framework.serializers import ValidationError
  7. from utils import response_ok, response_error
  8. from utils.wx.WXBizDataCrypt import WXBizDataCrypt
  9. from utils.permission import IsCustomerUser
  10. from .serializers import *
  11. class CustomerLoginView(APIView):
  12. serializer_class = CustomerMobileSignSerializer
  13. def post(self, request, *args, **kwargs):
  14. ser = self.serializer_class(data=request.data)
  15. if ser.is_valid():
  16. return response_ok(ser.validated_data)
  17. else:
  18. return response_error('参数错误')
  19. class CustomerRefreshTokenView(RefreshJSONWebToken):
  20. def post(self, request, *args, **kwargs):
  21. try:
  22. ser = self.serializer_class(data=request.data)
  23. if ser.is_valid(raise_exception=True):
  24. return response_ok({'token': ser.validated_data['token']})
  25. except ValidationError as e:
  26. return response_error(u'登录状态失效,请重新登录[' + e.detail['error'][0] + ']')
  27. class CustomerVerifyTokenView(VerifyJSONWebToken):
  28. def post(self, request, *args, **kwargs):
  29. try:
  30. ser = self.serializer_class(data=request.data)
  31. if ser.is_valid(raise_exception=True):
  32. return response_ok({'token': ser.validated_data['token']})
  33. except ValidationError as e:
  34. return response_error(u'登录状态失效,请重新登录[' + e.detail['error'][0] + ']')
  35. class WxLoginView(APIView):
  36. serializer_class = WechatLoginSerializer
  37. def post(self, request, *args, **kwargs):
  38. ser = self.serializer_class(data=request.data)
  39. if ser.is_valid():
  40. return response_ok(ser.validated_data)
  41. else:
  42. return response_error('参数错误')
  43. class WxBindView(APIView):
  44. serializer_class = WechatBindSerializer
  45. def post(self, request, *args, **kwargs):
  46. ser = self.serializer_class(data=request.data)
  47. if ser.is_valid():
  48. return response_ok(ser.validated_data)
  49. else:
  50. return response_error('参数错误')
  51. class SetUserInfoView(APIView):
  52. permission_classes = [IsCustomerUser, ]
  53. def post(self, request, *args, **kwargs):
  54. openid = request.POST.get('openid')
  55. encryptedData = request.POST.get('encryptedData')
  56. iv = request.POST.get('iv')
  57. customer_wechat = CustomerWechat.objects.filter(openid=openid).first()
  58. if not customer_wechat:
  59. raise CustomError(u'未找到相应的微信客户!')
  60. customer = request.customer
  61. if customer_wechat.customer and customer_wechat.customer.id != customer.id:
  62. raise CustomError(u'该微信已同步其他客户!')
  63. if not customer_wechat.customer:
  64. customer_wechat.customer = customer
  65. customer_wechat.save()
  66. pc = WXBizDataCrypt(customer_wechat.session_key)
  67. result = pc.decrypt(encryptedData, iv)
  68. with transaction.atomic():
  69. customer.setInfo(result['nickName'], result['gender'], result['avatarUrl'])
  70. tenant_log(customer.user, BizLog.INSERT, u'客户设置信息,id=%d' % customer.id, result)
  71. return response_ok()
  72. class CustomerInfoView(generics.RetrieveAPIView):
  73. permission_classes = [IsCustomerUser, ]
  74. # queryset = Customer.objects.filter()
  75. serializer_class = CustomerSerializer
  76. def get_object(self):
  77. # queryset = self.filter_queryset(self.get_queryset())
  78. # obj = queryset.filter(id=self.request.customer.id).first()
  79. obj = self.request.customer
  80. return obj
  81. def retrieve(self, request, *args, **kwargs):
  82. instance = self.get_object()
  83. serializer = self.get_serializer(instance)
  84. return response_ok(serializer.data)