serializers.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. is_validity = Tenant.check_validity(company_no)
  37. return {
  38. 'bind': 1,
  39. 'user_id': user.id,
  40. 'token': jwt_encode_handler(payload),
  41. 'openid': customer_wechat.openid,
  42. 'tenant_id': tenant_id,
  43. 'is_validity': is_validity,
  44. 'nick_name': settings.WEAPP['nick_name'],
  45. 'name': customer_wechat.customer.name or '',
  46. 'tel': customer_wechat.customer.tel or '',
  47. 'face': customer_wechat.customer.face,
  48. 'user_type': customer_wechat.customer.type, # 用户类别,1为平台管理员,2为管理者,3检修人,4报修人
  49. 'forbid_baoxiu': 'true' and customer_wechat.customer.status == Employee.DISABLE or 'false', # 是否禁用报修,
  50. 'emplate_id': settings.WEAPP['message_template_finish'],
  51. }
  52. else:
  53. msg = '参数无效'
  54. raise serializers.ValidationError(msg)
  55. class WechatBindSerializer(serializers.Serializer):
  56. def validate(self, attrs):
  57. appid = self.initial_data.get('appid')
  58. openid = self.initial_data.get('openid')
  59. phoneEncryptedData = self.initial_data.get('encryptedData')
  60. phoneIv = self.initial_data.get('iv')
  61. if openid and phoneEncryptedData and phoneIv:
  62. customer = CustomerWechat.bindWechat(appid, openid, phoneEncryptedData, phoneIv)
  63. user = customer.user
  64. payload = jwt_payload_handler(user)
  65. customer_log(customer, BizLog.INSERT, u'用户微信登录,username=%s' % user.username)
  66. tenant_id = customer.tenant and customer.tenant.id or ''
  67. company_no = customer.tenant and customer.tenant.company_no or ''
  68. is_validity = Tenant.check_validity(company_no)
  69. return {
  70. 'token': jwt_encode_handler(payload),
  71. 'user_id': user.id,
  72. 'name': customer.name or '',
  73. 'tel': customer.tel or '',
  74. 'face': customer.face,
  75. 'is_validity': is_validity,
  76. 'user_type': customer.type, # 用户类别,1为平台管理员,2为管理者,3检修人,4报修人
  77. 'forbid_baoxiu': 'true' and customer.status == Employee.DISABLE or 'false', # 是否禁用报修,
  78. 'tenant_id': tenant_id,
  79. 'emplate_id': settings.WEAPP['message_template_finish'],
  80. }
  81. else:
  82. msg = '参数无效'
  83. raise serializers.ValidationError(msg)