Lottery.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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, '发送验证码太频繁,或请更换手机')
  67. return response.json(10, res["Message"])
  68. else:
  69. return response.json(300)
  70. else:
  71. return response.json(444)
  72. class loginView(TemplateView):
  73. def post(self, request, *args, **kwargs):
  74. request.encoding = 'utf-8'
  75. request_dict = json.loads(request.body.decode('utf-8'))
  76. return self.validate(request_dict)
  77. def get(self, request, *args, **kwargs):
  78. request.encoding = 'utf-8'
  79. request_dict = request.GET
  80. return self.validate(request_dict)
  81. def validate(self, request_dict):
  82. response = ResponseObject()
  83. phone = request_dict.get('phone', None)
  84. authcode = request_dict.get('authcode', None)
  85. if phone and authcode:
  86. reds = RedisObject()
  87. phone_redis_key = '{phone}lottery_phone'.format(phone=phone)
  88. lottery_phone = reds.get_data(phone_redis_key)
  89. if lottery_phone is not False:
  90. if authcode == lottery_phone:
  91. user = phoneNumModel.objects.filter(phone=phone)
  92. tko = TokenObject()
  93. res = tko.generate({'userID': phone})
  94. if user.exists():
  95. reds.del_data(phone_redis_key)
  96. return response.json(0, res)
  97. else:
  98. nowTime = int(time.time())
  99. add_data = {
  100. 'phone': phone,
  101. 'addTime': nowTime,
  102. 'updTime': nowTime
  103. }
  104. try:
  105. phoneNumModel.objects.create(**add_data)
  106. except:
  107. return response.json(404)
  108. else:
  109. reds.del_data(phone_redis_key)
  110. return response.json(0, res)
  111. else:
  112. return response.json(409)
  113. else:
  114. return response.json(407)
  115. else:
  116. return response.json(444)
  117. class drawView(TemplateView):
  118. def post(self, request, *args, **kwargs):
  119. request.encoding = 'utf-8'
  120. request_dict = json.loads(request.body.decode('utf-8'))
  121. return self.validate(request_dict)
  122. def get(self, request, *args, **kwargs):
  123. request.encoding = 'utf-8'
  124. request_dict = request.GET
  125. return self.validate(request_dict)
  126. def validate(self, request_dict):
  127. response = ResponseObject()
  128. token = request_dict.get('token', None)
  129. tko = TokenObject(token=token)
  130. if tko.code == 0:
  131. phone = tko.userID
  132. qs = phoneNumModel.objects.filter(phone=phone, status=0)
  133. # if True:
  134. if qs.exists():
  135. test = randomMachine()
  136. # status c611=>1 , c612=>2 ,不中间=>3
  137. # 权重
  138. test.setWeight({1: 1, 2: 1, 3: 1})
  139. has_chow = test.drawing()
  140. user = phoneNumModel.objects.filter(phone=phone)
  141. count_1 = phoneNumModel.objects.filter(status=1).count()
  142. count_2 = phoneNumModel.objects.filter(status=2).count()
  143. print('has_chow')
  144. print(has_chow)
  145. if has_chow == 1 and count_1 == 5:
  146. print('count1')
  147. print(count_1)
  148. user.update(status=3)
  149. return response.json(0, {'status': 3})
  150. if has_chow == 2 and count_2 == 5:
  151. print('count2')
  152. print(count_2)
  153. user.update(status=3)
  154. return response.json(0, {'status': 3})
  155. user.update(status=has_chow)
  156. return response.json(0, {'status': has_chow})
  157. else:
  158. # 您已抽过奖
  159. return response.json(233)
  160. else:
  161. return response.json(tko.code)
  162. class indexView(TemplateView):
  163. def post(self, request, *args, **kwargs):
  164. request.encoding = 'utf-8'
  165. request_dict = json.loads(request.body.decode('utf-8'))
  166. return self.validate(request_dict)
  167. def get(self, request, *args, **kwargs):
  168. request.encoding = 'utf-8'
  169. request_dict = request.GET
  170. return self.validate(request_dict)
  171. def validate(self, request_dict):
  172. response = ResponseObject()
  173. token = request_dict.get('token', None)
  174. tko = TokenObject(token=token)
  175. if tko.code == 0:
  176. phone = tko.userID
  177. # 当前用户信息
  178. qs = phoneNumModel.objects.filter(phone=phone).values('status', 'addr')
  179. # 获取所有中奖名单
  180. phone_list = phoneNumModel.objects.filter(~Q(status=0)).values_list('phone', flat=True)
  181. if qs.exists():
  182. status = qs[0]['status']
  183. # 中奖详情
  184. # lottery_dict = {
  185. #
  186. # }
  187. return response.json(0, {'status': status, 'phone_list': list(phone_list), 'user': phone,
  188. 'addr': qs[0]['addr']})
  189. else:
  190. return response.json(10, '您已抽过奖了')
  191. else:
  192. return response.json(tko.code)
  193. class setAddrView(TemplateView):
  194. def post(self, request, *args, **kwargs):
  195. request.encoding = 'utf-8'
  196. request_dict = json.loads(request.body.decode('utf-8'))
  197. return self.validate(request_dict)
  198. def get(self, request, *args, **kwargs):
  199. request.encoding = 'utf-8'
  200. request_dict = request.GET
  201. return self.validate(request_dict)
  202. def validate(self, request_dict):
  203. response = ResponseObject()
  204. token = request_dict.get('token', None)
  205. addr = request_dict.get('addr', None)
  206. tko = TokenObject(token=token)
  207. if tko.code == 0:
  208. phone = tko.userID
  209. qs = phoneNumModel.objects.filter(phone=phone)
  210. qs = qs.filter(Q(status=1) | Q(status=2))
  211. if qs.exists():
  212. qs.update(addr=addr)
  213. return response.json(0)
  214. else:
  215. return response.json(0, '您未中奖')
  216. else:
  217. return response.json(tko.code)
  218. class randomMachine(object):
  219. import random as rd
  220. def setWeight(self, weight):
  221. self.weight = weight
  222. self.chanceList = []
  223. for k, v in self.weight.items():
  224. for t in range(v):
  225. self.chanceList.append(k)
  226. def drawing(self):
  227. r = self.rd.randrange(0, len(self.chanceList)) # 随机数
  228. # print("随机数 : ", r)
  229. has_chow = self.chanceList.pop(r)
  230. # print(has_chow)
  231. return has_chow
  232. def graphicsUI(self):
  233. pass
  234. def start(self):
  235. pass