serializers.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.tenant.models import Tenant
  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. tenant_id = customer_wechat.customer.tenant and customer_wechat.customer.tenant.id or ''
  35. company_no = customer_wechat.customer.tenant and customer_wechat.customer.tenant.company_no or ''
  36. nick_name = customer_wechat.customer.tenant and customer_wechat.customer.tenant.company_name or settings.WEAPP['nick_name']
  37. is_validity = Tenant.check_validity(company_no)
  38. return {
  39. 'bind': 1,
  40. 'user_id': user.id,
  41. 'token': jwt_encode_handler(payload),
  42. 'openid': customer_wechat.openid,
  43. 'tenant_id': tenant_id,
  44. 'is_validity': is_validity,
  45. 'nick_name': nick_name,
  46. 'name': customer_wechat.customer.name or '',
  47. 'tel': customer_wechat.customer.tel or '',
  48. 'face': customer_wechat.customer.face,
  49. 'user_type': customer_wechat.customer.type, # 用户类别,1为平台管理员,2为管理者,3检修人,4报修人
  50. 'forbid_baoxiu': 'true' and customer_wechat.customer.status == Employee.DISABLE or 'false', # 是否禁用报修,
  51. 'emplate_id': [settings.WEAPP['message_template_finish'],settings.WEAPP['message_template_wait_check'],
  52. settings.WEAPP['message_template_dispatch'],],
  53. }
  54. else:
  55. msg = '参数无效'
  56. raise serializers.ValidationError(msg)
  57. class WechatBindSerializer(serializers.Serializer):
  58. def validate(self, attrs):
  59. appid = self.initial_data.get('appid')
  60. openid = self.initial_data.get('openid')
  61. phoneEncryptedData = self.initial_data.get('encryptedData')
  62. phoneIv = self.initial_data.get('iv')
  63. company_no = self.initial_data.get('company_no')
  64. if openid and phoneEncryptedData and phoneIv:
  65. customer = CustomerWechat.bindWechat(appid, openid, phoneEncryptedData, phoneIv)
  66. user = customer.user
  67. payload = jwt_payload_handler(user)
  68. customer_log(customer, BizLog.INSERT, u'用户微信登录,username=%s' % user.username)
  69. tenant_id = ''
  70. nick_name = settings.WEAPP['nick_name']
  71. if customer.tenant:
  72. tenant_id = customer.tenant.id
  73. company_no = customer.tenant.company_no
  74. nick_name = customer.tenant.company_name
  75. elif company_no:
  76. # 扫二维码,没有绑定企业的用户之间绑定企业
  77. tenant = Tenant.objects.filter(company_no=company_no).first()
  78. if tenant:
  79. customer.tenant = tenant
  80. customer.save()
  81. tenant_id = tenant.id
  82. company_no = tenant.company_no
  83. nick_name = tenant.company_name
  84. is_validity = Tenant.check_validity(company_no)
  85. return {
  86. 'token': jwt_encode_handler(payload),
  87. 'user_id': user.id,
  88. 'name': customer.name or '',
  89. 'tel': customer.tel or '',
  90. 'face': customer.face,
  91. 'nick_name': nick_name,
  92. 'is_validity': is_validity,
  93. 'user_type': customer.type, # 用户类别,1为平台管理员,2为管理者,3检修人,4报修人
  94. 'forbid_baoxiu': 'true' and customer.status == Employee.DISABLE or 'false', # 是否禁用报修,
  95. 'tenant_id': tenant_id,
  96. 'emplate_id': [settings.WEAPP['message_template_finish'],settings.WEAPP['message_template_wait_check'],
  97. settings.WEAPP['message_template_dispatch'],],
  98. }
  99. else:
  100. msg = '参数无效'
  101. raise serializers.ValidationError(msg)