serializers.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #coding=utf-8
  2. from django.contrib.auth import get_user_model
  3. from django.conf import settings
  4. from django.db.models import Q
  5. from rest_framework import serializers
  6. from rest_framework_jwt.settings import api_settings
  7. from apps.WechatApplet.models import WechatApplet
  8. from apps.wxapp.models import CustomerWechat
  9. from apps.log.models import BizLog
  10. from apps.wxapp import customer_log
  11. from utils.exceptions import CustomError
  12. from apps.tenant.employee.models import Employee
  13. User = get_user_model()
  14. jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
  15. jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
  16. class WechatLoginSerializer(serializers.Serializer):
  17. def validate(self, attrs):
  18. code = self.initial_data.get('code') # 用户code
  19. appid = self.initial_data.get('appid') # 小程序appid
  20. if code and appid:
  21. customer_wechat = CustomerWechat.login(code, appid)
  22. if not customer_wechat.customer:
  23. return {
  24. 'bind': 0,
  25. 'openid': customer_wechat.openid,
  26. 'nick_name': settings.WEAPP['nick_name'],
  27. }
  28. user = customer_wechat.customer.user
  29. if not user.is_active:
  30. msg = '用户帐户已禁用.'
  31. raise serializers.ValidationError(msg)
  32. payload = jwt_payload_handler(user)
  33. customer_log(customer_wechat.customer, BizLog.INSERT, u'用户微信登录,username=%s' % user.username)
  34. return {
  35. 'bind': 1,
  36. 'user_id': user.id,
  37. 'token': jwt_encode_handler(payload),
  38. 'openid': customer_wechat.openid,
  39. 'tenant_id': customer_wechat.customer.tenant and customer_wechat.customer.tenant.id or '',
  40. 'nick_name': settings.WEAPP['nick_name'],
  41. 'name': customer_wechat.customer.name or '',
  42. 'tel': customer_wechat.customer.tel or '',
  43. 'face': customer_wechat.customer.face,
  44. 'user_type': customer_wechat.customer.type, # 用户类别,1为平台管理员,2为管理者,3检修人,4报修人
  45. 'forbid_baoxiu': 'true' and customer_wechat.customer.status == Employee.DISABLE or 'false', # 是否禁用报修,
  46. 'emplate_id': settings.WEAPP['message_template'],
  47. }
  48. else:
  49. msg = '参数无效'
  50. raise serializers.ValidationError(msg)
  51. class WechatBindSerializer(serializers.Serializer):
  52. def validate(self, attrs):
  53. appid = self.initial_data.get('appid')
  54. openid = self.initial_data.get('openid')
  55. phoneEncryptedData = self.initial_data.get('encryptedData')
  56. phoneIv = self.initial_data.get('iv')
  57. if openid and phoneEncryptedData and phoneIv:
  58. customer = CustomerWechat.bindWechat(appid, openid, phoneEncryptedData, phoneIv)
  59. user = customer.user
  60. payload = jwt_payload_handler(user)
  61. customer_log(customer, BizLog.INSERT, u'用户微信登录,username=%s' % user.username)
  62. return {
  63. 'token': jwt_encode_handler(payload),
  64. 'user_id': user.id,
  65. 'name': customer.name or '',
  66. 'tel': customer.tel or '',
  67. 'face': customer.face,
  68. 'user_type': customer.type, # 用户类别,1为平台管理员,2为管理者,3检修人,4报修人
  69. 'forbid_baoxiu': 'true' and customer.status == Employee.DISABLE or 'false', # 是否禁用报修,
  70. 'tenant_id': customer.tenant and customer.tenant.id or '',
  71. }
  72. else:
  73. msg = '参数无效'
  74. raise serializers.ValidationError(msg)