dong025 4 дней назад
Родитель
Сommit
127c0d0275
2 измененных файлов с 296 добавлено и 51 удалено
  1. 110 51
      app/admin/command/FengSu.php
  2. 186 0
      app/common/controller/Http.php

+ 110 - 51
app/admin/command/FengSu.php

@@ -31,7 +31,7 @@ use app\common\model\ImportList;
 use app\admin\service\JuShuiTanService;
 
 use think\facade\Cache;
-
+use app\common\controller\Http;
 //抖音
 class FengSu extends Command
 {
@@ -52,9 +52,9 @@ class FengSu extends Command
     protected $type_id = 1;
 
     //时间间隔
-    protected $start_end_space_time=10*60;
+    protected $start_end_space_time = 10 * 60;
     //下一次时间间隔
-    protected $next_time_space=7*60;
+    protected $next_time_space = 7 * 60;
 
 
     // 指令配置
@@ -70,19 +70,19 @@ class FengSu extends Command
 
         $resquet = $this->getOrderList('');
         if ($resquet['code'] == 0) {
-            $fengsu=[];
-            $fengsu=Cache::get('fengsu');
-            if ($resquet['data']['list']!=null&&!empty($resquet['data']['list'])) {
-
-                $fengsu_pageNum=$fengsu['pageNum'];
-                $fengsu_pageNum=$fengsu_pageNum+1;
-                $fengsu=[
-                    'startTime'=>$fengsu['startTime'],
-                    'endTime'=>$fengsu['endTime'],
-                    'pageNum'=>$fengsu_pageNum,
-                    'next_time_space'=>$fengsu['next_time_space']
+            $fengsu = [];
+            $fengsu = Cache::get('fengsu');
+            if ($resquet['data']['list'] != null && !empty($resquet['data']['list'])) {
+
+                $fengsu_pageNum = $fengsu['pageNum'];
+                $fengsu_pageNum = $fengsu_pageNum + 1;
+                $fengsu = [
+                    'startTime' => $fengsu['startTime'],
+                    'endTime' => $fengsu['endTime'],
+                    'pageNum' => $fengsu_pageNum,
+                    'next_time_space' => $fengsu['next_time_space']
                 ];
-                Cache::set('fengsu',$fengsu);
+                Cache::set('fengsu', $fengsu);
 
 
                 $list = $resquet['data']['list'];
@@ -93,10 +93,10 @@ class FengSu extends Command
             } else {
 
                 $now_time = time();
-                if ($fengsu['startTime']>=$now_time||($now_time - $fengsu['startTime']) < ($this->next_time_space)) {
+                if ($fengsu['startTime'] >= $now_time || ($now_time - $fengsu['startTime']) < ($this->next_time_space)) {
                     // dump("超时了");
                     $pre_time_stamp = $this->next_time_space;
-                    
+
                     $startTime_stamp = $now_time - $pre_time_stamp;
                     $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
                     $fengsu = [
@@ -105,7 +105,7 @@ class FengSu extends Command
                         'pageNum' => 1,
                         'next_time_space' => $fengsu['next_time_space']
                     ];
-                }else{
+                } else {
                     $fengsu = [
                         'startTime' => $fengsu['startTime'] + $fengsu['next_time_space'],
                         'endTime' => $fengsu['endTime'] + $fengsu['next_time_space'],
@@ -115,7 +115,7 @@ class FengSu extends Command
                 }
 
 
-                Cache::set('fengsu',$fengsu);
+                Cache::set('fengsu', $fengsu);
 
 
                 $output->writeln('没有数据');
@@ -155,30 +155,30 @@ class FengSu extends Command
 
         $startTime_stamp = strtotime($todayTime . ' 00:00:00');
         // $startTime_stamp = strtotime($todayTime . ' 07:20:00');
-        $endTime_stamp=$startTime_stamp+$this->start_end_space_time;
-        $pageNum=1;
-
-        $fengsu=[
-            'startTime'=>$startTime_stamp,
-            'endTime'=>$endTime_stamp,
-            'pageNum'=>$pageNum,
-            'next_time_space'=>$this->next_time_space
+        $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
+        $pageNum = 1;
+
+        $fengsu = [
+            'startTime' => $startTime_stamp,
+            'endTime' => $endTime_stamp,
+            'pageNum' => $pageNum,
+            'next_time_space' => $this->next_time_space
         ];
 
-        $fengsu_cache_data=Cache::get('fengsu');
-        
-        if(empty($fengsu_cache_data)||$fengsu_cache_data==null){
-            Cache::set('fengsu',$fengsu);
+        $fengsu_cache_data = Cache::get('fengsu');
+
+        if (empty($fengsu_cache_data) || $fengsu_cache_data == null) {
+            Cache::set('fengsu', $fengsu);
         }
-        $fengsu=Cache::get('fengsu');
-        
+        $fengsu = Cache::get('fengsu');
+
         // 无数据时,获取数据的开始时间会大于现在的时间,这是一个错误的,会导致一直无法获取数据,开始时间一直往后
         //当开始时间大于现在时间,将开始时间向前调整3分钟
         $now_time = time();
-        if ($fengsu['startTime']>=$now_time||($now_time - $fengsu['startTime']) < ($this->start_end_space_time - $this->next_time_space)) {
+        if ($fengsu['startTime'] >= $now_time || ($now_time - $fengsu['startTime']) < ($this->start_end_space_time - $this->next_time_space)) {
             // dump("超时了");
-            $pre_time_stamp =$this->next_time_space;
-            
+            $pre_time_stamp = $this->next_time_space;
+
             $startTime_stamp = $now_time - $pre_time_stamp;
             $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
             $fengsu = [
@@ -191,7 +191,7 @@ class FengSu extends Command
         Cache::set('fengsu', $fengsu);
         $startTime = date('Y-m-d H:i:s', $fengsu['startTime']);
         $endTime = date('Y-m-d H:i:s', $fengsu['endTime']);
-        $pageNum=(int)$fengsu['pageNum'];
+        $pageNum = (int)$fengsu['pageNum'];
         // dump($startTime);
         // dump($fengsu);
         // $body = [
@@ -207,41 +207,62 @@ class FengSu extends Command
         //     'goodsFlag'    => 2
         // ];
         $body = [
-            'startTime' =>$startTime,
+            'startTime' => $startTime,
             // 'startTime' =>'2026-01-03 00:23:00',
             'endTime' => $endTime,
             // 'endTime' => '2026-01-07 23:30:00',
             'current' => $pageNum,
             // 'current' => 1,
-            
+
             'goodType' => 2,
             'goodsFlag' => 1,
             'orderPrefixSelect' => 'waybillNos',
-            'prepareShip'=>0,
+            'prepareShip' => 0,
             'size' => 50,
             'sort' => 'CONSIGN_TIME',
             'sortAsc' => true,
-            
+
             'timeType' => 2,
             'tradeStatus' => 2,
             'skuOuterIdExact' => false,
             'skuPropExact' => false,
             'tradeSources' => []
         ];
-            // 'waybillNos'   => ['73589756512463']
+        // 'waybillNos'   => ['73589756512463']
         $body = json_encode($body);
-        
+
         $request = new Request('POST', 'https://fsdy2.fengsutb.com/amount/ship-list-new', $headers, $body);
+
         // $request = new Request('POST', 'https://fsdy2.fengsutb.com/trade/print-list', $headers, $body);
-        
-        $res = $client->sendAsync($request)->wait();
-        $json = [];
-        if ($res->getStatusCode() == 200) {
-            $json = json_decode($res->getBody()->getContents(), true);
-        } else {
-            dump($res->getStatusCode(), '接口返回结果');
+        try {
+            $res = $client->sendAsync($request)->wait();
+
+            $json = [];
+            if ($res->getStatusCode() == 200) {
+                $json = json_decode($res->getBody()->getContents(), true);
+            } else {
+                dump($res->getStatusCode(), '接口返回结果');
+            }
+            return $json;
+        } catch (\Exception $e) {
+            $errorStr = $e->getMessage();
+            // 定位response:位置
+            $pos = strpos($errorStr, 'response:');
+            if ($pos !== false) {
+                // 从response:后开始截取
+                $jsonPart = substr($errorStr, $pos + strlen('response:'));
+                // 清理空白字符
+                $jsonPart = preg_replace('/^\s+|\s+$/', '', $jsonPart);
+                $jsonPart = json_decode($jsonPart, true);
+                if (!empty($jsonPart)) {
+                    if ($jsonPart['code'] == 401 && $jsonPart['message'] == 'token失效') {
+                        $this->set_fengsu_Authorization($client);
+                    }
+                }
+            }
+            var_dump($e->getMessage());
+            return null;
         }
-        return $json;
     }
     public function insertOrder($order_list)
     {
@@ -316,6 +337,44 @@ class FengSu extends Command
         // 更新最后执行时间
         CrontabLog::create(['type_id' => 2, 'last_time' => date('Y-m-d H:i:s')]);
     }
+    //更新风速授权token
+    public function set_fengsu_Authorization($client)
+    {
+        $url = 'https://fx.fengsutb.com/gw/user/login';
+        $headers = [
+            'Accept' => '*/*',
+            'Accept-Encoding' => 'gzip, deflate, br',
+            'User-Agent' => 'PostmanRuntime-ApipostRuntime/1.1.0',
+            'Connection' => 'keep-alive',
+            'Content-Type' => 'application/json'
+        ];
+
+        $params = [
+            "mobile" => "data_sync",
+            "password" => "6YZKdG6iO!",
+            "verificationCode" => "2588",
+            "loginType" => "passwordLogin"
+        ];
+
+        $params = json_encode($params);
+
+        $request_login = new Request('POST', $url, $headers, $params);
+        $res = $client->sendAsync($request_login)->wait();
+        $json = [];
+        if ($res->getStatusCode() == 200) {
+            $json = json_decode($res->getBody()->getContents(), true);
+        } else {
+            dump($res->getStatusCode(), '接口返回结果');
+        }
+        if(!empty($json)){
+            if($json['code']==0&&$json['message']=='登录成功!'&&!empty($json['data'])){
+                site_config("addonsd.authorization_token",$json['data']);
+                dump('token更新成功');
+            }
+        }
+        
+        
+    }
 }
 //风速接口返回格式
 //            {

+ 186 - 0
app/common/controller/Http.php

@@ -0,0 +1,186 @@
+<?php
+
+namespace app\common\controller;
+
+/**
+ * Http 请求类
+ */
+class Http
+{
+
+    /**
+     * 发送一个POST请求
+     * @param string $url     请求URL
+     * @param array  $params  请求参数
+     * @param array  $options 扩展参数
+     * @return mixed|string
+     */
+    public static function post($url, $params = [], $options = [])
+    {
+        $req = self::sendRequest($url, $params, 'POST', $options);
+        
+        return $req['ret'] ? $req['msg'] : '';
+    }
+
+    /**
+     * 发送一个GET请求
+     * @param string $url     请求URL
+     * @param array  $params  请求参数
+     * @param array  $options 扩展参数
+     * @return mixed|string
+     */
+    public static function get($url, $params = [], $options = [])
+    {
+        $req = self::sendRequest($url, $params, 'GET', $options);
+        return $req['ret'] ? $req['msg'] : '';
+    }
+
+    /**
+     * CURL发送Request请求,含POST和REQUEST
+     * @param string $url     请求的链接
+     * @param mixed  $params  传递的参数
+     * @param string $method  请求的方法
+     * @param mixed  $options CURL的参数
+     * @return array
+     */
+    public static function sendRequest($url, $params = [], $method = 'POST', $options = [])
+    {
+        $method = strtoupper($method);
+        $protocol = substr($url, 0, 5);
+        $query_string = is_array($params) ? http_build_query($params) : $params;
+
+        $ch = curl_init();
+        $defaults = [];
+        if ('GET' == $method) {
+            $geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url;
+            $defaults[CURLOPT_URL] = $geturl;
+        } else {
+            $defaults[CURLOPT_URL] = $url;
+            if ($method == 'POST') {
+                $defaults[CURLOPT_POST] = 1;
+            } else {
+                $defaults[CURLOPT_CUSTOMREQUEST] = $method;
+            }
+            $defaults[CURLOPT_POSTFIELDS] = is_array($params) && count(array_filter($params, 'is_array')) > 0 ? $query_string : $params;
+        }
+
+        $defaults[CURLOPT_HEADER] = false;
+        $defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
+        $defaults[CURLOPT_FOLLOWLOCATION] = true;
+        $defaults[CURLOPT_RETURNTRANSFER] = true;
+        $defaults[CURLOPT_CONNECTTIMEOUT] = 10;
+        $defaults[CURLOPT_TIMEOUT] = 10;
+
+        // disable 100-continue
+        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
+
+        if ('https' == $protocol) {
+            $defaults[CURLOPT_SSL_VERIFYPEER] = false;
+            $defaults[CURLOPT_SSL_VERIFYHOST] = false;
+        }
+
+        curl_setopt_array($ch, (array)$options + $defaults);
+
+        $ret = curl_exec($ch);
+        $err = curl_error($ch);
+
+        if (false === $ret || !empty($err)) {
+            $errno = curl_errno($ch);
+            $info = curl_getinfo($ch);
+            curl_close($ch);
+            return [
+                'ret'   => false,
+                'errno' => $errno,
+                'msg'   => $err,
+                'info'  => $info,
+            ];
+        }
+        curl_close($ch);
+        return [
+            'ret' => true,
+            'msg' => $ret,
+        ];
+    }
+
+    /**
+     * 异步发送一个请求
+     * @param string $url    请求的链接
+     * @param mixed  $params 请求的参数
+     * @param string $method 请求的方法
+     * @return boolean TRUE
+     */
+    public static function sendAsyncRequest($url, $params = [], $method = 'POST')
+    {
+        $method = strtoupper($method);
+        $method = $method == 'POST' ? 'POST' : 'GET';
+        //构造传递的参数
+        if (is_array($params)) {
+            $post_params = [];
+            foreach ($params as $k => &$v) {
+                if (is_array($v)) {
+                    $v = implode(',', $v);
+                }
+                $post_params[] = $k . '=' . urlencode($v);
+            }
+            $post_string = implode('&', $post_params);
+        } else {
+            $post_string = $params;
+        }
+        $parts = parse_url($url);
+        //构造查询的参数
+        if ($method == 'GET' && $post_string) {
+            $parts['query'] = isset($parts['query']) ? $parts['query'] . '&' . $post_string : $post_string;
+            $post_string = '';
+        }
+        $parts['query'] = isset($parts['query']) && $parts['query'] ? '?' . $parts['query'] : '';
+        //发送socket请求,获得连接句柄
+        $fp = fsockopen($parts['host'], $parts['port'] ?? 80, $errno, $errstr, 10);
+        if (!$fp) {
+            return false;
+        }
+        //设置超时时间
+        stream_set_timeout($fp, 10);
+        $out = "{$method} {$parts['path']}{$parts['query']} HTTP/1.1\r\n";
+        $out .= "Host: {$parts['host']}\r\n";
+        $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
+        $out .= "Content-Length: " . strlen($post_string) . "\r\n";
+        $out .= "Connection: Close\r\n\r\n";
+        if ($post_string !== '') {
+            $out .= $post_string;
+        }
+        fwrite($fp, $out);
+        //不用关心服务器返回结果
+        //echo fread($fp, 1024);
+        fclose($fp);
+        return true;
+    }
+
+    /**
+     * 发送文件到客户端
+     * @param string $file
+     * @param bool   $delaftersend
+     * @param bool   $exitaftersend
+     */
+    public static function sendToBrowser($file, $delaftersend = true, $exitaftersend = true)
+    {
+        if (file_exists($file) && is_readable($file)) {
+            header('Content-Description: File Transfer');
+            header('Content-Type: application/octet-stream');
+            header('Content-Disposition: attachment;filename = ' . basename($file));
+            header('Content-Transfer-Encoding: binary');
+            header('Expires: 0');
+            header('Cache-Control: must-revalidate, post-check = 0, pre-check = 0');
+            header('Pragma: public');
+            header('Content-Length: ' . filesize($file));
+            ob_clean();
+            flush();
+            readfile($file);
+            if ($delaftersend) {
+                unlink($file);
+            }
+            if ($exitaftersend) {
+                exit;
+            }
+        }
+    }
+}