TaskTb.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare(strict_types=1);
  11. namespace app\admin\command;
  12. use app\common\model\FengsuShip;
  13. use think\console\Command;
  14. use think\console\Input;
  15. use think\console\Output;
  16. use GuzzleHttp\Client;
  17. use app\common\model\CrontabLog;
  18. use app\common\model\ImportSku;
  19. use app\common\model\ShopList;
  20. use app\common\model\ProductConfig;
  21. use app\common\model\ShopDelivery;
  22. use app\common\model\CustomerSpec;
  23. use app\admin\service\FengsuService;
  24. use GuzzleHttp\Psr7\Request;
  25. use app\common\model\ImportList;
  26. use app\admin\service\JuShuiTanService;
  27. use think\facade\Cache;
  28. use app\common\controller\Http;
  29. use app\admin\command\FengSu;
  30. //淘宝
  31. class TaskTb extends Command
  32. {
  33. //淘宝请求地址
  34. protected $url = 'https://fszntb.zhifeiyun.cn/amount/ship-list-new';
  35. //请求头部
  36. protected $headers = [
  37. 'Accept'=> "application/json, text/plain, */*",
  38. 'Accept-Encoding'=> "gzip, deflate, br, zstd",
  39. 'Accept-Language'=> "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
  40. "Authorization"=> "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZmEiLCJuaWtlIjoi6Zi_5Y-R5rWL6YCfIiwiY3JlYXRlZCI6MTc1ODc5MDI4NDU3OSwiZHBJZCI6MTkxOTQwLCJleHAiOjE3NTkwNDk0ODQsImVuYWJsZWQiOnRydWUsImF1dGhvcml0aWVzIjpbInpodXlvbmdodSJdLCJkcEFjY291bnRJZCI6MTc4MzU3fQ.TuHvPz7Jf0vuYsbrxX9VpyZ2oOrtZ0YzLQJafoX_RV8up1PmtPLfPATBE93_v_1Ig39nODadaTb4aePy3b0hsA",
  41. "Cache-Control"=> "no-cache",
  42. "Content-Type"=> "application/json",
  43. "Origin"=> "https://fx.fengsutb.com",
  44. "platform" => "tb",
  45. "Pragma"=> "no-cache",
  46. "priority" => "u=1, i",
  47. "Referer"=> "https://fx.fengsutb.com/",
  48. "Sec-ch-ua" => '"Chromium";v="140", "Not=A?Brand";v="24", "Microsoft Edge";v="140"',
  49. "Sec-ch-ua-Mobile"=> "?0",
  50. "Sec-ch-ua-platform"=> "Windows",
  51. "Sec-Fetch-Dest" => "empty",
  52. "Sec-Fetch-Mode" => "cors",
  53. "Sec-Fetch-Site" => "same-site",
  54. "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"
  55. ];
  56. protected $platform='tb';
  57. // 打单平台 1:风速 2:聚水潭
  58. protected $type_id = 1;
  59. //时间间隔
  60. protected $start_end_space_time = 10 * 60;
  61. //下一次时间间隔
  62. protected $next_time_space = 7 * 60;
  63. protected $fengSu_command=null;
  64. // 指令配置
  65. protected function configure()
  66. {
  67. $this->setName('task_'.$this->platform)->setDescription('the task command');
  68. }
  69. //根据shop_id
  70. protected function execute(Input $input, Output $output)
  71. {
  72. $output->writeln('开始同步订单');
  73. $this->fengSu_command=new FengSu();
  74. $resquet = $this->getOrderList('');
  75. if ($resquet['code'] == 0) {
  76. $fengsu = [];
  77. $fengsu = Cache::get('fengsu_'.$this->platform);
  78. if ($resquet['data']['list'] != null && !empty($resquet['data']['list'])) {
  79. $fengsu_pageNum = $fengsu['pageNum'];
  80. $fengsu_pageNum = $fengsu_pageNum + 1;
  81. $fengsu = [
  82. 'startTime' => $fengsu['startTime'],
  83. 'endTime' => $fengsu['endTime'],
  84. 'pageNum' => $fengsu_pageNum,
  85. 'next_time_space' => $fengsu['next_time_space']
  86. ];
  87. Cache::set('fengsu_'.$this->platform, $fengsu);
  88. $list = $resquet['data']['list'];
  89. // dump($list);
  90. // return;
  91. $this->fengSu_command->insertOrder($list);
  92. //dump($resquet);
  93. } else {
  94. $now_time = time();
  95. if ($fengsu['startTime'] >= $now_time || ($now_time - $fengsu['startTime']) < ($this->next_time_space)) {
  96. // dump("超时了");
  97. $pre_time_stamp = $this->next_time_space;
  98. $startTime_stamp = $now_time - $pre_time_stamp;
  99. $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
  100. $fengsu = [
  101. 'startTime' => $startTime_stamp,
  102. 'endTime' => $endTime_stamp,
  103. 'pageNum' => 1,
  104. 'next_time_space' => $fengsu['next_time_space']
  105. ];
  106. } else {
  107. $fengsu = [
  108. 'startTime' => $fengsu['startTime'] + $fengsu['next_time_space'],
  109. 'endTime' => $fengsu['endTime'] + $fengsu['next_time_space'],
  110. 'pageNum' => 1,
  111. 'next_time_space' => $fengsu['next_time_space']
  112. ];
  113. }
  114. Cache::set('fengsu_'.$this->platform, $fengsu);
  115. $output->writeln('没有数据');
  116. }
  117. } else {
  118. $output->writeln('接口错误: ' . $resquet['message']);
  119. }
  120. $output->writeln('ok');
  121. }
  122. public function getOrderList($platform = '')
  123. {
  124. $client = new \GuzzleHttp\Client([
  125. 'verify' => false,
  126. // 或指定证书路径
  127. // 'verify' => '/path/to/cacert.pem'
  128. ]);
  129. $this->headers['Authorization']=site_config("addonsd.authorization_token");
  130. //时间间隔
  131. // $start_end_space_time=10*60;
  132. //下一次时间间隔
  133. // $next_time_space=7*60;
  134. //将查询分为,"2026-01-05 00:00:00~2026-01-05 00:05:00",查询该区间的订单,但是该时间区间单独数量可能超过50条,为了性能又不能一次性查询太多,从而进行分页查询,
  135. //查询不到数据,就往下一个时间区间查询
  136. $todayTime = date("Y-m-d");
  137. $startTime_stamp = strtotime($todayTime . ' 00:00:00');
  138. // $startTime_stamp = strtotime($todayTime . ' 07:20:00');
  139. $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
  140. $pageNum = 1;
  141. $fengsu = [
  142. 'startTime' => $startTime_stamp,
  143. 'endTime' => $endTime_stamp,
  144. 'pageNum' => $pageNum,
  145. 'next_time_space' => $this->next_time_space
  146. ];
  147. $fengsu_cache_data = Cache::get('fengsu_'.$this->platform);
  148. if (empty($fengsu_cache_data) || $fengsu_cache_data == null) {
  149. Cache::set('fengsu_'.$this->platform, $fengsu);
  150. }
  151. $fengsu = Cache::get('fengsu_'.$this->platform);
  152. // 无数据时,获取数据的开始时间会大于现在的时间,这是一个错误的,会导致一直无法获取数据,开始时间一直往后
  153. //当开始时间大于现在时间,将开始时间向前调整3分钟
  154. $now_time = time();
  155. if ($fengsu['startTime'] >= $now_time || ($now_time - $fengsu['startTime']) < ($this->start_end_space_time - $this->next_time_space)) {
  156. // dump("超时了");
  157. $pre_time_stamp = $this->next_time_space;
  158. $startTime_stamp = $now_time - $pre_time_stamp;
  159. $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
  160. $fengsu = [
  161. 'startTime' => $startTime_stamp,
  162. 'endTime' => $endTime_stamp,
  163. 'pageNum' => 1,
  164. 'next_time_space' => $this->next_time_space
  165. ];
  166. }
  167. Cache::set('fengsu_'.$this->platform, $fengsu);
  168. $startTime = date('Y-m-d H:i:s', $fengsu['startTime']);
  169. $endTime = date('Y-m-d H:i:s', $fengsu['endTime']);
  170. $pageNum = (int)$fengsu['pageNum'];
  171. // dump($startTime);
  172. $body = [
  173. 'startTime' => $startTime,
  174. 'endTime' => $endTime,
  175. 'current' => $pageNum,
  176. 'goodType' => 2,
  177. 'goodsFlag' => 1,
  178. 'orderPrefixSelect' => 'waybillNos',
  179. 'prepareShip' => 0,
  180. 'size' => 100,
  181. 'sort' => 'CONSIGN_TIME',
  182. 'sortAsc' => true,
  183. 'timeType' => 2,
  184. 'tradeStatus' => 2,
  185. 'skuOuterIdExact' => false,
  186. 'skuPropExact' => false,
  187. 'tradeSources' => []
  188. ];
  189. // 'waybillNos' => ['73589756512463']
  190. $body = json_encode($body);
  191. $request = new Request('POST', $this->url, $this->headers, $body);
  192. try {
  193. $res = $client->sendAsync($request)->wait();
  194. $json = [];
  195. if ($res->getStatusCode() == 200) {
  196. $json = json_decode($res->getBody()->getContents(), true);
  197. } else {
  198. dump($res->getStatusCode(), '接口返回结果');
  199. }
  200. return $json;
  201. } catch (\Exception $e) {
  202. $errorStr = $e->getMessage();
  203. // 定位response:位置
  204. $pos = strpos($errorStr, 'response:');
  205. if ($pos !== false) {
  206. // 从response:后开始截取
  207. $jsonPart = substr($errorStr, $pos + strlen('response:'));
  208. // 清理空白字符
  209. $jsonPart = preg_replace('/^\s+|\s+$/', '', $jsonPart);
  210. $jsonPart = json_decode($jsonPart, true);
  211. if (!empty($jsonPart)) {
  212. if ($jsonPart['code'] == 401 && $jsonPart['message'] == 'token失效') {
  213. $this->fengSu_command->set_fengsu_Authorization($client);
  214. }
  215. }
  216. }
  217. var_dump($e->getMessage());
  218. return null;
  219. }
  220. }
  221. }