ソースを参照

Merge branch 'master' of git.shuncheng.lu:sina/music

lushuncheng 4 年 前
コミット
5261fafc88

+ 20 - 10
app/admin/view/sina/rule.html

@@ -4,22 +4,32 @@
     </fieldset>
     <div class="padding-left-40">
         <div class="layui-form-item">
-            <span class="color-green font-w7 label-required-prev">活动规则</span>
+            <span class="color-green font-w7 label-required-prev">聚合页活动规则</span>
             <span class="color-desc margin-left-5">Activity Rule</span>
-            <textarea class="layui-input" style="height: 550px;" name="config[rule]">{:trim($config['rule'] ?? '')}</textarea>
+            <textarea class="layui-input" style="height: 550px;"
+                name="config[rule]">{:trim($config['rule'] ?? '')}</textarea>
             <span class="help-block"></span>
         </div>
     </div>
-    <div class="padding-left-40"></div>
-        <div class="hr-line-dashed"></div>
-        <div class="layui-form-item text-center">
-            <button class="layui-btn" type="submit">保存配置</button>
-            <button class="layui-btn layui-btn-danger" type="button" data-confirm="确定要取消修改吗?" data-close>
-                取消修改
-            </button>
+    <div class="padding-left-40">
+        <div class="layui-form-item">
+            <span class="color-green font-w7 label-required-prev">品牌任务活动规则</span>
+            <span class="color-desc margin-left-5">Activity Rule</span>
+            <textarea class="layui-input" style="height: 550px;"
+                name="config[brand_rule]">{:trim($config['brand_rule'] ?? '')}</textarea>
+            <span class="help-block"></span>
         </div>
     </div>
+    <div class="padding-left-40"></div>
+    <div class="hr-line-dashed"></div>
+    <div class="layui-form-item text-center">
+        <button class="layui-btn" type="submit">保存配置</button>
+        <button class="layui-btn layui-btn-danger" type="button" data-confirm="确定要取消修改吗?" data-close>
+            取消修改
+        </button>
+    </div>
+    </div>
 </form>
 <script>
-  window.form.render();
+    window.form.render();
 </script>

+ 260 - 227
app/index/controller/Index.php

@@ -2,24 +2,24 @@
 
 namespace app\index\controller;
 
-use app\admin\model\SystemNotice;
+use think\facade\Db;
 use app\middleware\Log;
-use app\middleware\Safe;
-use app\service\WeiboService;
-use InvalidArgumentException;
+use think\facade\Cache;
 use ReflectionException;
+use app\middleware\Safe;
+use \think\response\Json;
+use think\facade\Request;
 use think\admin\Controller;
+use InvalidArgumentException;
+use app\service\WeiboService;
+use app\admin\model\SystemNotice;
+use think\facade\Log as FacadeLog;
+use think\db\exception\DbException;
 use think\admin\service\SystemService;
 use think\exception\FuncNotFoundException;
 use think\exception\ClassNotFoundException;
-use think\db\exception\DbException;
-use think\db\exception\ModelNotFoundException;
 use think\db\exception\DataNotFoundException;
-use think\facade\Cache;
-use think\facade\Db;
-use think\facade\Log as FacadeLog;
-use think\facade\Request;
-use \think\response\Json;
+use think\db\exception\ModelNotFoundException;
 
 /**
  * Class Index
@@ -27,15 +27,31 @@ use \think\response\Json;
  */
 class Index extends Controller
 {
-    public function index()
+    /**
+     * 获取品牌任务相关配置
+     *
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws ReflectionException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
+     */
+    public function brand()
     {
-        $this->redirect(sysuri('admin/login/index'));
+        $conf = $this->getBrandConfigAndState();
+        if ($conf instanceof Json) {
+            return $conf;
+        }
+        return $this->successResponse($conf);
     }
+
     /**
      * 检测登录 通过cookie中的SUB字段的内容,调用用户接口检测登录状态
-     * @return mixed 
-     * @throws ClassNotFoundException 
-     * @throws ReflectionException 
+     * @throws ClassNotFoundException
+     * @throws ReflectionException
+     * @return mixed
      */
     public function checkLogin()
     {
@@ -58,8 +74,8 @@ class Index extends Controller
         if (empty($userInfoRes) || $userInfoRes['code'] != 10000) {
             return $this->response(403, $userInfoRes['msg'] ?? '没有登录');
         }
-        // 使用客户端信息生成token 
-        $token =  md5($_SERVER['HTTP_USER_AGENT'] . $_SERVER['HTTP_ACCEPT_ENCODING'] . $_SERVER['HTTP_ACCEPT_LANGUAGE'] . $_SERVER['HTTP_REFERER'] . get_client_ip(0) . $userInfoRes['data']['uid']);
+        // 使用客户端信息生成token
+        $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'];
         // 生成加密用的密钥和向量
         $cipher = "aes-256-gcm";
@@ -83,100 +99,16 @@ class Index extends Controller
             'first' => Cache::get('u:f:' . $user['uid']) != 1,
         ]);
     }
