TaskJd.php 9.7 KB

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