Lottery.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. # status c611=>1 , c612=>2 ,不中间=>3
  136. # 权重
  137. test = randomMachine()
  138. # test.setWeight({1: 1, 2: 1, 3: 50})
  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('count_1')
  144. print(count_1)
  145. print('count_2')
  146. print(count_2)
  147. if count_1 >= 5 > count_2:
  148. print('type1')
  149. # 权重组
  150. chow_weight = {2: 1, 3: 25}
  151. test.setWeight(chow_weight)
  152. has_chow = test.drawing()
  153. elif count_1 < 5 <= count_2:
  154. print('type2')
  155. chow_weight = {1: 1, 3: 25}
  156. test.setWeight(chow_weight)
  157. has_chow = test.drawing()
  158. elif count_2 == 5 == count_2:
  159. print('type3')
  160. has_chow = 3
  161. else:
  162. print('type4')
  163. chow_weight = {1: 1, 2: 1, 3: 50}
  164. test.setWeight(chow_weight)
  165. has_chow = test.drawing()
  166. # 如果抽奖额度满了
  167. print(has_chow)
  168. user.update(status=has_chow)
  169. return response.json(0, {'status': has_chow})
  170. else:
  171. # 您已抽过奖
  172. return response.json(233)
  173. else:
  174. return response.json(tko.code)
  175. class indexView(TemplateView):
  176. def post(self, request, *args, **kwargs):
  177. request.encoding = 'utf-8'
  178. request_dict = json.loads(request.body.decode('utf-8'))
  179. return self.validate(request_dict)
  180. def get(self, request, *args, **kwargs):
  181. request.encoding = 'utf-8'
  182. request_dict = request.GET
  183. return self.validate(request_dict)
  184. def validate(self, request_dict):
  185. response = ResponseObject()
  186. token = request_dict.get('token', None)
  187. tko = TokenObject(token=token)
  188. if tko.code == 0:
  189. phone = tko.userID
  190. # 当前用户信息
  191. qs = phoneNumModel.objects.filter(phone=phone).values('status', 'addr')
  192. # 获取所有中奖名单
  193. phone_list = phoneNumModel.objects.filter(~Q(status=0)).values_list('phone', flat=True)
  194. if qs.exists():
  195. status = qs[0]['status']
  196. # 中奖详情
  197. # lottery_dict = {
  198. #
  199. # }
  200. return response.json(0, {'status': status, 'phone_list': list(phone_list), 'user': phone,
  201. 'addr': qs[0]['addr']})
  202. else:
  203. return response.json(10, '您已抽过奖了')
  204. else:
  205. return response.json(tko.code)
  206. class setAddrView(TemplateView):
  207. def post(self, request, *args, **kwargs):
  208. request.encoding = 'utf-8'
  209. request_dict = json.loads(request.body.decode('utf-8'))
  210. return self.validate(request_dict)
  211. def get(self, request, *args, **kwargs):
  212. request.encoding = 'utf-8'
  213. request_dict = request.GET
  214. return self.validate(request_dict)
  215. def validate(self, request_dict):
  216. response = ResponseObject()
  217. token = request_dict.get('token', None)
  218. addr = request_dict.get('addr', None)
  219. tko = TokenObject(token=token)
  220. if tko.code == 0:
  221. phone = tko.userID
  222. qs = phoneNumModel.objects.filter(phone=phone)
  223. qs = qs.filter(Q(status=1) | Q(status=2))
  224. if qs.exists():
  225. qs.update(addr=addr)
  226. return response.json(0)
  227. else:
  228. return response.json(0, '您未中奖')
  229. else:
  230. return response.json(tko.code)
  231. class randomMachine(object):
  232. import random as rd
  233. def setWeight(self, weight):
  234. self.weight = weight
  235. self.chanceList = []
  236. for k, v in self.weight.items():
  237. for t in range(v):
  238. self.chanceList.append(k)
  239. def drawing(self):
  240. r = self.rd.randrange(0, len(self.chanceList)) # 随机数
  241. # print("随机数 : ", r)
  242. has_chow = self.chanceList.pop(r)
  243. # print(has_chow)
  244. return has_chow
  245. def graphicsUI(self):
  246. pass
  247. def start(self):
  248. pass