-    /**
-     * 聚合页配置信息
-     * @return mixed 
-     * @throws FuncNotFoundException 
-     * @throws ReflectionException 
-     * @throws InvalidArgumentException 
-     * @throws ClassNotFoundException 
-     */
-    public function groupPageConfig()
-    {
-        return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
-    }
-    /**
-     * 聚合页通知获取
-     * 查询最近20条通知
-     * @return mixed 
-     * @throws DbException 
-     * @throws ModelNotFoundException 
-     * @throws DataNotFoundException 
-     */
-    public function notices()
-    {
-        $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
-        return $this->successResponse([
-            "lists" => $rows,
-        ]);
-    }
-    /**
-     * 首次弹窗
-     * 发放邀请函微博
-     * @return Json
-     */
-    public function sendInviteWeibo()
-    {
-        $config = SystemService::instance()->getData('group:page:config');
-        $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
-        if (empty($sendRes) || $sendRes['code'] != 10000) {
-            return $this->response(403, $sendRes['msg'] ?? '发布失败');
-        }
-        Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
-        return $this->successResponse(null, '发布成功!');
-    }
-    /**
-     * 首次弹窗用户未选择发送邀请函
-     * 标记已经首次弹窗过了
-     * @return mixed 
-     */
-    public function setFirst()
-    {
-        Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
-        return $this->successResponse(null, '操作成功!');
-    }
 
-    /**
-     * 获取活动规则
-     * @return mixed 
-     * @throws FuncNotFoundException 
-     * @throws ReflectionException 
-     * @throws InvalidArgumentException 
-     * @throws ClassNotFoundException 
-     */
-    public function getRule()
-    {
-        $config = SystemService::instance()->getData('activity:rule');
-        return $this->successResponse($config);
-    }
-    /**
-     * 获取品牌任务相关配置
-     * 
-     * @return mixed 
-     * @throws FuncNotFoundException 
-     * @throws ClassNotFoundException 
-     * @throws ReflectionException 
-     * @throws DbException 
-     * @throws ModelNotFoundException 
-     * @throws DataNotFoundException 
-     */
-    public function brand()
-    {
-        $conf = $this->getBrandConfigAndState();
-        if ($conf instanceof Json) {
-            return $conf;
-        }
-        return $this->successResponse($conf);
-    }
     /**
      * 品牌任务关注接口
-     * @return mixed 
-     * @throws FuncNotFoundException 
-     * @throws ClassNotFoundException 
-     * @throws ReflectionException 
-     * @throws DbException 
-     * @throws ModelNotFoundException 
-     * @throws DataNotFoundException 
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws ReflectionException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
      */
     public function follow()
     {
@@ -221,15 +153,16 @@ class Index extends Controller
             return $this->response(601, '更新任务状态失败');
         }
     }
+
     /**
      * 品牌任务转发博文接口
-     * @return mixed 
-     * @throws FuncNotFoundException 
-     * @throws ClassNotFoundException 
-     * @throws ReflectionException 
-     * @throws DbException 
-     * @throws ModelNotFoundException 
-     * @throws DataNotFoundException 
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws ReflectionException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
      */
     public function forward()
     {
@@ -274,68 +207,138 @@ class Index extends Controller
             return $this->response(601, '更新任务状态失败');
         }
     }
