|
|
@@ -1,4 +1,5 @@
|
|
|
<?php
|
|
|
+
|
|
|
/**
|
|
|
* ----------------------------------------------------------------------------
|
|
|
* 行到水穷处,坐看云起时
|
|
|
@@ -7,6 +8,7 @@
|
|
|
* Author: 老成
|
|
|
* email:85556713@qq.com
|
|
|
*/
|
|
|
+
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
namespace app\admin\command;
|
|
|
@@ -18,19 +20,24 @@ use think\console\Input;
|
|
|
use think\console\Output;
|
|
|
use GuzzleHttp\Client;
|
|
|
use app\common\model\CrontabLog;
|
|
|
-use app\common\model\FengsuSku;
|
|
|
+use app\common\model\ImportSku;
|
|
|
use app\common\model\ShopList;
|
|
|
use app\common\model\ProductConfig;
|
|
|
use app\common\model\ShopDelivery;
|
|
|
use app\common\model\CustomerSpec;
|
|
|
use app\admin\service\FengsuService;
|
|
|
-
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
+use app\common\model\ImportList;
|
|
|
+use app\admin\service\JuShuiTanService;
|
|
|
|
|
|
-// 小红书
|
|
|
+use think\facade\Cache;
|
|
|
+use app\common\controller\Http;
|
|
|
+
|
|
|
+use app\admin\command\FengSu;
|
|
|
+//小红书
|
|
|
class TaskXhs extends Command
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
protected $url = 'https://fsxpt.fengsutb.com/amount/ship-list-new';
|
|
|
|
|
|
protected $headers = [
|
|
|
@@ -54,80 +61,202 @@ class TaskXhs extends Command
|
|
|
"Sec-Fetch-Site" => "same-site",
|
|
|
"User-Agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0"
|
|
|
];
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+ protected $platform='xhs';
|
|
|
+
|
|
|
+ // 打单平台 1:风速 2:聚水潭
|
|
|
+ protected $type_id = 1;
|
|
|
+
|
|
|
+ //时间间隔
|
|
|
+ protected $start_end_space_time = 10 * 60;
|
|
|
+ //下一次时间间隔
|
|
|
+ protected $next_time_space = 7 * 60;
|
|
|
+
|
|
|
+ protected $fengSu_command=null;
|
|
|
+
|
|
|
// 指令配置
|
|
|
protected function configure()
|
|
|
{
|
|
|
- $this->setName('task:xhs')->setDescription('the task command');
|
|
|
+
|
|
|
+ $this->setName('task_'.$this->platform)->setDescription('the task command');
|
|
|
}
|
|
|
|
|
|
//根据shop_id
|
|
|
protected function execute(Input $input, Output $output)
|
|
|
{
|
|
|
- $client = new Client([ 'verify' => false]);
|
|
|
- $last_time = CrontabLog::getLastRunTime(5);
|
|
|
- $body = '{
|
|
|
- "goodType": 2,
|
|
|
- "goodsFlag": 1,
|
|
|
- "current": 1,
|
|
|
- "size": 25,
|
|
|
- "startTime": "'.$last_time.'",
|
|
|
- "endTime": "'.date('Y-m-d H:i:s').'",
|
|
|
- "skuOuterIdExact": false,
|
|
|
- "skuPropExact": false,
|
|
|
- "sort": "CONSIGN_TIME",
|
|
|
- "sortAsc": false,
|
|
|
- "timeType": 2,
|
|
|
- "tradeStatus": 2
|
|
|
- }';
|
|
|
- //获取Token
|
|
|
- $this->headers['Authorization'] = site_config('addonsd.authorization_token');
|
|
|
- $request = new Request('POST', $this->url, $this->headers, $body);
|
|
|
- $res = $client->sendAsync($request)->wait();
|
|
|
- if($res->getStatusCode() == 200) {
|
|
|
- $data = json_decode($res->getBody()->getContents(), true);
|
|
|
- $list = $data['data']['list'];
|
|
|
- if(empty($list)) {
|
|
|
- $output->writeln('无数据');
|
|
|
- return;
|
|
|
- }
|
|
|
- $queue = [];
|
|
|
- $shopList = new ShopList();
|
|
|
- $fengsuSku = new FengsuSku();
|
|
|
- $productConfig = new ProductConfig();
|
|
|
- $shopDelivery = new ShopDelivery();
|
|
|
- $customerSpec = new CustomerSpec();
|
|
|
- foreach($list as $item) {
|
|
|
- $status = FengsuService::getAdditionalPrice( $shopList, $fengsuSku, $productConfig, $shopDelivery, $customerSpec, $item);
|
|
|
- $queue[] = [
|
|
|
- 'shop_id' => $item['shopId'],
|
|
|
- 'trade_from' => $item['tradeFrom'],
|
|
|
- 'province' => $item['receiverProvince'],
|
|
|
- 'city' => $item['receiverCity'],
|
|
|
- 'company_name' => $item['tradeOrderPrintVos'][0]['companyName'],
|
|
|
- 'waybill_no' => $item['waybillNo'],
|
|
|
- 'consign_time' => $item['consignTime'],
|
|
|
- 'goods_id' => $item['tradeOrderPrintVos'][0]['goodsId'],
|
|
|
- 'sku_id' => $item['tradeOrderPrintVos'][0]['skuId'],
|
|
|
- 'num' => $item['tradeOrderPrintVos'][0]['total'],
|
|
|
- 'goods_title' => $item['tradeOrderPrintVos'][0]['goodsTitle'],
|
|
|
- 'weigh' => $item['tradeOrderPrintVos'][0]['weight'],
|
|
|
- 'price' => bcdiv((string)$item['payment'], '100', 2),
|
|
|
- 'status' => $status,
|
|
|
+ $output->writeln('开始同步订单');
|
|
|
+ $this->fengSu_command=new FengSu();
|
|
|
+ $resquet = $this->getOrderList('');
|
|
|
+ if ($resquet['code'] == 0) {
|
|
|
+ $fengsu = [];
|
|
|
+ $fengsu = Cache::get('fengsu_'.$this->platform);
|
|
|
+ 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_'.$this->platform, $fengsu);
|
|
|
+
|
|
|
+
|
|
|
+ $list = $resquet['data']['list'];
|
|
|
+ // dump($list);
|
|
|
+ // return;
|
|
|
+ $this->fengSu_command->insertOrder($list);
|
|
|
+ //dump($resquet);
|
|
|
+ } else {
|
|
|
+
|
|
|
+ $now_time = time();
|
|
|
+ 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 = [
|
|
|
+ 'startTime' => $startTime_stamp,
|
|
|
+ 'endTime' => $endTime_stamp,
|
|
|
+ 'pageNum' => 1,
|
|
|
+ 'next_time_space' => $fengsu['next_time_space']
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ $fengsu = [
|
|
|
+ 'startTime' => $fengsu['startTime'] + $fengsu['next_time_space'],
|
|
|
+ 'endTime' => $fengsu['endTime'] + $fengsu['next_time_space'],
|
|
|
+ 'pageNum' => 1,
|
|
|
+ 'next_time_space' => $fengsu['next_time_space']
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Cache::set('fengsu_'.$this->platform, $fengsu);
|
|
|
+
|
|
|
+
|
|
|
+ $output->writeln('没有数据');
|
|
|
}
|
|
|
-
|
|
|
- // 保存到数据库
|
|
|
- (new FengsuShip())->saveAll($queue);
|
|
|
- // 更新最后执行时间
|
|
|
- CrontabLog::create(['type_id' => 5,'last_time' => date('Y-m-d H:i:s')]);
|
|
|
+ } else {
|
|
|
+ $output->writeln('接口错误: ' . $resquet['message']);
|
|
|
}
|
|
|
|
|
|
- // 指令输出
|
|
|
$output->writeln('ok');
|
|
|
}
|
|
|
|
|
|
+ public function getOrderList($platform = 'pdd')
|
|
|
+ {
|
|
|
+ $client = new \GuzzleHttp\Client([
|
|
|
+ 'verify' => false,
|
|
|
+ // 或指定证书路径
|
|
|
+ // 'verify' => '/path/to/cacert.pem'
|
|
|
+ ]);
|
|
|
+
|
|
|
+
|
|
|
+ $this->headers['Authorization']=site_config("addonsd.authorization_token");
|
|
|
+
|
|
|
+ //时间间隔
|
|
|
+ // $start_end_space_time=10*60;
|
|
|
+ //下一次时间间隔
|
|
|
+ // $next_time_space=7*60;
|
|
|
+
|
|
|
+
|
|
|
+ //将查询分为,"2026-01-05 00:00:00~2026-01-05 00:05:00",查询该区间的订单,但是该时间区间单独数量可能超过50条,为了性能又不能一次性查询太多,从而进行分页查询,
|
|
|
+ //查询不到数据,就往下一个时间区间查询
|
|
|
+ $todayTime = date("Y-m-d");
|
|
|
+
|
|
|
+ $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
|
|
|
+ ];
|
|
|
+
|
|
|
+ $fengsu_cache_data = Cache::get('fengsu_'.$this->platform);
|
|
|
+
|
|
|
+ if (empty($fengsu_cache_data) || $fengsu_cache_data == null) {
|
|
|
+ Cache::set('fengsu_'.$this->platform, $fengsu);
|
|
|
+ }
|
|
|
+ $fengsu = Cache::get('fengsu_'.$this->platform);
|
|
|
+
|
|
|
+ // 无数据时,获取数据的开始时间会大于现在的时间,这是一个错误的,会导致一直无法获取数据,开始时间一直往后
|
|
|
+ //当开始时间大于现在时间,将开始时间向前调整3分钟
|
|
|
+ $now_time = time();
|
|
|
+ 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;
|
|
|
+
|
|
|
+ $startTime_stamp = $now_time - $pre_time_stamp;
|
|
|
+ $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
|
|
|
+ $fengsu = [
|
|
|
+ 'startTime' => $startTime_stamp,
|
|
|
+ 'endTime' => $endTime_stamp,
|
|
|
+ 'pageNum' => 1,
|
|
|
+ 'next_time_space' => $this->next_time_space
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ Cache::set('fengsu_'.$this->platform, $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'];
|
|
|
+ // dump($startTime);
|
|
|
+ $body = [
|
|
|
+ 'startTime' => $startTime,
|
|
|
+ 'endTime' => $endTime,
|
|
|
+ 'current' => $pageNum,
|
|
|
+ 'goodType' => 2,
|
|
|
+ 'goodsFlag' => 1,
|
|
|
+ 'orderPrefixSelect' => 'waybillNos',
|
|
|
+ 'prepareShip' => 0,
|
|
|
+ 'size' => 100,
|
|
|
+ 'sort' => 'CONSIGN_TIME',
|
|
|
+ 'sortAsc' => true,
|
|
|
+ 'timeType' => 2,
|
|
|
+ 'tradeStatus' => 2,
|
|
|
+ 'skuOuterIdExact' => false,
|
|
|
+ 'skuPropExact' => false,
|
|
|
+ 'tradeSources' => []
|
|
|
+ ];
|
|
|
+ // 'waybillNos' => ['73589756512463']
|
|
|
+ $body = json_encode($body);
|
|
|
|
|
|
+ $request = new Request('POST', $this->url, $this->headers, $body);
|
|
|
+ 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->fengSu_command->set_fengsu_Authorization($client);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var_dump($e->getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|