lushuncheng 4 년 전
부모
커밋
2e88ed9ec2
3개의 변경된 파일175개의 추가작업 그리고 119개의 파일을 삭제
  1. 7 7
      app/middleware/Log.php
  2. 15 7
      app/middleware/Safe.php
  3. 153 105
      app/service/WeiboService.php

+ 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;
     }
 }