+
     /**
-     * 完成品牌任务后领取加票机会接口
-     * @return mixed 
-     * @throws FuncNotFoundException 
-     * @throws ClassNotFoundException 
-     * @throws ReflectionException 
-     * @throws DbException 
-     * @throws ModelNotFoundException 
-     * @throws DataNotFoundException 
+     * 获取活动规则
+     * @throws FuncNotFoundException
+     * @throws ReflectionException
+     * @throws InvalidArgumentException
+     * @throws ClassNotFoundException
+     * @return mixed
      */
-    public function taskFinishAddVotes()
+    public function getRule()
+    {
+        $config = SystemService::instance()->getData('activity:rule');
+        return $this->successResponse($config);
+    }
+
+    /**
+     * 聚合页配置信息
+     * @throws FuncNotFoundException
+     * @throws ReflectionException
+     * @throws InvalidArgumentException
+     * @throws ClassNotFoundException
+     * @return mixed
+     */
+    public function groupPageConfig()
+    {
+        return $this->successResponse(['config' => SystemService::instance()->getData('group:page:config')]);
+    }
+
+    public function index()
+    {
+        $this->redirect(sysuri('admin/login/index'));
+    }
+
+    /**
+     * 聚合页通知获取
+     * 查询最近20条通知
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
+     */
+    public function notices()
+    {
+        $rows = SystemNotice::limit(20)->order('id', 'desc')->select();
+        return $this->successResponse([
+            "lists" => $rows,
+        ]);
+    }
+
+    /**
+     * 首次弹窗
+     * 发放邀请函微博
+     * @return Json
+     */
+    public function sendInviteWeibo()
+    {
+        $config = SystemService::instance()->getData('group:page:config');
+        $sendRes = (new WeiboService(Safe::$user['uid']))->status($config['firstDialog']['content']);
+        if (empty($sendRes) || $sendRes['code'] != 10000) {
+            return $this->response(403, $sendRes['msg'] ?? '发布失败');
+        }
+        Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
+        return $this->successResponse(null, '发布成功!');
+    }
+
+    /**
+     * 首次弹窗用户未选择发送邀请函
+     * 标记已经首次弹窗过了
+     * @return mixed
+     */
+    public function setFirst()
+    {
+        Cache::set('u:f:' . Safe::$user['uid'], 1, 15552000);
+        return $this->successResponse(null, '操作成功!');
+    }
+
+    /**
+     * 标记用户已经浏览过主页了,用户点了去浏览时调用
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws ReflectionException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
+     */
+    public function setViewed()
     {
         // 先获取品牌任务配置及状态
         $conf = $this->getBrandConfigAndState();
         if ($conf instanceof Json) {
             return $conf;
         }
-        if ($conf['state']['finish_add_votes'] == 1) {
+        if ($conf['state']['view_state'] == 1) {
             return $this->successResponse([
                 'result' => 2,
                 'brand' => $conf,
-            ], '您今日已经领取过投票机会了~');
-        }
-
-        // 未完成品牌任务 领取失败
-        if ($conf['state']['finish_state'] == 0) {
-            return $this->response(603, '请先完成品牌任务,再来领取加票', [
-                'result' => 0,
-                'brand' => $conf,
-            ]);
-        }
-        // 调用微博接口 给用户发券(加票)
-        $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
-        if (empty($weiboRes) || $weiboRes['code'] != 10000) {
-            return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
+            ], '您已经浏览过了');
         }
-        // 标记加票成功
         $date = date('Y-m-d');
         $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
+        // 如果已经完成了另外两项,则标记品牌任务已经完成
+        $finishState = $conf['state']['finish_state'];
+        if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
+            $finishState = 1;
+        }
         $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
-            'finish_add_votes' => 1,
+            'view_state' => 1,
+            'finish_state' => $finishState,
         ]);
         if ($nums) {
-            $conf['state']['finish_add_votes'] = 1;
+            $conf['state']['view_state'] = 1;
+            $conf['state']['finish_state'] = $finishState;
             Cache::set($redisKey, json_encode($conf['state']), 86400);
             return $this->successResponse([
                 'result' => 1,
                 'brand' => $conf,
-            ], '加票成功');
+            ], '浏览成功');
         } else {
             return $this->response(601, '更新任务状态失败');
         }
     }
