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