FengSu.php 19 KB

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