TaskDy.php 8.9 KB

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