serializers.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. User = get_user_model()
  13. jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
  14. jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
  15. class WechatLoginSerializer(serializers.Serializer):
  16. def validate(self, attrs):
  17. code = self.initial_data.get('code')
  18. appid = self.initial_data.get('appid')
  19. if code and appid:
  20. customer_wechat = CustomerWechat.login(code, appid)
  21. if not customer_wechat.customer:
  22. return {
  23. 'bind': 0,
  24. 'openid': customer_wechat.openid,
  25. 'nick_name': customer_wechat.wechat_app.nick_name,
  26. 'permission':[],
  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. wechat_applet = WechatApplet.objects.filter(tenant=user.employee.tenant).first()
  35. return {
  36. 'bind': 1,
  37. 'user_id': user.id,
  38. 'token': jwt_encode_handler(payload),
  39. 'openid': customer_wechat.openid,
  40. 'nick_name': customer_wechat.wechat_app.nick_name or '',
  41. 'name': customer_wechat.customer.name or '',
  42. 'tel': customer_wechat.customer.tel or '',
  43. 'face': customer_wechat.customer.face,
  44. 'gender': customer_wechat.customer.gender or 0,
  45. 'position': customer_wechat.customer.position and customer_wechat.customer.position.name or '',
  46. 'user_type': customer_wechat.customer.type, # 用户类别,1为员工,2为游客
  47. 'forbid_baoxiu': customer_wechat.customer.forbid_baoxiu, # 禁用报修,
  48. 'permission': list(user.get_all_permissions()),
  49. 'emplate_id': wechat_applet.getMsgTemplateId()
  50. }
  51. else:
  52. msg = '参数无效'
  53. raise serializers.ValidationError(msg)
  54. class WechatBindSerializer(serializers.Serializer):
  55. def validate(self, attrs):
  56. appid = self.initial_data.get('appid')
  57. openid = self.initial_data.get('openid')
  58. phoneEncryptedData = self.initial_data.get('encryptedData')
  59. phoneIv = self.initial_data.get('iv')
  60. if openid and phoneEncryptedData and phoneIv:
  61. customer = CustomerWechat.bindWechat(appid, openid, phoneEncryptedData, phoneIv)
  62. user = customer.user
  63. payload = jwt_payload_handler(user)
  64. customer_log(customer, BizLog.INSERT, u'用户微信登录,username=%s' % user.username)
  65. return {
  66. 'token': jwt_encode_handler(payload),
  67. 'user_id': user.id,
  68. 'name': customer.name or '',
  69. 'tel': customer.tel or '',
  70. 'face': customer.face,
  71. 'gender': customer.gender or 0,
  72. 'position': customer.position and customer.position.name or '',
  73. 'user_type': customer.type,
  74. 'forbid_baoxiu': customer.forbid_baoxiu,
  75. 'permission': list(user.get_all_permissions())
  76. }
  77. else:
  78. msg = '参数无效'
  79. raise serializers.ValidationError(msg)