models.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # coding=utf-8
  2. import requests
  3. from django.db import transaction
  4. from django.db import models
  5. from django.utils import timezone
  6. from django.conf import settings
  7. from apps.tenant.models import Tenant
  8. from apps.WechatApplet.models import WechatApplet
  9. from apps.account.models import User
  10. from utils.wx.wechat import WeChat
  11. from utils.wx.WXBizDataCrypt import WXBizDataCrypt
  12. from utils.exceptions import CustomError
  13. from apps.tenant.employee.models import Employee
  14. class CustomerWechat(models.Model):
  15. # wechat_app = models.ForeignKey(WechatApplet, verbose_name=u'小程序', on_delete=models.PROTECT, editable=False)
  16. customer = models.ForeignKey(Employee, verbose_name=u'用户', on_delete=models.PROTECT, editable=False, null=True)
  17. openid = models.CharField(max_length=512, verbose_name=u"openid")
  18. session_key = models.CharField(max_length=512, verbose_name=u'session_key',null=True)
  19. class Meta:
  20. db_table = 'customer_wechat'
  21. verbose_name = u'微信客户'
  22. unique_together = [
  23. ('openid')
  24. ]
  25. default_permissions = ()
  26. @staticmethod
  27. def login(code, appid):
  28. # wechat_applet = WechatApplet.getByAppid(appid)
  29. secret = settings.WEAPP['secret']
  30. res = WeChat.code2Session(appid, secret, code)
  31. instance = CustomerWechat.objects.filter(openid=res['openid']).first()
  32. if not instance:
  33. instance = CustomerWechat.objects.create(
  34. # wechat_app=wechat_applet,
  35. openid=res['openid'],
  36. session_key=res['session_key']
  37. )
  38. else:
  39. instance.session_key = res['session_key']
  40. instance.save()
  41. return instance
  42. @staticmethod
  43. def bindWechat(appid, openid, phoneEncryptedData, phoneIv):
  44. customer_wechat = CustomerWechat.objects.filter(openid=openid).first()
  45. if not customer_wechat:
  46. raise CustomError(u'未找到相应的微信客户!')
  47. pc = WXBizDataCrypt(appid, customer_wechat.session_key)
  48. phon_data = pc.decrypt(phoneEncryptedData, phoneIv)
  49. tel = phon_data['purePhoneNumber']
  50. if customer_wechat.customer:
  51. if customer_wechat.customer.user.username != tel:
  52. raise CustomError(u'微信绑定的手机号与系统记录的不符!')
  53. else:
  54. return customer_wechat.customer
  55. user = User.objects.filter(username=tel).first()
  56. if not user:
  57. user = User.objects.create_customer(tel, password=tel[5:])
  58. try:
  59. with transaction.atomic():
  60. customer = Employee.getOrRegister(user=user)
  61. except:
  62. raise CustomError(u'用户注册失败!')
  63. customer_wechat.customer = customer
  64. customer_wechat.save()
  65. return customer
  66. @staticmethod
  67. def sendFinishMsg(user, name, address, fault_des, no):
  68. """
  69. :param user: 接收人
  70. :param name: 接收人姓名
  71. :param address: 地址
  72. :param fault_des: 描述
  73. :param no: 单号
  74. """
  75. wechat_customer = CustomerWechat.objects.filter(customer__user=user).first()
  76. if wechat_customer:
  77. applet= WechatApplet.objects.filter(authorizer_appid=settings.WEAPP['appid']).first()
  78. applet.sendFinishMsg(wechat_customer.openid, name, address, fault_des, no)
  79. @staticmethod
  80. def sendWaitCheckMsg(user, name, address, fault_des, no):
  81. """
  82. :param user: 接收人
  83. :param name: 下单人姓名
  84. :param address: 地址
  85. :param fault_des: 描述
  86. :param no: 单号
  87. """
  88. wechat_customer = CustomerWechat.objects.filter(customer__user=user).first()
  89. if wechat_customer:
  90. applet= WechatApplet.objects.filter(authorizer_appid=settings.WEAPP['appid']).first()
  91. applet.sendWaitCheckMsg(wechat_customer.openid, name, address, fault_des, no)
  92. @staticmethod
  93. def sendDispatchMsg(user, name, address, fault_des, time, no):
  94. """
  95. :param user: 接收人
  96. :param name: 下单人姓名
  97. :param address: 地址
  98. :param fault_des: 描述
  99. :param no: 单号
  100. :param time: 报修时间
  101. """
  102. wechat_customer = CustomerWechat.objects.filter(customer__user=user).first()
  103. if wechat_customer:
  104. applet= WechatApplet.objects.filter(authorizer_appid=settings.WEAPP['appid']).first()
  105. applet.sendDispatchMsg(wechat_customer.openid, name, address, fault_des, time, no)