+
     /**
      * 用户完成端外分享时调用接口加票
-     * @return mixed 
-     * @throws FuncNotFoundException 
-     * @throws ClassNotFoundException 
-     * @throws ReflectionException 
-     * @throws DbException 
-     * @throws ModelNotFoundException 
-     * @throws DataNotFoundException 
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws ReflectionException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
      */
     public function shareFinishAddVotes()
     {
@@ -372,123 +375,141 @@ class Index extends Controller
             return $this->response(601, '更新任务状态失败');
         }
     }
+
     /**
-     * 标记用户已经浏览过主页了,用户点了去浏览时调用
-     * @return mixed 
-     * @throws FuncNotFoundException 
-     * @throws ClassNotFoundException 
-     * @throws ReflectionException 
-     * @throws DbException 
-     * @throws ModelNotFoundException 
-     * @throws DataNotFoundException 
+     * 完成品牌任务后领取加票机会接口
+     * @throws FuncNotFoundException
+     * @throws ClassNotFoundException
+     * @throws ReflectionException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return mixed
      */
-    public function setViewed()
+    public function taskFinishAddVotes()
     {
         // 先获取品牌任务配置及状态
         $conf = $this->getBrandConfigAndState();
         if ($conf instanceof Json) {
             return $conf;
         }
-        if ($conf['state']['view_state'] == 1) {
+        if ($conf['state']['finish_add_votes'] == 1) {
             return $this->successResponse([
                 'result' => 2,
                 'brand' => $conf,
-            ], '您已经浏览过了');
+            ], '您今日已经领取过投票机会了~');
         }
+
+        // 未完成品牌任务 领取失败
+        if ($conf['state']['finish_state'] == 0) {
+            return $this->response(603, '请先完成品牌任务,再来领取加票', [
+                'result' => 0,
+                'brand' => $conf,
+            ]);
+        }
+        // 调用微博接口 给用户发券(加票)
+        $weiboRes = (new WeiboService(Safe::$user['uid']))->setcoupons($conf['config']['task']['votes']);
+        if (empty($weiboRes) || $weiboRes['code'] != 10000) {
+            return $this->response(600, $weiboRes['msg'] ?? '加票失败~');
+        }
+        // 标记加票成功
         $date = date('Y-m-d');
         $redisKey = $this->getTaskStateRedisKey(intval(Safe::$user['uid']));
-        // 如果已经完成了另外两项,则标记品牌任务已经完成
-        $finishState = $conf['state']['finish_state'];
-        if ($conf['state']['forward_state'] == 1 && $conf['state']['follow_state'] == 1) {
-            $finishState = 1;
-        }
         $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update([
-            'view_state' => 1,
-            'finish_state' => $finishState,
+            'finish_add_votes' => 1,
         ]);
         if ($nums) {
-            $conf['state']['view_state'] = 1;
-            $conf['state']['finish_state'] = $finishState;
+            $conf['state']['finish_add_votes'] = 1;
             Cache::set($redisKey, json_encode($conf['state']), 86400);
             return $this->successResponse([
                 'result' => 1,
                 'brand' => $conf,
-            ], '浏览成功');
+            ], '加票成功');
         } else {
             return $this->response(601, '更新任务状态失败');
         }
     }
