|
@@ -0,0 +1,234 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+# -*- coding: utf-8 -*-
|
|
|
+"""
|
|
|
+@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
|
|
|
+@AUTHOR: ASJRD018
|
|
|
+@NAME: langer
|
|
|
+@software: PyCharm
|
|
|
+@DATE: 2019/9/26 16:24
|
|
|
+@Version: python3.6
|
|
|
+@MODIFY DECORD:ansjer dev
|
|
|
+@file: Lottery.py
|
|
|
+@Contact: chanjunkai@163.com
|
|
|
+"""
|
|
|
+import json
|
|
|
+import time
|
|
|
+
|
|
|
+from django.views.generic import TemplateView
|
|
|
+
|
|
|
+from model.models import phoneNumModel
|
|
|
+from object.AliSmsObject import AliSmsObject
|
|
|
+from object.RedisObject import RedisObject
|
|
|
+from object.ResponseObject import ResponseObject
|
|
|
+from object.TokenObject import TokenObject
|
|
|
+from service.CommonService import CommonService
|
|
|
+from django.db.models import Q
|
|
|
+
|
|
|
+
|
|
|
+class AuthCodeView(TemplateView):
|
|
|
+
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = json.loads(request.body.decode('utf-8'))
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = request.GET
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def validate(self, request_dict):
|
|
|
+ response = ResponseObject()
|
|
|
+ phone = request_dict.get('phone', None)
|
|
|
+ if phone:
|
|
|
+ import re
|
|
|
+ # ret = re.match(r"1[35678]\d{9}", tel)
|
|
|
+ # 由于手机号位数大于11位也能匹配成功,所以修改如下:
|
|
|
+ ret = re.match(r"^1[35678]\d{9}$", phone)
|
|
|
+ if ret:
|
|
|
+ reds = RedisObject()
|
|
|
+ lottery_phone = reds.get_data('lottery_phone')
|
|
|
+ if lottery_phone:
|
|
|
+ return response.json(301)
|
|
|
+ identifyingCode = CommonService.RandomStr(6, True)
|
|
|
+ aliSms = AliSmsObject()
|
|
|
+ sign_ms = 'Ansjer'
|
|
|
+ res = aliSms.send_code_sms(phone=phone, code=identifyingCode, sign_name=sign_ms,
|
|
|
+ temp_msg='SMS_151600991')
|
|
|
+ # print(res)
|
|
|
+ if res["Code"] == "OK":
|
|
|
+ if reds.set_data(key=lottery_phone, val=identifyingCode, expire=300) is not True:
|
|
|
+ # if reds.set_data(key=phone + '_identifyingCode', val=identifyingCode, expire=60) is not True:
|
|
|
+
|
|
|
+ return response.json(10, '生成缓存系统错误')
|
|
|
+ return response.json(0)
|
|
|
+ else:
|
|
|
+ return response.json(10, res["Message"])
|
|
|
+ else:
|
|
|
+ return response.json(300)
|
|
|
+ else:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+
|
|
|
+class loginView(TemplateView):
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = json.loads(request.body.decode('utf-8'))
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = request.GET
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def validate(self, request_dict):
|
|
|
+ response = ResponseObject()
|
|
|
+ phone = request_dict.get('phone', None)
|
|
|
+ authcode = request_dict.get('authcode', None)
|
|
|
+ if phone and authcode:
|
|
|
+ reds = RedisObject()
|
|
|
+ lottery_phone = reds.get_data('lottery_phone')
|
|
|
+ if lottery_phone is not False:
|
|
|
+ if authcode == lottery_phone:
|
|
|
+ nowTime = int(time.time())
|
|
|
+ add_data = {
|
|
|
+ 'phone': phone,
|
|
|
+ 'addTime': nowTime,
|
|
|
+ 'updTime': nowTime
|
|
|
+ }
|
|
|
+ try:
|
|
|
+ phoneNumModel.objects.create(**add_data)
|
|
|
+ except:
|
|
|
+ return response.json(404)
|
|
|
+ else:
|
|
|
+ tko = TokenObject()
|
|
|
+ res = tko.generate({'userID': phone})
|
|
|
+ return response.json(0, res)
|
|
|
+ else:
|
|
|
+ return response.json(409)
|
|
|
+ else:
|
|
|
+ return response.json(407)
|
|
|
+ else:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+
|
|
|
+class drawView(TemplateView):
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = json.loads(request.body.decode('utf-8'))
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = request.GET
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def validate(self, request_dict):
|
|
|
+ response = ResponseObject()
|
|
|
+ token = request_dict.get('token', None)
|
|
|
+ tko = TokenObject(token=token)
|
|
|
+ if tko.code == 0:
|
|
|
+ phone = tko.userID
|
|
|
+ qs = phoneNumModel.objects.filter(phone=phone, status=0)
|
|
|
+ if qs.exists():
|
|
|
+ test = randomMachine()
|
|
|
+ # status c611=>1 , c612=>2 ,不中间=>3
|
|
|
+ test.setWeight({1: 1, 2: 1, 3: 50})
|
|
|
+ has_chow = test.drawing()
|
|
|
+ count_1 = phoneNumModel.objects.filter(status=1).count()
|
|
|
+ count_2 = phoneNumModel.objects.filter(status=2).count()
|
|
|
+ if has_chow == count_1 and count_1 == 5:
|
|
|
+ return response.json(0, 3)
|
|
|
+ if has_chow == count_2 and count_2 == 5:
|
|
|
+ return response.json(0, 3)
|
|
|
+ return response.json(0, has_chow)
|
|
|
+ else:
|
|
|
+ return response.json(0, '您已抽过奖了')
|
|
|
+ else:
|
|
|
+ return response.json(tko.code)
|
|
|
+
|
|
|
+
|
|
|
+class indexView(TemplateView):
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = json.loads(request.body.decode('utf-8'))
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = request.GET
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def validate(self, request_dict):
|
|
|
+ response = ResponseObject()
|
|
|
+ token = request_dict.get('token', None)
|
|
|
+ tko = TokenObject(token=token)
|
|
|
+ if tko.code == 0:
|
|
|
+ phone = tko.userID
|
|
|
+ qs = phoneNumModel.objects.filter(phone=phone).values('status')
|
|
|
+ # 获取所有中奖名单
|
|
|
+ phone_list = phoneNumModel.objects.filter(~Q(status=0)).values_list('phone', flat=True)
|
|
|
+ if qs.exists():
|
|
|
+ status = qs[0]['status']
|
|
|
+ # 中奖详情
|
|
|
+ # lottery_dict = {
|
|
|
+ #
|
|
|
+ # }
|
|
|
+ return response.json(0, {'status': status, 'phone_list': list(phone_list), 'user': phone})
|
|
|
+ else:
|
|
|
+ return response.json(0, '您已抽过奖了')
|
|
|
+ else:
|
|
|
+ return response.json(tko.code)
|
|
|
+
|
|
|
+
|
|
|
+class setAddrView(TemplateView):
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = json.loads(request.body.decode('utf-8'))
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = request.GET
|
|
|
+ return self.validate(request_dict)
|
|
|
+
|
|
|
+ def validate(self, request_dict):
|
|
|
+ response = ResponseObject()
|
|
|
+ token = request_dict.get('token', None)
|
|
|
+ addr = request_dict.get('addr', None)
|
|
|
+ tko = TokenObject(token=token)
|
|
|
+ if tko.code == 0:
|
|
|
+ phone = tko.userID
|
|
|
+ qs = phoneNumModel.objects.filter(phone=phone)
|
|
|
+ qs = qs.filter(Q(status=1) | Q(status=2))
|
|
|
+
|
|
|
+ if qs.exists():
|
|
|
+ qs.update(addr=addr)
|
|
|
+ return response.json(0)
|
|
|
+ else:
|
|
|
+ return response.json(0, '您未中奖')
|
|
|
+ else:
|
|
|
+ return response.json(tko.code)
|
|
|
+
|
|
|
+
|
|
|
+class randomMachine(object):
|
|
|
+ import random as rd
|
|
|
+ def setWeight(self, weight):
|
|
|
+ self.weight = weight
|
|
|
+ self.chanceList = []
|
|
|
+ for k, v in self.weight.items():
|
|
|
+ for t in range(v):
|
|
|
+ self.chanceList.append(k)
|
|
|
+
|
|
|
+ def drawing(self):
|
|
|
+ r = self.rd.randrange(0, len(self.chanceList)) # 随机数
|
|
|
+ # print("随机数 : ", r)
|
|
|
+ has_chow = self.chanceList.pop(r)
|
|
|
+ # print(has_chow)
|
|
|
+ return has_chow
|
|
|
+
|
|
|
+ def graphicsUI(self):
|
|
|
+ pass
|
|
|
+
|
|
|
+ def start(self):
|
|
|
+ pass
|