FengSu.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. //抖音
  29. class FengSu extends Command
  30. {
  31. //抖音请求地址
  32. protected $url = 'https://fsdy2.fengsutb.com/amount/ship-list-new';
  33. //抖音请求头部
  34. protected $headers = [
  35. 'Host' => 'fsdy2.fengsutb.com',
  36. 'Accept' => 'application/json, text/plain, */*',
  37. 'Authorization' => 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxODYwNTQ1MTExNSIsImRhdGFfcGVybWlzc2lvbiI6InByb2R1Y3RDb3N0IiwibmlrZSI6IjE4NjA1NDUxMTE1IiwiY3JlYXRlZCI6MTc2NDEyNTQwMzQ0OSwiZHBJZCI6MTAwNjczLCJleHAiOjE3NjQ3MzAyMDMsImVuYWJsZWQiOnRydWUsImF1dGhvcml0aWVzIjpbInpodXlvbmdodSJdLCJkcEFjY291bnRJZCI6MTAwNjczfQ.ihv6NRfD2hzU1NQoqttTaZ-mPxqAEXIUICM8MuxVx2tVLEPW9j-uPhHoFz4T3-lH07wTt2ORf1k_SPOQP4PuQg',
  38. 'Content-Type' => 'application/json',
  39. // 'platform' => 'dy',
  40. ];
  41. // 打单平台 1:风速 2:聚水潭
  42. protected $type_id = 1;
  43. //时间间隔
  44. protected $start_end_space_time=10*60;
  45. //下一次时间间隔
  46. protected $next_time_space=7*60;
  47. // 指令配置
  48. protected function configure()
  49. {
  50. $this->setName('feng_su')->setDescription('the task command');
  51. }
  52. //根据shop_id
  53. protected function execute(Input $input, Output $output)
  54. {
  55. $output->writeln('开始同步订单');
  56. $resquet = $this->getOrderList('');
  57. if ($resquet['code'] == 0) {
  58. $fengsu=[];
  59. $fengsu=Cache::get('fengsu');
  60. if ($resquet['data']['list']!=null&&!empty($resquet['data']['list'])) {
  61. $fengsu_pageNum=$fengsu['pageNum'];
  62. $fengsu_pageNum=$fengsu_pageNum+1;
  63. $fengsu=[
  64. 'startTime'=>$fengsu['startTime'],
  65. 'endTime'=>$fengsu['endTime'],
  66. 'pageNum'=>$fengsu_pageNum,
  67. 'next_time_space'=>$fengsu['next_time_space']
  68. ];
  69. Cache::set('fengsu',$fengsu);
  70. $list = $resquet['data']['list'];
  71. // dump($list);
  72. // return;
  73. $this->insertOrder($list);
  74. //dump($resquet);
  75. } else {
  76. $now_time = time();
  77. if ($fengsu['startTime']>=$now_time||($now_time - $fengsu['startTime']) < ($this->next_time_space)) {
  78. // dump("超时了");
  79. $pre_time_stamp = $this->next_time_space;
  80. $startTime_stamp = $now_time - $pre_time_stamp;
  81. $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
  82. $fengsu = [
  83. 'startTime' => $startTime_stamp,
  84. 'endTime' => $endTime_stamp,
  85. 'pageNum' => 1,
  86. 'next_time_space' => $fengsu['next_time_space']
  87. ];
  88. }else{
  89. $fengsu = [
  90. 'startTime' => $fengsu['startTime'] + $fengsu['next_time_space'],
  91. 'endTime' => $fengsu['endTime'] + $fengsu['next_time_space'],
  92. 'pageNum' => 1,
  93. 'next_time_space' => $fengsu['next_time_space']
  94. ];
  95. }
  96. Cache::set('fengsu',$fengsu);
  97. $output->writeln('没有数据');
  98. }
  99. } else {
  100. $output->writeln('接口错误: ' . $resquet['message']);
  101. }
  102. $output->writeln('ok');
  103. }
  104. public function getOrderList($platform = '')
  105. {
  106. $client = new \GuzzleHttp\Client([
  107. 'verify' => false,
  108. // 或指定证书路径
  109. // 'verify' => '/path/to/cacert.pem'
  110. ]);
  111. $headers = [
  112. 'Accept' => ' application/json, text/plain, */*',
  113. 'Authorization' => site_config("addonsd.authorization_token"),
  114. 'Host' => ' fsdy2.fengsutb.com',
  115. 'platform' => $platform,
  116. 'Content-Type' => ' application/json'
  117. ];
  118. //时间间隔
  119. // $start_end_space_time=10*60;
  120. //下一次时间间隔
  121. // $next_time_space=7*60;
  122. //将查询分为,"2026-01-05 00:00:00~2026-01-05 00:05:00",查询该区间的订单,但是该时间区间单独数量可能超过50条,为了性能又不能一次性查询太多,从而进行分页查询,
  123. //查询不到数据,就往下一个时间区间查询
  124. $todayTime = date("Y-m-d");
  125. $startTime_stamp = strtotime($todayTime . ' 00:00:00');
  126. // $startTime_stamp = strtotime($todayTime . ' 07:20:00');
  127. $endTime_stamp=$startTime_stamp+$this->start_end_space_time;
  128. $pageNum=1;
  129. $fengsu=[
  130. 'startTime'=>$startTime_stamp,
  131. 'endTime'=>$endTime_stamp,
  132. 'pageNum'=>$pageNum,
  133. 'next_time_space'=>$this->next_time_space
  134. ];
  135. $fengsu_cache_data=Cache::get('fengsu');
  136. if(empty($fengsu_cache_data)||$fengsu_cache_data==null){
  137. Cache::set('fengsu',$fengsu);
  138. }
  139. $fengsu=Cache::get('fengsu');
  140. // 无数据时,获取数据的开始时间会大于现在的时间,这是一个错误的,会导致一直无法获取数据,开始时间一直往后
  141. //当开始时间大于现在时间,将开始时间向前调整3分钟
  142. $now_time = time();
  143. if ($fengsu['startTime']>=$now_time||($now_time - $fengsu['startTime']) < ($this->start_end_space_time - $this->next_time_space)) {
  144. // dump("超时了");
  145. $pre_time_stamp =$this->next_time_space;
  146. $startTime_stamp = $now_time - $pre_time_stamp;
  147. $endTime_stamp = $startTime_stamp + $this->start_end_space_time;
  148. $fengsu = [
  149. 'startTime' => $startTime_stamp,
  150. 'endTime' => $endTime_stamp,
  151. 'pageNum' => 1,
  152. 'next_time_space' => $this->next_time_space
  153. ];
  154. }
  155. Cache::set('fengsu', $fengsu);
  156. $startTime = date('Y-m-d H:i:s', $fengsu['startTime']);
  157. $endTime = date('Y-m-d H:i:s', $fengsu['endTime']);
  158. $pageNum=(int)$fengsu['pageNum'];
  159. // dump($startTime);
  160. // dump($fengsu);
  161. // $body = [
  162. // 'startTime' => $startTime,//时间区间
  163. // 'endTime' => $endTime,
  164. // 'sort' => 'CONSIGN_TIME',
  165. // 'sortAsc' => true,
  166. // 'current' => $pageNum,//页数
  167. // 'size' => 50,//每页多少条
  168. // 'tradeStatus' => 2,
  169. // 'fromAccountId' => [],
  170. // 'goodType' => 2,
  171. // 'goodsFlag' => 2
  172. // ];
  173. $body = [
  174. 'startTime' =>$startTime,
  175. // 'startTime' =>'2026-01-03 00:23:00',
  176. 'endTime' => $endTime,
  177. // 'endTime' => '2026-01-07 23:30:00',
  178. 'current' => $pageNum,
  179. // 'current' => 1,
  180. 'goodType' => 2,
  181. 'goodsFlag' => 1,
  182. 'orderPrefixSelect' => 'waybillNos',
  183. 'prepareShip'=>0,
  184. 'size' => 50,
  185. 'sort' => 'CONSIGN_TIME',
  186. 'sortAsc' => true,
  187. 'timeType' => 2,
  188. 'tradeStatus' => 2,
  189. 'skuOuterIdExact' => false,
  190. 'skuPropExact' => false,
  191. 'tradeSources' => []
  192. ];
  193. // 'waybillNos' => ['73589756512463']
  194. $body = json_encode($body);
  195. $request = new Request('POST', 'https://fsdy2.fengsutb.com/amount/ship-list-new', $headers, $body);
  196. // $request = new Request('POST', 'https://fsdy2.fengsutb.com/trade/print-list', $headers, $body);
  197. $res = $client->sendAsync($request)->wait();
  198. $json = [];
  199. if ($res->getStatusCode() == 200) {
  200. $json = json_decode($res->getBody()->getContents(), true);
  201. } else {
  202. dump($res->getStatusCode(), '接口返回结果');
  203. }
  204. return $json;
  205. }
  206. public function insertOrder($order_list)
  207. {
  208. if (!(count($order_list) > 0)) {
  209. return false;
  210. }
  211. $queue = [];
  212. $shopList = new ShopList();
  213. $importSku = new ImportSku();
  214. $productConfig = new ProductConfig();
  215. $shopDelivery = new ShopDelivery();
  216. $customerSpec = new CustomerSpec();
  217. $shopDelivery_list = [];
  218. foreach ($order_list as $item) {
  219. $goods_id = $item['tradeOrderPrintVos'][0]['goodsId'];
  220. $oid = $item['tradeOrderPrintVos'][0]['oid'];
  221. $sku_id = $item['tradeOrderPrintVos'][0]['skuId'];
  222. $waybill_no = $item['tradeOrderPrintVos'][0]['waybillNo'];
  223. if (empty($waybill_no)) continue;
  224. $add_status = JuShuiTanService::prevent_duplicate_additions($sku_id, $waybill_no);
  225. if ($add_status) continue;
  226. // $status = FengsuService::getAdditionalPrice( $shopList, $importSku, $productConfig, $shopDelivery, $customerSpec, $item);
  227. $res = FengsuService::getAdditionalPrice($shopList, $importSku, $productConfig, $shopDelivery, $customerSpec, $item);
  228. $status = $res['status'];
  229. $shopDelivery_list[] = $res['shopDelivery'];
  230. $weight = $item['tradeOrderPrintVos'][0]['weight'];
  231. $weight = strval($weight);
  232. $weight = bcdiv($weight, '500', 2);
  233. $weight = (float)$weight;
  234. $price = $item['tradeOrderPrintVos'][0]['payment'];
  235. $price = strval($price);
  236. $price = bcdiv($price, '100', 2);
  237. $price = (float)$price;
  238. $getPackSpecsFee = JuShuiTanService::getPackSpecsFee($importSku, $item['shopId'], $item['tradeOrderPrintVos'][0]['skuId'], $item['receiverProvince']);
  239. $queue[] = [
  240. 'type_id' => $this->type_id,
  241. 'shop_id' => $item['shopId'],
  242. 'order_id' => $oid,
  243. 'trade_from' => $item['tradeFrom'],
  244. 'province' => $item['receiverProvince'],
  245. 'city' => $item['receiverCity'],
  246. 'company_name' => $item['companyName'],
  247. 'waybill_no' => $waybill_no,
  248. 'consign_time' => $item['tradeOrderPrintVos'][0]['consignTime'],
  249. 'goods_id' => $goods_id,
  250. 'sku_id' => $item['tradeOrderPrintVos'][0]['skuId'],
  251. 'num' => $item['tradeOrderPrintVos'][0]['total'],
  252. 'goods_title' => $item['tradeOrderPrintVos'][0]['goodsTitle'],
  253. 'goods_info' => $item['tradeOrderPrintVos'][0]['skuProp'],
  254. 'weight' => $weight,
  255. 'price' => $price,
  256. 'status' => $status,
  257. 'specs_name' => $getPackSpecsFee['data']['title'],
  258. 'pack_specs_id' => $getPackSpecsFee['data']['id'],
  259. 'labor_cost_money' => $getPackSpecsFee['data']['labor_cost_money'],
  260. 'one_surcharge_money' => $getPackSpecsFee['one_surcharge_money'],
  261. 'two_surcharge_money' => $getPackSpecsFee['two_surcharge_money'],
  262. 'order_status' => $item['tradeOrderPrintVos'][0]['orderStatus']
  263. ];
  264. }
  265. // dump($queue);
  266. // 保存到数据库
  267. $shopDelivery->saveAll($shopDelivery_list);
  268. (new ImportList())->saveAll($queue);
  269. // 更新最后执行时间
  270. CrontabLog::create(['type_id' => 2, 'last_time' => date('Y-m-d H:i:s')]);
  271. }
  272. }
  273. //风速接口返回格式
  274. // {
  275. // "shopId": "DY117939700",
  276. // "sysNum": "3908161982177974",
  277. // "groupId": "g3908162034664709",
  278. // "tradeFrom": "dy",
  279. // "tid": "6948414820472788426",
  280. // "fromAccountId": 0,
  281. // "createTime": "2025-11-26 21:27:53",
  282. // "payTime": "2025-11-26 21:27:55",
  283. // "inserTime": "2025-11-27 15:11:27",
  284. // "inventoryPrintTime": null,
  285. // "consignTime": "2025-11-27 15:11:27",
  286. // "refunding": 0,
  287. // "buyerId": "1@#MhctHqbGY6MptrPvaPcNSU6JEPT1RwM+zhPxvrsCvBSVf2u9/qsla+xb9Fu81aUBMmCH",
  288. // "buyerNick": null,
  289. // "buyerMessage": "",
  290. // "sellerFlag": 0,
  291. // "localFlag": 0,
  292. // "sellerMemo": null,
  293. // "localMemo": null,
  294. // "offlineMemo": null,
  295. // "offlineFlag": null,
  296. // "receiverProvince": "黑龙江省",
  297. // "receiverCity": "哈尔滨市",
  298. // "receiverDistrict": "依兰县",
  299. // "receiverTown": "依兰镇",
  300. // "desensitizationName": "昕*",
  301. // "desensitizationPhone": null,
  302. // "desensitizationAddress": "*******",
  303. // "desensitizationMobile": "1**********",
  304. // "modifyAddr": 0,
  305. // "invoiceType": null,
  306. // "invoiceTitle": null,
  307. // "invoiceTaxId": null,
  308. // "promiseShipTime": "2025-12-01 21:27:56",
  309. // "tradeStatus": "WAIT_CONFIRM",
  310. // "payment": 9440,
  311. // "receiptAmount": 9440,
  312. // "postFee": 0,
  313. // "splitType": 1,
  314. // "packageNum": 1,
  315. // "dpAccountId": 100673,
  316. // "bizDpAccountId": 100673,
  317. // "uid": "117939700",
  318. // "bizShopId": "DY117939700",
  319. // "companyName": "中通快递",
  320. // "waybillNo": null,
  321. // "companyId": 3,
  322. // "templateId": 145375,
  323. // "printSize": 1,
  324. // "mergeHash": "6BC35CF08C74762098670BB71B38F77C387FB374",
  325. // "mergeHash2": "AC9CEFB7FD89A32A1AA235A9E09BD69FE838D08D",
  326. // "mergeable": 0,
  327. // "handDeliverFlag": 0,
  328. // "tradeAttr": "{\"promise_type\":1,\"recommend_logistics_list\":\"[]\"}",
  329. // "localAttr": null,
  330. // "senderAddressEntity": null,
  331. // "outChannel": null,
  332. // "tradeFlag": null,
  333. // "link": "",
  334. // "roleFlg": true,
  335. // "shopFlg": true,
  336. // "saleFlg": true,
  337. // "picUrlFlag": true,
  338. // "preSale": 1,
  339. // "preSaleTime": null,
  340. // "splitFailReason": null,
  341. // "businessTag": 0,
  342. // "locked": 0,
  343. // "tradeOrderPrintVos": [
  344. // {
  345. // "distTime": null,
  346. // "orderStatus": "WAIT_CONFIRM",
  347. // "goodsTitle": "田与薯云南圆圆果·榴莲蜜薯软糯蜜甜新鲜高原黄心番薯地瓜粗粮",
  348. // "goodsId": "3665081518029678296",
  349. // "skuProp": "{\"65\":\"小糖豆(50g-150g)\",\"66\":\"5斤(精品装(带箱5斤))\",\"67\":\"5天内发货\"}",
  350. // "companyName": "中通快递",
  351. // "waybillNo": "76767227765924",
  352. // "outerId": null,
  353. // "refundStatus": null,
  354. // "picUrl": "https://p3-aio.ecombdimg.com/obj/ecom-shop-material/jpeg_m_e379a8d857f16246745a757892497184_sx_320989_www1668-1668",
  355. // "total": 1,
  356. // "price": 3980,
  357. // "payment": 3780,
  358. // "receiptAmount": null,
  359. // "orderType": 0,
  360. // "consignTime": "2025-11-27 15:11:27",
  361. // "signTime": null,
  362. // "weight": 0,
  363. // "goodShortName": null,
  364. // "skuShortName": null,
  365. // "oid": "6948414820472788426",
  366. // "packageNum": 1,
  367. // "splitType": 1,
  368. // "companyId": 3,
  369. // "templateId": 145375,
  370. // "outerSkuId": "LFT-LLSPK-4.5-SC-CX",
  371. // "skuId": "3617103894193922",
  372. // "printSize": 1,
  373. // "printTime": "2025-11-27 15:11:22",
  374. // "roleType": 1,
  375. // "roleName": "田与薯官方旗舰店",
  376. // "lastFactoryId": 100673,
  377. // "inventoryPrintSize": 0,
  378. // "inventoryPrintTime": null,
  379. // "platCode": "ZJ",
  380. // "purchaseAttr": null,
  381. // "shelfPosition": "",
  382. // "pushTime": null,
  383. // "orderAttr": "{\"author_name\":\"田与薯官方旗舰店\",\"order_goods_type\":0,\"order_biz\":2,\"pre_sale_label\":\"全款预售\",\"compensate_for_rotten_platform_guarantee\":\"坏了包退官方保障\",\"quick_refund_label\":\"极速退\",\"unsupport_7days_refund\":\"不支持7天\",\"c_biz_self_sell\":\"小店自卖\",\"compass_source_not_ad_mark\":\"非广告\",\"compass_source_content_type_live\":\"直播\",\"afterSaleType\":\"refund\",\"source_platform\":\"\",\"period_purchase_info\":{},\"pack_id\":\"147242304405974025\"}",
  384. // "freeze": 0,
  385. // "freezeReason": null,
  386. // "auditStatus": 2,
  387. // "payStatus": null,
  388. // "settleStatus": null,
  389. // "settleRefundStatus": null,
  390. // "settleAmount": null,
  391. // "templateType": "1",
  392. // "productSkuName": null,
  393. // "productSkuCode": null
  394. // }
  395. // ],
  396. // "tradePrintList": null,
  397. // "platCode": "ZJ",
  398. // "purchaseId": null,
  399. // "purchaseStatus": null,
  400. // "supplierName": null,
  401. // "supplierId": null,
  402. // "purchaseName": null,
  403. // "offerId": null,
  404. // "specId": null,
  405. // "tradeClassify": "NORMAL",
  406. // "payType": "NORMAL",
  407. // "expressType": "NORMAL",
  408. // "msgTradeAttr": null,
  409. // "logisticsNames": null,
  410. // "logisticsTag": 0,
  411. // "tradeSource": null,
  412. // "printBatch": null,
  413. // "printBatchSn": 0,
  414. // "assignDeliveryId": null,
  415. // "factoryId": 100673,
  416. // "prepareShipGroupId": null,
  417. // "prepareShipDeliverStatus": null,
  418. // "prepareShipErrorMsg": null,
  419. // "prepareShipStatus": 0,
  420. // "prepareShipCreateTime": null,
  421. // "prepareShipUpdateTime": null,
  422. // "waybillEntities": [
  423. // {
  424. // "id": 7671095,
  425. // "dpAccountId": 100673,
  426. // "relationId": 3909499998804122,
  427. // "deliveryId": 3,
  428. // "waybillNo": "76767227765924",
  429. // "createdTime": "2025-11-27 15:11:27",
  430. // "recycleFlag": 0
  431. // }
  432. // ],
  433. // "relationId": 3909499998804122,
  434. // "optType": 0,
  435. // "obsStatus": 0,
  436. // "obsTime": null,
  437. // "obsReason": null,
  438. // "printFailReason": null
  439. // },