-    /**
-     * 获取品牌任务redis 缓存键
-     * @param int $uid 
-     * @return string 
-     */
-    protected function getTaskStateRedisKey(int $uid): string
-    {
-        $dateForRedis = date('Ymd');
-        return "t:{$dateForRedis}:" . $uid;
-    }
+
     /**
      * 获取品牌任务配置及状态
-     * @return \think\response\Json|array 
-     * @throws FuncNotFoundException 
-     * @throws ReflectionException 
-     * @throws InvalidArgumentException 
-     * @throws ClassNotFoundException 
-     * @throws DbException 
-     * @throws ModelNotFoundException 
-     * @throws DataNotFoundException 
+     * @throws FuncNotFoundException
+     * @throws ReflectionException
+     * @throws InvalidArgumentException
+     * @throws ClassNotFoundException
+     * @throws DbException
+     * @throws ModelNotFoundException
+     * @throws DataNotFoundException
+     * @return \think\response\Json|array
      */
     protected function getBrandConfigAndState()
     {
         $config = SystemService::instance()->getData('brand:task:config');
         $date = date('Y-m-d');
         $dateForRedis = date('Ymd');
-        $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
-        $state = Cache::get($redisKey);
-        if (empty($state)) {
-            // 缓存失败 查数据库
-            $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
+        if (empty(Safe::$user['uid'])) {
+            $state = [
+                // 品牌任务完成状态
+                'finish_state' => 0,
+                // 关注子任务状态
+                'follow_state' => 0,
+                // 转发子任务状态
+                'forward_state' => 0,
+                // 查看浏览主页任务状态
+                'view_state' => 0,
+                // 品牌任务完成后是否加票
+                'finish_add_votes' => 0,
+                // 分享完成后是否加票
+                'share_add_votes' => 0,
+                'uid' => 0,
+                'date' => $date,
+            ];
+        } else {
+            $redisKey = "t:{$dateForRedis}:" . Safe::$user['uid'];
+            $state = Cache::get($redisKey);
             if (empty($state)) {
-                // 当天第一次,初始化数据
-                $state = [
-                    // 品牌任务完成状态
-                    'finish_state' => 0,
-                    // 关注子任务状态
-                    'follow_state' => 0,
-                    // 转发子任务状态
-                    'forward_state' => 0,
-                    // 查看浏览主页任务状态
-                    'view_state' => 0,
-                    // 品牌任务完成后是否加票
-                    'finish_add_votes' => 0,
-                    // 分享完成后是否加票
-                    'share_add_votes' => 0,
-                    'uid' => Safe::$user['uid'],
-                    'date' => $date,
-                ];
-                if (0 == Db::table('awards_user_task')->insert($state)) {
-                    return $this->response(5001, '系统错误,请稍后再试~');
+                // 缓存失败 查数据库
+                $state = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->find();
+                if (empty($state)) {
+                    // 当天第一次,初始化数据
+                    $state = [
+                        // 品牌任务完成状态
+                        'finish_state' => 0,
+                        // 关注子任务状态
+                        'follow_state' => 0,
+                        // 转发子任务状态
+                        'forward_state' => 0,
+                        // 查看浏览主页任务状态
+                        'view_state' => 0,
+                        // 品牌任务完成后是否加票
+                        'finish_add_votes' => 0,
+                        // 分享完成后是否加票
+                        'share_add_votes' => 0,
+                        'uid' => Safe::$user['uid'],
+                        'date' => $date,
+                    ];
+                    if (0 == Db::table('awards_user_task')->insert($state)) {
+                        return $this->response(5001, '系统错误,请稍后再试~');
+                    }
                 }
+                Cache::set($redisKey, json_encode($state), 86400);
+            } else {
+                $state = json_decode($state, true);
             }
-            Cache::set($redisKey, json_encode($state), 86400);
-        } else {
-            $state = json_decode($state, true);
-        }
-        // 如果未关注状态,查询下已经实际已经关注了
-        if ($state['follow_state'] == 0) {
-            $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
-            if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
-                $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1,]);
-                if ($nums) {
-                    $state['follow_state'] = 1;
-                    Cache::set($redisKey, json_encode($state), 86400);
+            // 如果未关注状态,查询下已经实际已经关注了
+            if ($state['follow_state'] == 0) {
+                $weiboRes = (new WeiboService(Safe::$user['uid']))->friends($config['follow']['id']);
+                if (isset($weiboRes['data'][$config['follow']['id']]) && $weiboRes['data'][$config['follow']['id']] == 1) {
+                    $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['follow_state' => 1]);
+                    if ($nums) {
+                        $state['follow_state'] = 1;
+                        Cache::set($redisKey, json_encode($state), 86400);
+                    }
                 }
             }
         }
         // 校准状态,可能存在异常情况关注,转发,浏览都完成了,但是总的品牌任务状态没标记成已完成
         if ($state['follow_state'] == 1 && $state['forward_state'] == 1 && $state['view_state'] == 1 && $state['finish_state'] != 1) {
-            $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1,]);
+            $nums = Db::table('awards_user_task')->where('uid', Safe::$user['uid'])->where('date', $date)->update(['finish_state' => 1]);
             if ($nums) {
                 $state['finish_state'] = 1;
                 Cache::set($redisKey, json_encode($state), 86400);
@@ -499,6 +520,18 @@ class Index extends Controller
             'state' => $state,
         ];
     }
