allen před 3 roky
rodič
revize
119a25ea4f
1 změnil soubory, kde provedl 83 přidání a 95 odebrání
  1. 83 95
      app/index/controller/Index.php

+ 83 - 95
app/index/controller/Index.php

@@ -80,16 +80,25 @@ class Index extends Controller
         $token = md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_REFERER'] . get_client_ip(0) . $userInfoRes['data']['uid']);
         $user = $userInfoRes['data'];
 
+        $first = 0;
         $userInfo = Db::table('awards_user_info')->where('uid', $user['uid'])->find();
         if (empty($userInfo)) {
             $userAttr = [
                 'uid' => $user['uid'],
                 'portrait' => $user['profile_image_url'],
-                'nickname' => $user['name']
+                'nickname' => $user['name'],
+                'is_share' => 0,
+                'count' => 1,
+                'create_at' => time()
             ];
             if (0 == Db::table('awards_user_info')->insert($userAttr)) {
                 return $this->response(5001, '系统错误,请稍后再试~');
             }
+        } else {
+            $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->count('id');
+            if ($count < $userInfo['count']) {
+                $first = 1;
+            }
         }
 
         // 生成加密用的密钥和向量
@@ -107,41 +116,6 @@ class Index extends Controller
             'uid' => $user['uid'],
         ];
 
