allen hace 3 años
padre
commit
1af68e486d
Se han modificado 2 ficheros con 171 adiciones y 14 borrados
  1. 159 1
      app/index/controller/Index.php
  2. 12 13
      app/service/WeiboService.php

+ 159 - 1
app/index/controller/Index.php

@@ -91,13 +91,171 @@ class Index extends Controller
             'ase_iv' => $iv,
             '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', 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;
+            }
+        }
         // 缓存用户信息1天
         Cache::set('u:' . $token, json_encode($cacheUser), 86400);
         return $this->successResponse([
             'user' => $user,
             'token' => $token,
-            'first' => Cache::get('u:f:' . $user['uid']) != 1,
+            'first' => $first,
+        ]);
+    }
+
+    /**
+     * 提交任务接口
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
+     */
+    public function submitTask()
+    {
+        $duration = Request::post("duration");
+        $number = Request::post("number");
+
+        $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, '没有登录');
+        }
+
+        $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(403, '没有参与次数');
+            }
+            $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(403, '没有参与次数');
+            }
+            $log = [
+                'uid' => Safe::$user['uid'],
+                'date' => $date,
+                'duration' => $duration,
+                'number' => $number,
+                'create_at' => time()
+            ];
+        }
+
+        if (empty($log)) {
+            return $this->response(403, '没有参与次数');
+        }
+
+        if (0 == Db::table('awards_user_task_log')->insert($log)) {
+            return $this->response(5001, '系统错误,请稍后再试~');
+        }
+
+        return $this->successResponse(null, '提交成功');
+    }
+
+    /**
+     * 提交发布微博接口
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
+     */
+    public function submitShare()
+    {
+        $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, '没有登录');
+        }
+
+        if ($userTask['share_add_votes'] > 0) {
+            return $this->response(601, '今日已发布');
+        }
+
+        // 更新任务状态
+        $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
+            'share_add_votes' => 1,
         ]);
+
+        if ($nums) {
+            return $this->response(601, '发布成功');
+        } else {
+            return $this->response(601, '发布失败');
+        }
+    }
+
+    /**
+     * 排行接口
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
+     */
+    public function ranking()
+    {
+        $date = date('Y-m-d');
+        $res = 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(15)
+            ->select();
+
+        return $this->successResponse($res);
     }
 
     /**

+ 12 - 13
app/service/WeiboService.php

@@ -13,6 +13,11 @@ class WeiboService
      */
     public $signStr;
 
+    /**
+     * @var time
+     */
+    public $time;
+
     /**
      * @var string
      */
@@ -28,7 +33,7 @@ class WeiboService
      */
 
     //@todo 切换为正式接口地址
-    private static $base_url = 'https://ent.weibo.cn/movie/z/musicawards2021h5/';
+    private static $base_url = 'https://ent.weibo.cn/open/wmac/';
 
     /**
      * @var mixed
@@ -41,10 +46,11 @@ class WeiboService
     public function __construct($uid = 0)
     {
         $this->uid = $uid;
-        $this->signStr = $this->signStr($uid);
         $this->appKey = env('weibo.api_key', 'h5vote');
         $this->appSecret = env('weibo.api_secret', '8d6967d654bca47a');
-        static::$base_url = env('weibo.api_host', 'https://ent.weibo.cn/movie/z/musicawards2021h5/');
+        $this->time = time();
+        $this->signStr = $this->signStr($this->time, $uid);
+        static::$base_url = env('weibo.api_host', 'https://ent.weibo.cn/open/wmac/');
     }
 
     /**
@@ -235,7 +241,7 @@ class WeiboService
                 ],
             ];
         }
-        $url = self::$base_url . 'userinfo' . $this->signStr . '&sub=' . $sub;
+        $url = self::$base_url . 'user'. '?sign=' . $this->signStr . '&sub=' . $sub . '&time='.$this->time.'&secret='.$this->appSecret;
 
         return self::_httpGet($url);
     }
@@ -243,15 +249,8 @@ class WeiboService
     /**
      * @param $uid
      */
-    private function signStr($uid)
+    private function signStr($time, $sub)
     {
-        $timestamp = time();
-        $appSign = substr(strtolower(md5($this->appKey . $timestamp . $this->appSecret)), 0, 6);
-        if ($uid > 0) {
-            $uid_str = '&uid=' . $uid;
-        } else {
-            $uid_str = '';
-        }
-        return '?timestamp=' . $timestamp . '&appKey=' . $this->appKey . '&appSign=' . $appSign . $uid_str;
+        return md5($time>>8 . $this->appSecret . $sub);
     }
 }