+
+    /**
+     * 获取品牌任务redis 缓存键
+     * @param  int      $uid
+     * @return string
+     */
+    protected function getTaskStateRedisKey(int $uid): string
+    {
+        $dateForRedis = date('Ymd');
+        return "t:{$dateForRedis}:" . $uid;
+    }
+
     /**
      * @param  $code
      * @param  $msg

+ 7 - 7
app/middleware/Log.php

@@ -1,16 +1,16 @@
 <?php
 
-declare(strict_types=1);
+declare (strict_types = 1);
 
 namespace app\middleware;
 
-use app\utils\StringsUtil;
 use Closure;
-use think\facade\Event;
-use think\facade\Log as FacadeLog;
 use think\Request;
 use think\Response;
+use think\facade\Event;
 use think\response\Json;
+use app\utils\StringsUtil;
+use think\facade\Log as FacadeLog;
 
 class Log
 {
@@ -22,8 +22,8 @@ class Log
     /**
      * 处理请求
      *
-     * @param Request $request
-     * @param Closure $next
+     * @param  Request    $request
+     * @param  Closure    $next
      * @return Response
      */
     public function handle(Request $request, Closure $next)
@@ -67,7 +67,7 @@ class Log
          */
         $response = $next($request);
         $endTime = microtime(true);
-        $spendTime = bcsub(strval($endTime), strval($startTime), 4);
+        $spendTime = round($endTime - $startTime, 4);
         $data = '';
         if ($response instanceof Json) {
             $data = $response->getData();

+ 15 - 7
app/middleware/Safe.php

@@ -1,24 +1,32 @@
 <?php
 
-declare(strict_types=1);
+declare (strict_types = 1);
 
 namespace app\middleware;
 
 use Closure;
-use think\facade\Cache;
-use think\facade\Log;
 use think\Request;
 use think\Response;
+use think\facade\Log;
+use think\facade\Cache;
 
 class Safe
 {
-    public static $user = [];
+    /**
+     * @var array
+     */
     public static $body = [];
+
+    /**
+     * @var array
+     */
+    public static $user = [];
+
     /**
      * 处理请求
      *
-     * @param Request $request
-     * @param Closure $next
+     * @param  Request    $request
+     * @param  Closure    $next
      * @return Response
      */
     public function handle(Request $request, Closure $next)
@@ -38,7 +46,7 @@ class Safe
                 return null;
             }
         }
