Nenhuma Descrição

oauth_views.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- coding: utf-8 -*-
  2. from django.conf import settings
  3. from django.db import transaction
  4. from django.shortcuts import render
  5. from django_logit import logit
  6. from django_response import response
  7. from json_render import json_render
  8. from account.models import LensmanInfo, TourGuideInfo
  9. from mch.models import BrandInfo, DistributorInfo, SaleclerkInfo
  10. from utils.error.errno_utils import ProductBrandStatusCode, ProductDistributorStatusCode, SaleclerkStatusCode
  11. from utils.redis.connect import r
  12. def lensman_oauth(request):
  13. lensman_type = int(request.GET.get('lt') or 0)
  14. unionid = request.GET.get('unionid', '')
  15. try:
  16. lensman = LensmanInfo.objects.get(unionid=unionid)
  17. except LensmanInfo.DoesNotExist:
  18. lensman = None
  19. return render(request, 'page/lensman_oauth.html', {
  20. 'lensman_type': lensman_type,
  21. 'lensman_info': lensman and lensman.data(lensman_type),
  22. 'modified': bool((not lensman) or (lensman and lensman.modified(lensman_type))), # 是否可以更改信息
  23. })
  24. def tourguide_oauth(request):
  25. unionid = request.GET.get('unionid', '')
  26. try:
  27. tourguide = TourGuideInfo.objects.get(unionid=unionid)
  28. except TourGuideInfo.DoesNotExist:
  29. tourguide = None
  30. return render(request, 'page/tourguide_oauth.html', {
  31. 'tourguide_info': tourguide and tourguide.data,
  32. 'modified': bool((not tourguide) or (tourguide and tourguide.modified)), # 是否可以更改信息
  33. })
  34. def login_qrcode(request):
  35. lensman_type = int(request.GET.get('lt') or 0)
  36. unionid = request.GET.get('unionid', '')
  37. data = {
  38. 'lensman_type': lensman_type,
  39. 'unionid': unionid,
  40. 'token': r.token(unionid)
  41. }
  42. return json_render(request, 'page/login_qrcode.html', data)
  43. def clerk_oauth(request):
  44. brand_id = request.GET.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  45. unionid = request.GET.get('unionid', '')
  46. try:
  47. BrandInfo.objects.get(brand_id=brand_id, status=True)
  48. except BrandInfo.DoesNotExist:
  49. return response(ProductBrandStatusCode.BRAND_NOT_FOUND)
  50. distributors = DistributorInfo.objects.filter(brand_id=brand_id, status=True)
  51. distributors = [distributor.admindata for distributor in distributors]
  52. try:
  53. clerk = SaleclerkInfo.objects.get(brand_id=brand_id, unionid=unionid, status=True)
  54. except SaleclerkInfo.DoesNotExist:
  55. clerk = None
  56. return render(request, 'page/clerk_oauth.html', {
  57. 'domain': settings.DOMAIN,
  58. 'distributors': distributors,
  59. 'clerk_info': clerk and clerk.data,
  60. 'modified': bool((not clerk) or (clerk and clerk.user_status in [SaleclerkInfo.UNVERIFIED, SaleclerkInfo.REFUSED])), # 是否可以更改信息
  61. })
  62. @logit
  63. @transaction.atomic
  64. def clerk_submit_api(request):
  65. """ 店员授权信息提交 """
  66. brand_id = request.POST.get('brand_id', settings.KODO_DEFAULT_BRAND_ID)
  67. distributor_id = request.POST.get('distributor_id', '')
  68. unionid = request.POST.get('unionid', '')
  69. openid = request.POST.get('openid', '')
  70. phone = request.POST.get('phone', '')
  71. if SaleclerkInfo.objects.filter(clerk_phone=phone).exclude(unionid=unionid).exists():
  72. return response(SaleclerkStatusCode.CLERK_PHONE_ALREADY_EXISTS)
  73. try:
  74. distributor = DistributorInfo.objects.get(distributor_id=distributor_id)
  75. except DistributorInfo.DoesNotExist:
  76. return response(ProductDistributorStatusCode.DISTRIBUTOR_NOT_FOUND)
  77. fields = {
  78. 'distributor_id': distributor_id,
  79. 'distributor_name': distributor.distributor_name,
  80. 'clerk_name': request.POST.get('name', ''),
  81. 'clerk_sex': int(request.POST.get('sex', 1)),
  82. 'clerk_phone': phone,
  83. 'openid': openid,
  84. 'user_status': SaleclerkInfo.UNVERIFIED,
  85. }
  86. clerk, created = SaleclerkInfo.objects.select_for_update().get_or_create(brand_id=brand_id, unionid=unionid, defaults=fields)
  87. # clerk.user_status = SaleclerkInfo.UNVERIFIED
  88. clerk.status = True
  89. # 状态为 UNVERIFIED 的允许修改, 其他需要登录摄影师 APP 进行信息的修改
  90. if clerk.user_status not in [SaleclerkInfo.UNVERIFIED, SaleclerkInfo.REFUSED]:
  91. return response(SaleclerkStatusCode.CLERK_ALREADY_NOT_UNVERIFIED)
  92. if not created:
  93. for key, value in fields.iteritems():
  94. setattr(clerk, key, value)
  95. clerk.save()
  96. return response(200, 'Submit Success', u'提交成功', {})