serializers.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # coding=utf-8
  2. from django.contrib.auth import get_user_model
  3. from rest_framework import serializers
  4. from rest_framework_jwt.settings import api_settings
  5. from apps.customer.models import *
  6. from apps.log.models import BizLog
  7. from apps.base import Formater
  8. User = get_user_model()
  9. jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
  10. jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
  11. class WechatLoginSerializer(serializers.Serializer):
  12. def validate(self, attrs):
  13. code = self.initial_data.get('code')
  14. appid = self.initial_data.get('appid')
  15. if code and appid:
  16. wx_customer = CustomerWechat.login(code, appid)
  17. if not wx_customer.customer:
  18. return {
  19. 'bind': 0,
  20. 'openid': wx_customer.openid
  21. }
  22. user = wx_customer.customer.user
  23. if not user.is_active:
  24. msg = '用户帐户已禁用.'
  25. raise serializers.ValidationError(msg)
  26. payload = jwt_payload_handler(user)
  27. BizLog.objects.addnew(user, BizLog.INSERT, u'用户微信登录,username=%s' % user.username)
  28. return {
  29. 'bind': 1,
  30. 'token': jwt_encode_handler(payload),
  31. 'openid': wx_customer.openid,
  32. 'name': wx_customer.customer.name or '',
  33. 'tel': wx_customer.customer.tel or '',
  34. 'face': wx_customer.customer.face and wx_customer.customer.face.get_path() or '',
  35. 'customer_id': wx_customer.customer.id,
  36. }
  37. else:
  38. msg = '参数无效'
  39. raise serializers.ValidationError(msg)
  40. class WechatBindSerializer(serializers.Serializer):
  41. def validate(self, attrs):
  42. appid = self.initial_data.get('appid')
  43. openid = self.initial_data.get('openid')
  44. phoneEncryptedData = self.initial_data.get('encryptedData')
  45. phoneIv = self.initial_data.get('iv')
  46. referrer = self.initial_data.get('referrer')
  47. if openid and phoneEncryptedData and phoneIv:
  48. customer = CustomerWechat.bindWechat(appid, openid, phoneEncryptedData, phoneIv, referrer)
  49. user = customer.user
  50. payload = jwt_payload_handler(user)
  51. BizLog.objects.addnew(user, BizLog.INSERT, u'用户微信登录,username=%s' % user.username)
  52. return {
  53. 'token': jwt_encode_handler(payload),
  54. 'name': customer.name or '',
  55. 'tel': customer.tel or '',
  56. 'face': customer.face and customer.face.get_path() or '',
  57. 'customer_id': customer.id,
  58. }
  59. else:
  60. msg = '参数无效'
  61. raise serializers.ValidationError(msg)
  62. class CustomerSerializer(serializers.ModelSerializer):
  63. gender_text = serializers.CharField(source='get_gender_display', read_only=True)
  64. is_distributor_text = serializers.SerializerMethodField()
  65. balance = serializers.SerializerMethodField()
  66. total_amount = serializers.SerializerMethodField()
  67. date_join = serializers.DateTimeField(source='user.date_joined', format='%Y-%m-%d %H:%M', read_only=True)
  68. class Meta:
  69. model = Customer
  70. fields = '__all__'
  71. def get_is_distributor_text(self, obj):
  72. if obj.is_distributor:
  73. return u'是'
  74. return u'否'
  75. def get_balance(self, obj):
  76. return Formater.formatAmountShow(obj.balance)
  77. def get_total_amount(self, obj):
  78. return Formater.formatAmountShow(obj.total_amount)