serializers.py 5.6 KB

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