serializers.py 3.5 KB

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