-        $first = 0;
-        $date = date('Y-m-d');
-        $userTask = Db::table('awards_user_task')->where('uid', $user['uid'])->where('date', $date)->find();
-        if (empty($userTask)) {
-            // 当天第一次,初始化数据
-            $state = [
-                // 品牌任务完成状态
-                'finish_state' => 0,
-                // 关注子任务状态
-                'follow_state' => 0,
-                // 转发子任务状态
-                'forward_state' => 0,
-                // 查看浏览主页任务状态
-                'view_state' => 0,
-                // 品牌任务完成后是否加票
-                'finish_add_votes' => 0,
-                // 分享完成后是否加票
-                'share_add_votes' => 0,
-                'uid' => $user['uid'],
-                'date' => $date,
-            ];
-            if (0 == Db::table('awards_user_task')->insert($state)) {
-                return $this->response(5001, '系统错误,请稍后再试~');
-            }
-        } else if ($userTask['finish_state'] == 0 && $userTask['share_add_votes'] == 0) {
-            $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->where('date', $date)->count('id');
-            if ($count < 1) {
-                $first = 1;
-            }
-        } else if ($userTask['finish_state'] > 0 && $userTask['share_add_votes'] > 0) {
-            $count = Db::table('awards_user_task_log')->where('uid', $user['uid'])->where('date', $date)->count('id');
-            if ($count < 2) {
-                $first = 1;
-            }
-        }
         // 缓存用户信息1天
         Cache::set('u:' . $token, json_encode($cacheUser), 86400);
         return $this->successResponse([
@@ -160,20 +134,11 @@ class Index extends Controller
     public function checkRole()
     {
         $first = 0;
-        $date = date('Y-m-d');
-        $userTask = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
-        if (empty($userTask)) {
-            return $this->response(403, 'not login.');
-        } else if ($userTask['finish_state'] == 0 && $userTask['share_add_votes'] == 0) {
-            $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->where('date', $date)->count('id');
-            if ($count < 1) {
-                $first = 1;
-            }
-        } else if ($userTask['finish_state'] > 0 && $userTask['share_add_votes'] > 0) {
-            $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->where('date', $date)->count('id');
-            if ($count < 2) {
-                $first = 1;
-            }
+
+        $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
+        $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
+        if ($count < $userInfo['count']) {
+            $first = 1;
         }
 
         return $this->successResponse([
@@ -209,46 +174,23 @@ class Index extends Controller
 
         $date = date('Y-m-d');
 
-        $userTask = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
-
-        if (empty($userTask)) {
+        $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
+        if (empty($userInfo)) {
             return $this->response(403, '没有登录');
         }
 
-        $log = [];
-        if ($userTask['finish_state'] == 0) {
-            $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->where('date', $date)->count('id');
-            if ($count > 0) {
-                return $this->response(5002, '没有参与次数');
-            }
-            $log = [
-                'uid' => Safe::$user['uid'],
-                'date' => $date,
-                'duration' => $duration,
-                'number' => $number,
-                'create_at' => time()
-            ];
-            Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
-                'finish_state' => 1,
-            ]);
-        } else if ($userTask['share_add_votes'] == 1) {
-            $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->where('date', $date)->count('id');
-            if ($count > 1) {
-                return $this->response(5002, '没有参与次数');
-            }
-            $log = [
-                'uid' => Safe::$user['uid'],
-                'date' => $date,
-                'duration' => $duration,
-                'number' => $number,
-                'create_at' => time()
-            ];
-        }
-
-        if (empty($log)) {
+        $count = Db::table('awards_user_task_log')->where('uid', Safe::$user['uid'])->count('id');
+        if ($count >= $userInfo['count']) {
             return $this->response(5002, '没有参与次数');
         }
 
+        $log = [
+            'uid' => Safe::$user['uid'],
+            'date' => $date,
+            'duration' => $duration,
+            'number' => $number,
+            'create_at' => time()
+        ];
         if (0 == Db::table('awards_user_task_log')->insert($log)) {
             return $this->response(5001, '系统错误,请稍后再试~');
         }
@@ -269,19 +211,20 @@ class Index extends Controller
     {
         $date = date('Y-m-d');
 
-        $userTask = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
-
-        if (empty($userTask)) {
+        $userInfo = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->find();
+        if (empty($userInfo)) {
             return $this->response(403, '没有登录');
         }
 
-        if ($userTask['share_add_votes'] > 0) {
-            return $this->response(601, '今日已发布');
+        if ($userInfo['is_share'] > 0) {
+            return $this->response(601, '已发布');
         }
 
         // 更新任务状态
-        $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
-            'share_add_votes' => 1,
+        $nums = Db::table('awards_user_info')->where('uid', Safe::$user['uid'])->update([
+            'is_share' => 1,
+            'count' => 2,
+            'share_at' => time()
         ]);
 
         if ($nums) {
@@ -292,7 +235,7 @@ class Index extends Controller
     }
 
     /**
-     * 排行接口
+     * 头号排行接口
      * @throws FuncNotFoundException
      * @throws ClassNotFoundException
      * @throws DbException
@@ -302,16 +245,61 @@ class Index extends Controller
      */
     public function ranking()
     {
-        $date = date('Y-m-d');
-        $res = Db::table('awards_user_task_log')->alias("l")
+        $ranking = Db::table('awards_user_task_log')->alias("l")
             ->leftJoin('awards_user_info u', 'l.uid = u.uid')
             ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
-            ->where('l.date', $date)
+            ->order('l.number', 'desc')
+            ->order('l.duration', 'asc')
+            ->limit(50)
+            ->select();
+
+        $userInfo = Db::table('awards_user_task_log')->alias('l')
+            ->leftJoin('awards_user_info u', 'l.uid = u.uid')
+            ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
+            ->order('l.number', 'desc')
+            ->order('l.duration', 'asc')
+            ->find();
+
+        $res = [
+            'self' => $userInfo,
+            'ranking' => $ranking
+        ];
+
+        return $this->successResponse($res);
+    }
+
+    /**
+     * 最新排行接口
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
+     */
+    public function newRanking()
+    {
+        $ranking = Db::table('awards_user_task_log')->alias("l")
+            ->leftJoin('awards_user_info u', 'l.uid = u.uid')
+            ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
+            ->order('l.create_at', 'desc')
             ->order('l.number', 'desc')
             ->order('l.duration', 'asc')
             ->limit(15)
             ->select();
 
+        $userInfo = Db::table('awards_user_task_log')->alias('l')
+            ->leftJoin('awards_user_info u', 'l.uid = u.uid')
+            ->field(['u.nickname', 'u.portrait', 'u.uid', 'l.duration', 'l.number'])
+            ->order('l.number', 'desc')
+            ->order('l.duration', 'asc')
+            ->find();
+
+        $res = [
+            'self' => $userInfo,
+            'ranking' => $ranking
+        ];
+
         return $this->successResponse($res);
     }