Lottery.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: langer
  7. @software: PyCharm
  8. @DATE: 2019/9/26 16:24
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: Lottery.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. import json
  15. import time
  16. from django.views.generic import TemplateView
  17. from model.models import phoneNumModel
  18. from object.AliSmsObject import AliSmsObject
  19. from object.RedisObject import RedisObject
  20. from object.ResponseObject import ResponseObject
  21. from object.TokenObject import TokenObject
  22. from service.CommonService import CommonService
  23. from django.db.models import Q
  24. '''
  25. http://192.168.136.40:7724/lottery/authcode?phone=13119657713
  26. http://192.168.136.40:7724/lottery/login?phone=13119657713&authcode=xxxxxx
  27. http://192.168.136.40:7724/lottery/draw?token=xx
  28. http://192.168.136.40:7724/lottery/setAddr?token=xx&addr=地址
  29. http://192.168.136.40:7724/lottery/index?token=xx&addr=地址
  30. '''
  31. class AuthCodeView(TemplateView):
  32. def post(self, request, *args, **kwargs):
  33. request.encoding = 'utf-8'
  34. request_dict = json.loads(request.body.decode('utf-8'))
  35. return self.validate(request_dict)
  36. def get(self, request, *args, **kwargs):
  37. request.encoding = 'utf-8'
  38. request_dict = request.GET
  39. return self.validate(request_dict)
  40. def validate(self, request_dict):
  41. response = ResponseObject()
  42. phone = request_dict.get('phone', None)
  43. if phone:
  44. import re
  45. # ret = re.match(r"1[35678]\d{9}", tel)
  46. # 由于手机号位数大于11位也能匹配成功,所以修改如下:
  47. ret = re.match(r"^1[35678]\d{9}$", phone)
  48. if ret:
  49. reds = RedisObject()
  50. phone_redis_key = '{phone}lottery_phone'.format(phone=phone)
  51. lottery_phone = reds.get_data(phone_redis_key)
  52. if lottery_phone:
  53. return response.json(301)
  54. identifyingCode = CommonService.RandomStr(6, True)
  55. aliSms = AliSmsObject()
  56. sign_ms = 'Ansjer'
  57. res = aliSms.send_code_sms(phone=phone, code=identifyingCode, sign_name=sign_ms,
  58. temp_msg='SMS_151600991')
  59. # print(res)
  60. if res["Code"] == "OK":
  61. if reds.set_data(key=phone_redis_key, val=identifyingCode, expire=300) is not True:
  62. # if reds.set_data(key=phone + '_identifyingCode', val=identifyingCode, expire=60) is not True:
  63. return response.json(10, '生成缓存系统错误')
  64. return response.json(0)
  65. else:
  66. return response.json(10, res["Message"])
  67. else:
  68. return response.json(300)
  69. else:
  70. return response.json(444)
  71. class loginView(TemplateView):
  72. def post(self, request, *args, **kwargs):
  73. request.encoding = 'utf-8'
  74. request_dict = json.loads(request.body.decode('utf-8'))
  75. return self.validate(request_dict)
  76. def get(self, request, *args, **kwargs):
  77. request.encoding = 'utf-8'
  78. request_dict = request.GET
  79. return self.validate(request_dict)
  80. def validate(self, request_dict):
  81. response = ResponseObject()
  82. phone = request_dict.get('phone', None)
  83. authcode = request_dict.get('authcode', None)
  84. if phone and authcode:
  85. reds = RedisObject()
  86. phone_redis_key = '{phone}lottery_phone'.format(phone=phone)
  87. lottery_phone = reds.get_data(phone_redis_key)
  88. if lottery_phone is not False:
  89. if authcode == lottery_phone:
  90. user = phoneNumModel.objects.filter(phone=phone)
  91. tko = TokenObject()
  92. res = tko.generate({'userID': phone})
  93. if user.exists():
  94. reds.del_data(phone_redis_key)
  95. return response.json(0, res)
  96. else:
  97. nowTime = int(time.time())
  98. add_data = {
  99. 'phone': phone,
  100. 'addTime': nowTime,
  101. 'updTime': nowTime
  102. }
  103. try:
  104. phoneNumModel.objects.create(**add_data)
  105. except:
  106. return response.json(404)
  107. else:
  108. reds.del_data(phone_redis_key)
  109. return response.json(0, res)
  110. else:
  111. return response.json(409)
  112. else:
  113. return response.json(407)
  114. else:
  115. return response.json(444)
  116. class drawView(TemplateView):
  117. def post(self, request, *args, **kwargs):
  118. request.encoding = 'utf-8'
  119. request_dict = json.loads(request.body.decode('utf-8'))
  120. return self.validate(request_dict)
  121. def get(self, request, *args, **kwargs):
  122. request.encoding = 'utf-8'
  123. request_dict = request.GET
  124. return self.validate(request_dict)
  125. def validate(self, request_dict):
  126. response = ResponseObject()
  127. token = request_dict.get('token', None)
  128. tko = TokenObject(token=token)
  129. if tko.code == 0:
  130. phone = tko.userID
  131. qs = phoneNumModel.objects.filter(phone=phone, status=0)
  132. if qs.exists():
  133. test = randomMachine()
  134. # status c611=>1 , c612=>2 ,不中间=>3
  135. # 权重
  136. test.setWeight({1: 1, 2: 1, 3: 50})
  137. has_chow = test.drawing()
  138. user = phoneNumModel.objects.filter(phone=phone)
  139. count_1 = phoneNumModel.objects.filter(status=1).count()
  140. count_2 = phoneNumModel.objects.filter(status=2).count()
  141. print(has_chow)
  142. if has_chow == count_1 and count_1 == 5:
  143. user.update(status=3)
  144. return response.json(0, 3)
  145. if has_chow == count_2 and count_2 == 5:
  146. user.update(status=3)
  147. return response.json(0, 3)
  148. user.update(status=has_chow)
  149. return response.json(0, {'status': has_chow})
  150. else:
  151. # 您已抽过奖
  152. return response.json(233)
  153. else:
  154. return response.json(tko.code)
  155. class indexView(TemplateView):
  156. def post(self, request, *args, **kwargs):
  157. request.encoding = 'utf-8'
  158. request_dict = json.loads(request.body.decode('utf-8'))
  159. return self.validate(request_dict)
  160. def get(self, request, *args, **kwargs):
  161. request.encoding = 'utf-8'
  162. request_dict = request.GET
  163. return self.validate(request_dict)
  164. def validate(self, request_dict):
  165. response = ResponseObject()
  166. token = request_dict.get('token', None)
  167. tko = TokenObject(token=token)
  168. if tko.code == 0:
  169. phone = tko.userID
  170. # 当前用户信息
  171. qs = phoneNumModel.objects.filter(phone=phone).values('status', 'addr')
  172. # 获取所有中奖名单
  173. phone_list = phoneNumModel.objects.filter(~Q(status=0)).values_list('phone', flat=True)
  174. if qs.exists():
  175. status = qs[0]['status']
  176. # 中奖详情
  177. # lottery_dict = {
  178. #
  179. # }
  180. return response.json(0, {'status': status, 'phone_list': list(phone_list), 'user': phone,
  181. 'addr': qs[0]['addr']})
  182. else:
  183. return response.json(0, '您已抽过奖了')
  184. else:
  185. return response.json(tko.code)
  186. class setAddrView(TemplateView):
  187. def post(self, request, *args, **kwargs):
  188. request.encoding = 'utf-8'
  189. request_dict = json.loads(request.body.decode('utf-8'))
  190. return self.validate(request_dict)
  191. def get(self, request, *args, **kwargs):
  192. request.encoding = 'utf-8'
  193. request_dict = request.GET
  194. return self.validate(request_dict)
  195. def validate(self, request_dict):
  196. response = ResponseObject()
  197. token = request_dict.get('token', None)
  198. addr = request_dict.get('addr', None)
  199. tko = TokenObject(token=token)
  200. if tko.code == 0:
  201. phone = tko.userID
  202. qs = phoneNumModel.objects.filter(phone=phone)
  203. qs = qs.filter(Q(status=1) | Q(status=2))
  204. if qs.exists():
  205. qs.update(addr=addr)
  206. return response.json(0)
  207. else:
  208. return response.json(0, '您未中奖')
  209. else:
  210. return response.json(tko.code)
  211. class randomMachine(object):
  212. import random as rd
  213. def setWeight(self, weight):
  214. self.weight = weight
  215. self.chanceList = []
  216. for k, v in self.weight.items():
  217. for t in range(v):
  218. self.chanceList.append(k)
  219. def drawing(self):
  220. r = self.rd.randrange(0, len(self.chanceList)) # 随机数
  221. # print("随机数 : ", r)
  222. has_chow = self.chanceList.pop(r)
  223. # print(has_chow)
  224. return has_chow
  225. def graphicsUI(self):
  226. pass
  227. def start(self):
  228. pass