-        if (strpos(\think\facade\Request::url(), '/checkLogin') !== false) {
+        if (strpos(\think\facade\Request::url(), '/checkLogin') !== false || strpos(\think\facade\Request::url(), '/groupPageConfig') || strpos(\think\facade\Request::url(), '/notices') || strpos(\think\facade\Request::url(), '/getRule') || strpos(\think\facade\Request::url(), '/brand')) {
             return $next($request);
         }
         $token = $request->post('token', '');

+ 153 - 105
app/service/WeiboService.php

@@ -3,11 +3,26 @@
 namespace app\service;
 
 use app\middleware\Log;
-use think\facade\Log as FacadeLog;
 use think\facade\Request;
+use think\facade\Log as FacadeLog;
 
 class WeiboService
 {
+    /**
+     * @var mixed
+     */
+    public $signStr;
+
+    /**
+     * @var string
+     */
+    private $appKey = 'h5vote';
+
+    /**
+     * @var string
+     */
+    private $appSecret = '8d6967d654bca47a';
+
     /**
      * 本类为底层微博侧相关接口请求代码示例
      */
@@ -15,11 +30,14 @@ class WeiboService
     //@todo 切换为正式接口地址
     private static $base_url = 'https://ent.weibo.cn/movie/z/musicawards2021h5/';
 
-    private $appKey = 'h5vote';
-    private $appSecret = '8d6967d654bca47a';
+    /**
+     * @var mixed
+     */
     private $uid;
-    public $signStr;
 
+    /**
+     * @param $uid
+     */
     public function __construct($uid = 0)
     {
         $this->uid = $uid;
@@ -29,39 +47,79 @@ class WeiboService
         static::$base_url = env('weibo.api_host', 'https://ent.weibo.cn/movie/z/musicawards2021h5/');
     }
 
-    private function signStr($uid)
+    /**
+     * @param  $url
+     * @return mixed
+     */
+    public static function _httpGet($url = "")
     {
-        $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;
+        $curl = curl_init();
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
+        curl_setopt($curl, CURLOPT_URL, $url);
+        curl_setopt($curl, CURLOPT_REFERER, 'https://ent.weibo.cn'); //模拟来路
+        curl_setopt($curl, CURLOPT_HTTPHEADER, [
+            'X-Forwarded-For' => get_client_ip(0),
+            'X-Request-Id' => Log::$logID,
+        ]);
+        FacadeLog::info(
+            'weibo_request|uri:' . $url
+        );
+        $start = microtime(true);
+        $res = curl_exec($curl);
+        $end = microtime(true);
+        $diff = round($end, $start, 4);
+        FacadeLog::info(
+            'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
+        );
+        curl_close($curl);
+        $res = json_decode($res, true);
+
+        return $res;
     }
 
-    //获取用户信息
-    public function userinfo($sub)
+    /**
+     * @param $url
+     * @param array  $requestData
+     */
+    public static function _httpPost($url = "", $requestData = [])
     {
-        if (env('weibo.mock') == 1) {
-            return [
-                'code' => 10000,
-                'msg' => '操作成功',
-                'data' => [
-                    'uid' => mt_rand(1000, 9999),
-                    'screen_name' => substr(md5(time()), 0, 6),
-                    'avatar' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLZyicsRMQS6KouWddW4E0HZYfmwZNTiaE6QbgvTf5JJicvc1RsK0ibAXzDq8mOIVcK9tG9Zz1icXprY0Q/132',
-                ]
-            ];
-        }
-        $url = self::$base_url . 'userinfo' . $this->signStr . '&sub=' . $sub;
+        $curl = curl_init();
 
-        return self::_httpGet($url);
+        curl_setopt($curl, CURLOPT_URL, $url);
+        curl_setopt($curl, CURLOPT_REFERER, 'https://ent.weibo.cn'); //模拟来路
+        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+
+        curl_setopt($curl, CURLOPT_HTTPHEADER, [
+            'X-Forwarded-For' => get_client_ip(0),
+            'X-Request-Id' => Log::$logID,
+        ]);
+
+        //POST数据
+        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($requestData));
+
+        FacadeLog::info(
+            'weibo_request|uri:' . $url . '|params:' . json_encode(
+                http_build_query($requestData),
+                JSON_UNESCAPED_UNICODE
+            )
+        );
+        $start = microtime(true);
+        $res = curl_exec($curl);
+        $end = microtime(true);
+        $diff = round($end - $start, 4);
+        FacadeLog::info(
+            'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
+        );
+        curl_close($curl);
+        return json_decode($res, true);
     }
 
-    //给用户发放指定类型卡券
-    public function setcoupons($num)
+    //添加关注
+    /**
+     * @param $friends
+     */
+    public function add($friends)
     {
         if (env('weibo.mock') == 1) {
             return [
@@ -70,32 +128,38 @@ class WeiboService
                 'data' => null,
             ];
         }
-        $url = self::$base_url . 'setcoupons' . $this->signStr;
+        $url = self::$base_url . 'add' . $this->signStr;
         $data['uid'] = $this->uid;
-        $data['data'] = $num;
+        $data['id'] = $friends;
         return self::_httpPost($url, $data);
     }
 
-    //发原创微博
-    public function status($content, $pic_id = '')
+    //查询关注关系
+    /**
+     * @param $friends
+     */
+    public function friends($friends)
     {
         if (env('weibo.mock') == 1) {
             return [
                 'code' => 10000,
                 'msg' => '操作成功',
-                'data' => null,
+                'data' => [
+                    $friends => mt_rand(0, 1),
+                ],
             ];
         }
-        $url = self::$base_url . 'status' . $this->signStr;
+        $url = self::$base_url . 'friends' . $this->signStr;
         $data['uid'] = $this->uid;
-        $data['content'] = $content;
-        if (!empty($pic_id)) {
-            $data['pic_id'] = $pic_id;
-        }
+        $data['id'] = $friends;
         return self::_httpPost($url, $data);
     }
 
     //转发微博
+    /**
+     * @param $mid
+     * @param $content
+     */
     public function repost($mid, $content)
     {
         if (env('weibo.mock') == 1) {
@@ -112,26 +176,31 @@ class WeiboService
         return self::_httpPost($url, $data);
     }
 
-    //查询关注关系
-    public function friends($friends)
+    //给用户发放指定类型卡券
+    /**
+     * @param $num
+     */
+    public function setcoupons($num)
     {
         if (env('weibo.mock') == 1) {
             return [
                 'code' => 10000,
                 'msg' => '操作成功',
-                'data' => [
-                    $friends => mt_rand(0, 1),
-                ],
+                'data' => null,
             ];
         }
-        $url = self::$base_url . 'friends' . $this->signStr;
+        $url = self::$base_url . 'setcoupons' . $this->signStr;
         $data['uid'] = $this->uid;
-        $data['id'] = $friends;
+        $data['data'] = $num;
         return self::_httpPost($url, $data);
     }
 
-    //添加关注
-    public function add($friends)
+    //发原创微博
+    /**
+     * @param $content
+     * @param $pic_id
+     */
+    public function status($content, $pic_id = '')
     {
         if (env('weibo.mock') == 1) {
             return [
@@ -140,70 +209,49 @@ class WeiboService
                 'data' => null,
             ];
         }
-        $url = self::$base_url . 'add' . $this->signStr;
+        $url = self::$base_url . 'status' . $this->signStr;
         $data['uid'] = $this->uid;
-        $data['id'] = $friends;
+        $data['content'] = $content;
+        if (!empty($pic_id)) {
+            $data['pic_id'] = $pic_id;
+        }
         return self::_httpPost($url, $data);
     }
 
-
-    public static function _httpGet($url = "")
+    //获取用户信息
+    /**
+     * @param $sub
+     */
+    public function userinfo($sub)
     {
-        $curl = curl_init();
-        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
-        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
-        curl_setopt($curl, CURLOPT_URL, $url);
-        curl_setopt($curl, CURLOPT_REFERER, 'https://ent.weibo.cn'); //模拟来路
-        curl_setopt($curl, CURLOPT_HTTPHEADER, [
-            'X-Forwarded-For' => get_client_ip(0),
-            'X-Request-Id' => Log::$logID,
-        ]);
-        FacadeLog::info(
-            'weibo_request|uri:' . $url
-        );
-        $start = microtime(true);
-        $res = curl_exec($curl);
-        $end = microtime(true);
-        $diff = bcsub(strval($end), strval($start), 4);
-        FacadeLog::info(
-            'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
-        );
-        curl_close($curl);
-        $res = json_decode($res, true);
+        if (env('weibo.mock') == 1) {
+            return [
+                'code' => 10000,
+                'msg' => '操作成功',
+                'data' => [
+                    'uid' => mt_rand(1000, 9999),
+                    'screen_name' => substr(md5(time()), 0, 6),
+                    'avatar' => 'https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLZyicsRMQS6KouWddW4E0HZYfmwZNTiaE6QbgvTf5JJicvc1RsK0ibAXzDq8mOIVcK9tG9Zz1icXprY0Q/132',
+                ],
+            ];
+        }
+        $url = self::$base_url . 'userinfo' . $this->signStr . '&sub=' . $sub;
 
-        return $res;
+        return self::_httpGet($url);
     }
 
-    public static function _httpPost($url = "", $requestData = [])
+    /**
+     * @param $uid
+     */
+    private function signStr($uid)
     {
-        $curl = curl_init();
-
-        curl_setopt($curl, CURLOPT_URL, $url);
-        curl_setopt($curl, CURLOPT_REFERER, 'https://ent.weibo.cn'); //模拟来路
-        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
-
-        curl_setopt($curl, CURLOPT_HTTPHEADER, [
-            'X-Forwarded-For' => get_client_ip(0),
-            'X-Request-Id' => Log::$logID,
-        ]);
-
-        //POST数据
-        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($requestData));
-
-        FacadeLog::info(
-            'weibo_request|uri:' . $url . '|params:' . json_encode(
-                http_build_query($requestData),
-                JSON_UNESCAPED_UNICODE
-            )
-        );
-        $start = microtime(true);
-        $res = curl_exec($curl);
-        $end = microtime(true);
-        $diff = bcsub(strval($end), strval($start), 4);
-        FacadeLog::info(
-            'weibo_request|spentTime:' . $diff . '|uri|' . $url . '|response:' . $res
-        );
-        curl_close($curl);
-        return json_decode($res, true);
+        $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;
     }
 }