No Description

encrypt_views.py 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. # -*- coding: utf-8 -*-
  2. from __future__ import division
  3. import random
  4. import re
  5. from django.conf import settings
  6. from django.db import transaction
  7. from django_logit import logit
  8. from django_response import response
  9. from pywe_marketcode import tickettocode
  10. from pywe_storage import RedisStorage
  11. from account.models import UserInfo
  12. from logs.models import MchInfoDecryptLogInfo, MchInfoEncryptLogInfo
  13. from marketcode.models import MarketCodeInfo
  14. from mch.models import ActivityInfo, BrandInfo, ModelInfo
  15. from utils.algorithm.b64 import b64_decrypt, b64_encrypt
  16. from utils.algorithm.caesar import caesar_decrypt, caesar_encrypt
  17. from utils.algorithm.rsalg import rsa_decrypt, rsa_encrypt
  18. from utils.error.errno_utils import MarketCodeStatusCode, UserStatusCode
  19. from utils.redis.connect import r
  20. WECHAT = settings.WECHAT
  21. # CIPHER_ALGORITHM = ('CAESAR', 'B64', 'RSA')
  22. CIPHER_ALGORITHM = ('CAESAR', )
  23. CIPHER_PREFIX = {
  24. 'CAESAR': '0',
  25. 'B64': '1',
  26. 'RSA': '2',
  27. }
  28. @logit(res=True)
  29. def encrypt(request):
  30. plaintext = request.POST.get('plaintext', '')
  31. optor_id = request.POST.get('optor_id', '')
  32. marketcode = request.POST.get('marketcode', '')
  33. # brand_id#model_id#distributor_id#sn#time
  34. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  35. brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  36. mieli, created_at = MchInfoEncryptLogInfo.objects.get_or_create(plaintext=plaintext)
  37. if marketcode:
  38. if created_at or not mieli.code:
  39. with transaction.atomic():
  40. marketcode = MarketCodeInfo.objects.select_for_update().filter(has_used=False).first()
  41. if not marketcode:
  42. return response(MarketCodeStatusCode.MARKET_CODE_NOT_FOUND)
  43. marketcode.has_used = True
  44. marketcode.save()
  45. mieli.application_id = marketcode.application_id
  46. mieli.code = marketcode.code
  47. mieli.code_url = marketcode.code_url
  48. mieli.brand_pk = brand_pk
  49. mieli.model_pk = model_pk
  50. mieli.distributor_pk = distributor_pk
  51. mieli.sn = sn
  52. mieli.operator_id = optor_id
  53. mieli.save()
  54. return response(200, data={
  55. 'ciphertext': mieli.code_url,
  56. })
  57. if created_at:
  58. alg = random.choice(CIPHER_ALGORITHM)
  59. if alg == 'CAESAR':
  60. ciphertext = caesar_encrypt(plaintext)
  61. elif alg == 'B64':
  62. ciphertext = b64_encrypt(plaintext)
  63. elif alg == 'RSA':
  64. ciphertext = rsa_encrypt(plaintext)
  65. else:
  66. ciphertext = plaintext
  67. mieli.alg = alg
  68. mieli.ciphertext = ciphertext
  69. mieli.brand_pk = brand_pk
  70. mieli.model_pk = model_pk
  71. mieli.distributor_pk = distributor_pk
  72. mieli.sn = sn
  73. mieli.operator_id = optor_id
  74. mieli.save()
  75. return response(200, data={
  76. 'ciphertext': u'{prefix}+{cipherlen}+{ciphertext}'.format(
  77. prefix=CIPHER_PREFIX.get(mieli.alg, ''),
  78. cipherlen=len(mieli.ciphertext),
  79. ciphertext=mieli.ciphertext,
  80. ),
  81. })
  82. @logit(res=True)
  83. def decrypt(request, v='v2'):
  84. ciphertext = request.POST.get('ciphertext', '')
  85. prefix, cipherlen, ciphertext = ciphertext.split('+', 2)
  86. ciphertext = ciphertext[:int(cipherlen)]
  87. if prefix == CIPHER_PREFIX['CAESAR']:
  88. plaintext = caesar_decrypt(ciphertext)
  89. elif prefix == CIPHER_PREFIX['B64']:
  90. plaintext = b64_decrypt(ciphertext)
  91. elif prefix == CIPHER_PREFIX['RSA']:
  92. plaintext = rsa_decrypt(ciphertext)
  93. else:
  94. plaintext = ciphertext
  95. # brand_id#model_id#distributor_id#sn#time
  96. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  97. brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  98. try:
  99. brand = BrandInfo.objects.get(pk=brand_pk)
  100. except BrandInfo.DoesNotExist:
  101. brand = None
  102. try:
  103. model = ModelInfo.objects.get(pk=model_pk)
  104. except ModelInfo.DoesNotExist:
  105. model = None
  106. mdli, created_at = MchInfoDecryptLogInfo.objects.get_or_create(ciphertext=ciphertext, defaults={
  107. 'brand_pk': brand_pk,
  108. 'model_pk': model_pk,
  109. 'distributor_pk': distributor_pk,
  110. 'sn': sn,
  111. 'decrypt_count': 1,
  112. })
  113. if not created_at:
  114. mdli.decrypt_count += 1
  115. mdli.save()
  116. # 弃用老版本的劵形式,和会员系统统一
  117. if v == 'v1':
  118. has_unexpired_activity = False
  119. coupon_infos = {}
  120. else:
  121. activities = ActivityInfo.objects.filter(status=True).order_by('-pk')
  122. coupon_infos = [act.coupon_info3 for act in activities if act and act.has_unexpired_activity(model.model_uni_name)]
  123. return response(200, data={
  124. 'plaintext': plaintext,
  125. 'logo_url': brand.brand_logo_url if brand else '',
  126. 'model_imgs': model.images if model else [],
  127. 'code_version': 1,
  128. 'goodsInfo': {
  129. 'BrandID': brand_pk,
  130. 'Brand': brand.brand_name if brand else '',
  131. 'ModelID': model_pk,
  132. 'Model': (model.model_full_name or model.model_name) if model else '',
  133. 'DistributorID': distributor_pk,
  134. 'SerialNo': sn,
  135. 'img': model.imgdata1 if model else '',
  136. 'img2': model.imgdata if model else {},
  137. },
  138. 'has_unexpired_activity': False,
  139. 'coupon_infos': coupon_infos,
  140. })
  141. @logit(res=True)
  142. def decrypt2(request, v='v2'):
  143. code_ticket = request.POST.get('code_ticket', '')
  144. code = request.POST.get('code', '')
  145. user_id = request.POST.get('user_id', '')
  146. if code_ticket and user_id:
  147. try:
  148. user = UserInfo.objects.get(user_id=user_id)
  149. except UserInfo.DoesNotExist:
  150. return response(UserStatusCode.USER_NOT_FOUND)
  151. wxcfg = WECHAT.get('JSAPI', {})
  152. appid = wxcfg.get('appID')
  153. secret = wxcfg.get('appsecret')
  154. code_info = tickettocode(code_ticket=code_ticket, openid=user.openid_miniapp, appid=appid, secret=secret, token=None, storage=RedisStorage(r))
  155. code = code_info.get('code', '')
  156. try:
  157. mieli = MchInfoEncryptLogInfo.objects.get(code=code)
  158. except MchInfoEncryptLogInfo.DoesNotExist:
  159. return response()
  160. else:
  161. code = re.sub(r'http://|https://', '', code)
  162. try:
  163. mieli = MchInfoEncryptLogInfo.objects.get(code_url=code)
  164. except MchInfoEncryptLogInfo.DoesNotExist:
  165. return response()
  166. plaintext = mieli.plaintext
  167. # brand_id#model_id#distributor_id#sn#time
  168. # AAAA#AAAAAA#AAAAA#AAAAAAAAAAAAAA#180224
  169. brand_pk, model_pk, distributor_pk, sn, time = plaintext.split('#')
  170. try:
  171. brand = BrandInfo.objects.get(pk=brand_pk)
  172. except BrandInfo.DoesNotExist:
  173. brand = None
  174. try:
  175. model = ModelInfo.objects.get(pk=model_pk)
  176. except ModelInfo.DoesNotExist:
  177. model = None
  178. mdli, created_at = MchInfoDecryptLogInfo.objects.get_or_create(
  179. application_id=mieli.application_id,
  180. code=mieli.code,
  181. code_url=mieli.code_url,
  182. defaults={
  183. 'brand_pk': brand_pk,
  184. 'model_pk': model_pk,
  185. 'distributor_pk': distributor_pk,
  186. 'sn': sn,
  187. 'decrypt_count': 1,
  188. }
  189. )
  190. if not created_at:
  191. mdli.decrypt_count += 1
  192. mdli.save()
  193. if v == 'v1':
  194. has_unexpired_activity = False
  195. coupon_infos = {}
  196. else:
  197. activities = ActivityInfo.objects.filter(status=True).order_by('-pk')
  198. coupon_infos = [act.coupon_info3 for act in activities if act.has_unexpired_activity(model.model_uni_name)]
  199. return response(200, data={
  200. 'plaintext': plaintext,
  201. 'logo_url': brand.brand_logo_url if brand else '',
  202. 'model_imgs': model.images if model else [],
  203. 'code_version': mieli.version,
  204. 'goodsInfo': {
  205. 'BrandID': brand_pk,
  206. 'Brand': brand.brand_name if brand else '',
  207. 'ModelID': model_pk,
  208. 'Model': (model.model_full_name or model.model_name) if model else '',
  209. 'DistributorID': distributor_pk,
  210. 'SerialNo': sn,
  211. 'img': model.imgdata1 if model else '',
  212. 'img2': model.imgdata if model else {},
  213. },
  214. 'has_unexpired_activity': False,
  215. 'coupon_infos': coupon_infos,
  216. })