chenjunkai há 5 anos atrás
pai
commit
6e658077b6
4 ficheiros alterados com 53 adições e 23 exclusões
  1. 47 21
      controller/Lottery.py
  2. 2 0
      langer/urls.py
  3. 2 2
      object/RedisObject.py
  4. 2 0
      object/ResponseObject.py

+ 47 - 21
controller/Lottery.py

@@ -24,6 +24,14 @@ from object.TokenObject import TokenObject
 from service.CommonService import CommonService
 from django.db.models import Q
 
+'''
+   http://192.168.136.40:7724/lottery/authcode?phone=13119657713
+   http://192.168.136.40:7724/lottery/login?phone=13119657713&authcode=xxxxxx
+   http://192.168.136.40:7724/lottery/draw?token=xx
+   http://192.168.136.40:7724/lottery/setAddr?token=xx&addr=地址
+   http://192.168.136.40:7724/lottery/index?token=xx&addr=地址
+   '''
+
 
 class AuthCodeView(TemplateView):
 
@@ -47,7 +55,8 @@ class AuthCodeView(TemplateView):
             ret = re.match(r"^1[35678]\d{9}$", phone)
             if ret:
                 reds = RedisObject()
-                lottery_phone = reds.get_data('lottery_phone')
+                phone_redis_key = '{phone}lottery_phone'.format(phone=phone)
+                lottery_phone = reds.get_data(phone_redis_key)
                 if lottery_phone:
                     return response.json(301)
                 identifyingCode = CommonService.RandomStr(6, True)
@@ -57,7 +66,7 @@ class AuthCodeView(TemplateView):
                                            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_redis_key, 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, '生成缓存系统错误')
@@ -87,23 +96,30 @@ class loginView(TemplateView):
         authcode = request_dict.get('authcode', None)
         if phone and authcode:
             reds = RedisObject()
-            lottery_phone = reds.get_data('lottery_phone')
+            phone_redis_key = '{phone}lottery_phone'.format(phone=phone)
+            lottery_phone = reds.get_data(phone_redis_key)
             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})
+                    user = phoneNumModel.objects.filter(phone=phone)
+                    tko = TokenObject()
+                    res = tko.generate({'userID': phone})
+                    if user.exists():
+                        reds.del_data(phone_redis_key)
                         return response.json(0, res)
+                    else:
+                        nowTime = int(time.time())
+                        add_data = {
+                            'phone': phone,
+                            'addTime': nowTime,
+                            'updTime': nowTime
+                        }
+                        try:
+                            phoneNumModel.objects.create(**add_data)
+                        except:
+                            return response.json(404)
+                        else:
+                            reds.del_data(phone_redis_key)
+                            return response.json(0, res)
                 else:
                     return response.json(409)
             else:
@@ -133,17 +149,25 @@ class drawView(TemplateView):
             if qs.exists():
                 test = randomMachine()
                 # status c611=>1 , c612=>2 ,不中间=>3
-                test.setWeight({1: 1, 2: 1, 3: 50})
+                # 权重
+                test.setWeight({1: 1, 2: 1, 3: 3})
                 has_chow = test.drawing()
+                user = phoneNumModel.objects.filter(phone=phone)
+
                 count_1 = phoneNumModel.objects.filter(status=1).count()
                 count_2 = phoneNumModel.objects.filter(status=2).count()
+                print(has_chow)
                 if has_chow == count_1 and count_1 == 5:
+                    user.update(status=3)
                     return response.json(0, 3)
                 if has_chow == count_2 and count_2 == 5:
+                    user.update(status=3)
                     return response.json(0, 3)
-                return response.json(0, has_chow)
+                user.update(status=has_chow)
+                return response.json(0, {'status': has_chow})
             else:
-                return response.json(0, '您已抽过奖了')
+                # 您已抽过奖
+                return response.json(233)
         else:
             return response.json(tko.code)
 
@@ -165,7 +189,8 @@ class indexView(TemplateView):
         tko = TokenObject(token=token)
         if tko.code == 0:
             phone = tko.userID
-            qs = phoneNumModel.objects.filter(phone=phone).values('status')
+            # 当前用户信息
+            qs = phoneNumModel.objects.filter(phone=phone).values('status', 'addr')
             # 获取所有中奖名单
             phone_list = phoneNumModel.objects.filter(~Q(status=0)).values_list('phone', flat=True)
             if qs.exists():
@@ -174,7 +199,8 @@ class indexView(TemplateView):
                 # lottery_dict = {
                 #
                 # }
-                return response.json(0, {'status': status, 'phone_list': list(phone_list), 'user': phone})
+                return response.json(0, {'status': status, 'phone_list': list(phone_list), 'user': phone,
+                                         'addr': qs[0]['addr']})
             else:
                 return response.json(0, '您已抽过奖了')
         else:

+ 2 - 0
langer/urls.py

@@ -27,8 +27,10 @@ urlpatterns = [
     path('asimg/cycleImgView', AsImg.cycleImgView.as_view()),
 
     # 抽奖模块
+
     path('lottery/authcode', Lottery.AuthCodeView.as_view()),
     path('lottery/login', Lottery.loginView.as_view()),
     path('lottery/draw', Lottery.drawView.as_view()),
     path('lottery/setAddr', Lottery.setAddrView.as_view()),
+    path('lottery/index', Lottery.indexView.as_view()),
 ]

+ 2 - 2
object/RedisObject.py

@@ -19,8 +19,8 @@ import redis
 '''
 db=3  -> 统计在线人数用
 '''
-SERVER_HOST = 'localhost'
-# SERVER_HOST = '192.168.136.45'
+# SERVER_HOST = 'localhost'
+SERVER_HOST = '192.168.136.45'
 
 
 class RedisObject:

+ 2 - 0
object/ResponseObject.py

@@ -10,6 +10,8 @@ class ResponseObject(object):
     def json(self, code, res={}, extra={}):
         msg_data = {
             0: 'Success',
+            10: res,
+            233: '您已抽过奖',
             300: '请输入正确的手机号码',
             301: '验证码已发过,请等待',
             309: 'Please ReLogin! errmsg token',