IntraCityController.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\wx\classes\WxMessageClass;
  4. use biz\wx\classes\WxOpenClass;
  5. use bizGhs\admin\classes\AdminClass;
  6. use bizGhs\express\classes\GhsExpressOrderClass;
  7. use bizGhs\shop\classes\ShopAdminClass;
  8. use bizHd\order\classes\OrderGoodsClass;
  9. use bizGhs\order\classes\OrderItemClass;
  10. use bizHd\purchase\classes\PurchaseClass;
  11. use common\components\dict;
  12. use common\components\imgUtil;
  13. use common\components\orderSn;
  14. use hd\models\IntraCity\StoreFeeForm;
  15. // 以上类来自零售端
  16. use bizGhs\order\classes\OrderClass;
  17. use biz\shop\classes\MainClass;
  18. use biz\shop\classes\ShopClass;
  19. use common\components\noticeUtil;
  20. use common\components\util;
  21. use common\components\IntraCityExpress;
  22. use yii\helpers\Json;
  23. use yii\db\Query;
  24. use Yii;
  25. /**
  26. * 同城配送控制器
  27. * 提供同城配送相关的API接口
  28. */
  29. class IntraCityController extends BaseController
  30. {
  31. public $guestAccess = ['callback'];
  32. //申请开通跑腿 ssh
  33. public function actionApply()
  34. {
  35. IntraCityExpress::applyStore();
  36. }
  37. /**
  38. * 创建门店
  39. * POST /intra-city/create-store
  40. */
  41. public function actionCreateStore()
  42. {
  43. $shop = $this->shop;
  44. $shopName = $shop->shopName ?? '';
  45. $sjName = $shop->merchantName ?? '';
  46. $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  47. if (empty($shop->latitude) || empty($shop->longitude)) {
  48. util::fail('没有经纬度,请修改地址');
  49. }
  50. if (empty($shop->address)) {
  51. util::fail('地址缺失,请修改');
  52. }
  53. $adminId = $this->adminId;
  54. util::checkRepeatCommit($adminId, 4);
  55. $storeData = [
  56. 'out_store_id' => $this->mainId, //自定义门店编号
  57. 'store_name' => $name,
  58. 'address_info' => [
  59. 'province' => $shop->province,
  60. 'city' => $shop->city,
  61. 'area' => $shop->dist,
  62. 'street' => $shop->address,
  63. 'house' => $shop->floor,
  64. 'lat' => $shop->lat,
  65. 'lng' => $shop->long,
  66. 'phone' => $shop->telephone
  67. ]
  68. ];
  69. // 验证必填参数
  70. $requiredFields = ['out_store_id', 'store_name', 'address_info'];
  71. foreach ($requiredFields as $field) {
  72. if (empty($storeData[$field])) {
  73. util::fail("缺少必填参数:{$field}");
  74. }
  75. }
  76. // 创建门店前,检查是否已经 开通门店权限 (有wxStoreId,即说明开通了)
  77. $main = $this->main;
  78. if (!empty($main->wxStoreId)) {
  79. util::fail('已经创建门店,请刷新');
  80. }
  81. $result = IntraCityExpress::createStore($storeData);
  82. if ($result['errcode'] === 0) {
  83. // 保存微信门店编号(wx_store_id)
  84. $main->wxStoreId = $result['wx_store_id'];
  85. $main->save();
  86. util::success($result, "门店创建成功");
  87. } else {
  88. $errMsg = $result['errmsg'] ?? '';
  89. Yii::error("门店创建失败:" . $errMsg);
  90. noticeUtil::push("跑腿创建门店报错:" . $errMsg, '15280215347');
  91. util::fail('创建失败,请联系客服');
  92. }
  93. }
  94. /**
  95. * 查询门店创建情况
  96. */
  97. public function actionStore()
  98. {
  99. $shopId = $this->shopId;
  100. // 首先从门店扩展表查询是否已经创建
  101. $main = MainClass::getById($this->mainId, true, 'id, wxStoreId');
  102. if ($main->wxStoreId == '') { // 不存在,则从微信接口查询
  103. $result = IntraCityExpress::getStore($this->mainId);
  104. if ($result['errcode'] === 0) {
  105. $store = $result['store_list'][0];
  106. // 保存 wx_store_id
  107. $main->wxStoreId = $store['wx_store_id'];
  108. $main->save();
  109. util::success($store, "门店查询成功");
  110. } else {
  111. Yii::error("门店查询失败:" . $result['errmsg']);
  112. util::error(-1, '门店查询失败');
  113. }
  114. } else {
  115. $field = 'province, city, dist, lat, long, floor, address';
  116. $shop = ShopClass::getById($shopId, false, $field);
  117. $data = array_merge($shop, $main->toArray());
  118. util::success($data, "门店查询成功");
  119. }
  120. }
  121. /**
  122. * 更新门店
  123. */
  124. public function actionUpdateStore()
  125. {
  126. // 首先从门店扩展表查询是否已经创建
  127. $main = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  128. if ($main['wxStoreId'] == '') {
  129. util::fail('未创建门店,请先创建');
  130. }
  131. $shop = $this->shop;
  132. $shopName = $shop->shopName ?? '';
  133. $sjName = $shop->merchantName ?? '';
  134. $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  135. $storeData = [
  136. 'keys' => ['wx_store_id' => $main['wxStoreId']],
  137. 'content' => [
  138. 'store_name' => $name,
  139. 'address_info' => [
  140. 'province' => $shop->province,
  141. 'city' => $shop->city,
  142. 'area' => $shop->dist,
  143. 'street' => '',
  144. 'house' => $shop->floor ? $shop->address . $shop->floor : $shop->address,
  145. 'lat' => $shop->lat,
  146. 'lng' => $shop->long,
  147. 'phone' => $shop->telephone
  148. ],
  149. //"order_pattern" => 2,
  150. //"service_trans_prefer" => "SFTC" //order_pattern = 2时必填
  151. ]
  152. ];
  153. $result = IntraCityExpress::updateStore($storeData);
  154. if ($result['errcode'] === 0) {
  155. util::success($result, "门店更新成功");
  156. } else {
  157. Yii::error("门店更新失败:" . $result['errmsg']);
  158. util::fail($result['errmsg']);
  159. }
  160. }
  161. /**
  162. * 门店运费充值
  163. */
  164. public function actionStoreCharge()
  165. {
  166. $post = Yii::$app->request->post();
  167. $amount = floatval($post['amount']);
  168. $amountTurnFen = $amount * 100;
  169. if ($amountTurnFen < 5000) {
  170. util::fail('50元起充');
  171. }
  172. $expressId = $post['expressId'];
  173. $serviceTransIds = ['SFTC', 'DADA'];
  174. if (!in_array($expressId, $serviceTransIds)) {
  175. util::fail('快递动力ID出错');
  176. }
  177. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  178. if ($shopExt['wxStoreId'] == '') {
  179. util::fail('微信门店编号为空');
  180. }
  181. $wxStoreId = $shopExt['wxStoreId'];
  182. $result = IntraCityExpress::storeCharge($wxStoreId, $expressId, $amountTurnFen);
  183. if ($result['errcode'] === 0) {
  184. util::success($result, "门店余额充值单已生成");
  185. } else {
  186. Yii::error("门店余额充值失败:" . $result['errmsg']);
  187. util::fail('门店余额充值失败');
  188. }
  189. }
  190. /**
  191. * 门店余额查询
  192. */
  193. public function actionStoreBalance()
  194. {
  195. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  196. if ($shopExt['wxStoreId'] == '') {
  197. util::error(-1, '微信门店编号为空');
  198. }
  199. $wxStoreId = $shopExt['wxStoreId'];
  200. $result = IntraCityExpress::getBalance($wxStoreId);
  201. if ($result['errcode'] === 0) {
  202. $data = ['all_balance' => $result['all_balance'] / 100.0];
  203. foreach ($result['balance_detail'] as $item) {
  204. if ($item['service_trans_id'] == 'DADA') {
  205. $data['dada_balance'] = $item['balance'] / 100.0;
  206. }
  207. if ($item['service_trans_id'] == 'SFTC') {
  208. $data['sf_balance'] = $item['balance'] / 100.0;
  209. }
  210. }
  211. util::success($data, "门店余额查询成功");
  212. } else {
  213. Yii::error("门店余额查询失败:" . $result['errmsg']);
  214. util::fail('门店余额查询失败');
  215. }
  216. }
  217. /**
  218. * 查询运费
  219. */
  220. public function actionStoreFee()
  221. {
  222. $post = Yii::$app->request->post();
  223. // 创建表单验证模型
  224. $form = new StoreFeeForm();
  225. $form->setScenario('store_fee');
  226. $form->load($post, ''); // 直接从 post 数据加载,不使用模型名作为前缀
  227. // 执行表单验证
  228. $form->validateForm();
  229. $orderId = $post['orderId'];
  230. $order = OrderClass::getById($orderId);
  231. if (empty($order)) {
  232. util::fail('订单出错');
  233. }
  234. if ($order['mainId'] != $this->mainId) {
  235. util::fail('订单出错');
  236. }
  237. $sn = $order['orderSn'];
  238. $shopId = intval($this->shopId);
  239. if (empty($shopId)) {
  240. util::fail("门店不存在");
  241. }
  242. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  243. if (empty($shopExt)) {
  244. util::fail("微信门店不存在");
  245. }
  246. $itemInfoList = OrderItemClass::getAllByCondition(['orderSn' => $sn], null, 'id, name, cover, num');
  247. $orderType = 2; // 默认是 2。说明:1花束订单 2花材订单 3花束和花材都有
  248. if (empty($itemInfoList)) {
  249. $orderType = 1;
  250. }
  251. $cargoName = '花材'; // 商品名称,默认是花材
  252. $goodsInfoList = OrderGoodsClass::getListBySn($sn);
  253. if (count($goodsInfoList) == 1) {
  254. $goodsInfo = $goodsInfoList[0];
  255. $cargoName = $goodsInfo['name'];
  256. } else {
  257. if ($orderType != 2) {
  258. util::fail("订单商品数量不正确");
  259. }
  260. }
  261. $cargo = [
  262. 'cargo_name' => $cargoName, // 商品名称 -- $order 中没有,要在 goodsInfo 中取
  263. 'cargo_weight' => intval($post['weight'] * 1000), // 单位:克 -- 把千克转换为克
  264. 'cargo_price' => intval($post['price'] * 100), // 单位:分 -- 把元转换为分
  265. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  266. 'cargo_num' => intval($post['packageNum'])
  267. ];
  268. $originalLng = doubleval($post['user_lng']);
  269. $originalLat = doubleval($post['user_lat']);
  270. $orderData = [
  271. //'out_store_id' => $shopId,
  272. 'wx_store_id' => $shopExt['wxStoreId'],
  273. 'user_name' => $post['user_name'],
  274. 'user_phone' => $post['user_phone'],
  275. 'user_lng' => $originalLng,
  276. 'user_lat' => $originalLat,
  277. 'user_address' => $post['user_address'],
  278. 'cargo' => $cargo,
  279. // 'use_sandbox' => getenv('YII_ENV') == 'production' ? 0 : 1, //正式环境不能传这个参数,会报错
  280. ];
  281. //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  282. $result = IntraCityExpress::preAddOrder($orderData);
  283. if ($result['errcode'] === 0) {
  284. util::success($result, "查询成功");
  285. } else {
  286. Yii::error("运费查询失败:" . json_encode($result));
  287. util::fail('运费查询失败,请确认是否超过20公斤,编号56698');
  288. }
  289. }
  290. //根据订单查询运费 ssh
  291. public function actionGetOrderFee()
  292. {
  293. $post = Yii::$app->request->post();
  294. $orderId = $post['id'];
  295. $order = OrderClass::getById($orderId, true);
  296. if (empty($order)) {
  297. util::fail('没有找到订单');
  298. }
  299. if ($order->mainId != $this->mainId) {
  300. util::fail('不是你的订单');
  301. }
  302. $main = $this->main;
  303. $wxStoreId = $main->wxStoreId ?? '';
  304. if (empty($wxStoreId)) {
  305. util::fail('你还没有开通跑腿');
  306. }
  307. $weight = $post['weight'] ?? 0;
  308. if ($weight <= 0) {
  309. util::fail('请填写重量');
  310. }
  311. $packageNum = $post['packageNum'] ?? 1;
  312. $price = $order->actPrice ?? 0;
  313. $cargo = [
  314. 'cargo_name' => '鲜花花材', // 商品名称 -- $order 中没有,要在 goodsInfo 中取
  315. 'cargo_weight' => intval($weight * 1000), // 单位:克 -- 把千克转换为克
  316. 'cargo_price' => intval($price * 100), // 单位:分 -- 把元转换为分
  317. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  318. 'cargo_num' => intval($packageNum)
  319. ];
  320. $originalLng = $order->long;
  321. $originalLat = $order->lat;
  322. if (empty($originalLng) || empty($originalLat)) {
  323. util::fail('客户地址不完整');
  324. }
  325. $customName = $order->customName ?? '';
  326. $customMobile = $order->customMobile ?? '';
  327. if (empty($customMobile)) {
  328. util::fail('客户手机缺失');
  329. }
  330. $address = $order->address ?? '';
  331. if (empty($address)) {
  332. util::fail('客户地址缺失');
  333. }
  334. //根据环境变量判断是否使用沙盒环境
  335. // $useSandBox = getenv('YII_ENV') == 'production' ? 0 : 1;
  336. $orderData = [
  337. 'wx_store_id' => $wxStoreId,
  338. 'user_name' => $customName,
  339. 'user_phone' => $customMobile,
  340. 'user_lng' => $originalLng,
  341. 'user_lat' => $originalLat,
  342. 'user_address' => $address,
  343. 'cargo' => $cargo,
  344. // 'use_sandbox' => $useSandBox,//正式环境不能传这个参数,会报错
  345. ];
  346. //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
  347. $result = IntraCityExpress::preAddOrder($orderData);
  348. if ($result['errcode'] === 0) {
  349. util::success($result, "查询成功");
  350. } else {
  351. $errMsg = $result['errmsg'] ?? '';
  352. $errCode = $result['errcode'] ?? '';
  353. noticeUtil::push("运费查询失败了:" . $errMsg . ",编号:" . $errCode, '15280215347');
  354. util::fail('查询失败,请确认量否超过20公斤,编号8930');
  355. }
  356. }
  357. /**
  358. * 创建订单
  359. * POST /intra-city/create-order
  360. */
  361. public function actionCreateOrder()
  362. {
  363. $post = Yii::$app->request->post();
  364. $adminId = $this->adminId;
  365. util::checkRepeatCommit($adminId, 4);
  366. $orderId = intval($post['orderId']);
  367. $order = OrderClass::getById($orderId, true);
  368. if (empty($order)) {
  369. util::fail('没有找到订单');
  370. }
  371. if ($order->mainId != $this->mainId) {
  372. util::fail('不是你的订单');
  373. }
  374. $packageNum = $post['packageNum'] ?? 0;
  375. if ($packageNum <= 0) {
  376. util::fail('请填写包裹数量');
  377. }
  378. $weight = $post['weight'] ?? 0;
  379. if ($weight <= 0) {
  380. util::fail('请填写重量');
  381. }
  382. $actPrice = $order->actPrice ?? 0;
  383. if ($actPrice <= 0) {
  384. util::fail('订单没有价格,不能发跑腿');
  385. }
  386. if ($order->status != 2) {
  387. util::fail('待发货订单才能发跑腿,请刷新页面');
  388. }
  389. //如果不是没鹛跑腿和取消了,则不能再叫跑腿,有多处,关键词no_call_express
  390. $notCanCreateSendStatus = [-2, 3];
  391. if (!in_array($order->sendStatus, $notCanCreateSendStatus)) {
  392. util::fail('不能叫跑腿,请刷新页面,编号800');
  393. }
  394. $cgId = $order->purchaseId ?? 0;
  395. $cgInfo = PurchaseClass::getById($cgId, true);
  396. if (empty($cgInfo)) {
  397. util::fail('没有采购信息');
  398. }
  399. $sn = $order->orderSn;
  400. $itemInfo = OrderItemClass::getByCondition(['orderSn' => $sn], null, 'id, name, cover, num');
  401. $num = intval($order->itemNum);
  402. $itemList[] = [
  403. 'item_name' => '花材',
  404. 'item_pic_url' => imgUtil::groupImg($itemInfo['cover']) . "?x-oss-process=image/resize,m_fill,h_130,w_130",
  405. 'count' => $num,
  406. ];
  407. $main = $this->main;
  408. $wxStoreId = $main->wxStoreId ?? '';
  409. if (empty($wxStoreId)) {
  410. util::fail('你还没有开通跑腿功能');
  411. }
  412. $cgId = $order->purchaseId ?? 0;
  413. if (empty($cgId)) {
  414. util::fail('没有找到采购信息');
  415. }
  416. $miniOpenId = '';
  417. $customShopAdminId = $order->customShopAdminId ?? 0;
  418. if (!empty($customShopAdminId)) {
  419. $customStaff = ShopAdminClass::getById($customShopAdminId, true);
  420. $customStaffAdminId = $customStaff->adminId ?? '';
  421. if (!empty($customStaffAdminId)) {
  422. $customAdmin = AdminClass::getById($customStaffAdminId, true);
  423. $miniOpenId = $customAdmin->miniOpenId ?? '';
  424. }
  425. }
  426. if (empty($miniOpenId)) {
  427. $admin = $this->admin;
  428. $miniOpenId = $admin->miniOpenId ?? '';
  429. }
  430. if (empty($miniOpenId)) {
  431. util::fail('没有找到微信登录信息,无法呼叫跑腿');
  432. }
  433. $cargoName = '鲜花花材';//商品名称
  434. $cargo = [
  435. 'cargo_name' => $cargoName,
  436. 'cargo_weight' => intval($weight * 1000),
  437. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  438. 'cargo_num' => intval($packageNum),
  439. 'cargo_price' => intval($actPrice * 100), // $order 中有各个价格,请注意
  440. 'item_list' => $itemList,
  441. ];
  442. $shopId = $this->shopId;
  443. $mainId = $this->mainId;
  444. $snData = ['shopId' => $shopId, 'mainId' => $mainId];
  445. $storeOrderId = orderSn::getGhsExpressSn($snData);
  446. $sendNum = $order->sendNum ?? '';
  447. $customName = $order->customName ?? '';
  448. if (empty($customName)) {
  449. util::fail('客户名称无效');
  450. }
  451. $customMobile = $order->customMobile ?? '';
  452. if (empty($customMobile)) {
  453. util::fail('客户手机号无效');
  454. }
  455. $long = $order->long ?? '';
  456. $lat = $order->lat ?? '';
  457. if (empty($long) || empty($lat)) {
  458. util::fail('客户地址无效');
  459. }
  460. $address = $order['address'] ?? '';
  461. if (empty($address)) {
  462. util::fail('客户地址无效呢');
  463. }
  464. $floor = $order['floor'] ?? '';
  465. $fullAddress = $address . $floor;
  466. $callbackUrl = Yii::$app->params['ghsHost'] . '/intra-city/callback';
  467. $orderData = [
  468. 'wx_store_id' => $wxStoreId,
  469. 'store_order_id' => $storeOrderId, //同一个门店订单编号要保证唯一,相同的订单号会重入
  470. 'user_openid' => $miniOpenId,
  471. 'user_lng' => (float)$long,
  472. 'user_lat' => (float)$lat,
  473. 'user_address' => $fullAddress,
  474. 'user_name' => $customName,
  475. 'user_phone' => $customMobile,
  476. 'order_seq' => $sendNum, // 用于配送员快速寻找到匹配的商品(非必传) -- 对应 xhOrder 表的 sendNum
  477. 'verify_code_type' => 0,
  478. 'order_detail_path' => '/pagesPurchase/purDetails?id=' . $cgId,
  479. 'callback_url' => $callbackUrl, // 订单状态回调地址(非必传)
  480. // 'use_sandbox' => 0,//正式环境不能传这个参数,会报错
  481. 'cargo' => $cargo
  482. ];
  483. $result = IntraCityExpress::createOrder($orderData);
  484. if (isset($result['errcode']) && $result['errcode'] === 0) {
  485. noticeUtil::push(json_encode($result), '15280215347');
  486. // 保存进 xhGhsExpressOrder 表
  487. $data = [
  488. 'mainId' => $this->mainId,
  489. 'shopId' => $this->shopId,
  490. 'deliveryId' => $result['service_trans_id'] ?? '',
  491. 'wxOrderId' => $result['wx_order_id'] ?? '',
  492. 'wxStoreId' => $result['wx_store_id'] ?? '',
  493. 'storeOrderId' => $result['store_order_id'] ?? '',
  494. 'orderId' => $orderId,
  495. 'transOrderId' => $result['trans_order_id'] ?? '',
  496. 'distance' => $result['distance'] ?? 0,
  497. 'fee' => $result['fee'] ?? 0,
  498. 'fetchCode' => $result['fetch_code'] ?? '',
  499. 'orderSeq' => $result['order_seq'] ?? '',
  500. 'status' => 1,//有订单可以取消
  501. 'orderStatus' => 0,//已提交申请,待确认
  502. ];
  503. GhsExpressOrderClass::add($data);
  504. $sendDistance = $data['distance'] ?? 0;
  505. $deliveryId = $data['deliveryId'] ?? '';
  506. //更新订单
  507. $order->sendDistance = $sendDistance;
  508. $order->sendStatus = -1;//已提交申请,待确认
  509. $order->deliveryId = $deliveryId;
  510. $order->sendType = 2;
  511. $order->save();
  512. $cgInfo->sendDistance = $sendDistance;
  513. $cgInfo->sendStatus = -1;//已提交申请,待确认
  514. $cgInfo->deliveryId = $deliveryId;
  515. $cgInfo->sendType = 2;
  516. $cgInfo->save();
  517. $params = [
  518. 'fhType' => 0,
  519. 'fhWl' => '',
  520. 'fhWlNo' => '',
  521. 'staffId' => 0,
  522. 'staffName' => '',
  523. ];
  524. //确认发货,花店会增加路上库存
  525. OrderClass::orderSend($order, $params);
  526. $totalFee = $result['fee'] ?? 0;
  527. $ghsName = $cgInfo->ghsName ?? '';
  528. noticeUtil::push("批发订单,发跑跑腿,获取运费:{$totalFee} 重量:{$weight} 距离:{$sendDistance} 订单号:{$orderId} 供货商:{$ghsName} 客户:{$customName} 客户手机:{$customMobile} 收货地址:{$fullAddress}", '15280215347');
  529. util::success(['fee' => $data['fee']], '订单创建成功');
  530. } else {
  531. noticeUtil::push("批发 -- " . $this->mainId . ",订单创建失败:" . json_encode($result));
  532. Yii::error("订单创建失败:" . ($result['errmsg'] ?? '未知错误'), 'intraCity');
  533. util::fail('提交失败,请确认余额是否充足');
  534. }
  535. }
  536. /**
  537. * 查询订单
  538. * GET /intra-city/get-order
  539. */
  540. public function actionGetOrder()
  541. {
  542. try {
  543. $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
  544. if (empty($shopExt)) {
  545. util::fail("微信门店不存在");
  546. }
  547. $post = Yii::$app->request->post();
  548. $wxOrderId = isset($post['wx_order_id']) ? $post['wx_order_id'] : '';
  549. $orderId = $post['orderId'];
  550. $wxStoreId = isset($post['wx_store_id']) ? $post['wx_store_id'] : $shopExt['wxStoreId'];
  551. if (empty($wxOrderId) && (empty($orderId) || empty($wxStoreId))) {
  552. util::fail("参数不足:wx_order_id 或 (order_id + wx_store_id) 必须提供一组");
  553. }
  554. $order = OrderClass::getById($orderId, false, 'id, mainId, sendStatus');
  555. if ($order['mainId'] != $this->mainId) {
  556. util::fail('订单未找到');
  557. }
  558. // 检查是否存在重复的订单:如果存在,则使用最后一个订单的 store_order_id
  559. $storeOrderId = $orderId; // ------------------------------------- 注意区分:$storeOrderId , $orderId 。它们有可能不相同
  560. $query = (new Query())
  561. ->from('xhGhsExpressOrder')
  562. ->where(['orderId' => $orderId])
  563. ->andWhere(['mainId' => $this->mainId]);
  564. $esDatas = $query->all();
  565. $count = count($esDatas);
  566. if ($count > 0) {
  567. $es = $esDatas[0];
  568. $storeOrderId = $es['storeOrderId'];
  569. }
  570. $result = IntraCityExpress::queryOrder($wxStoreId, $storeOrderId, $wxOrderId);
  571. if (isset($result['errcode']) && $result['errcode'] === 0) {
  572. util::success($result, "查询成功");
  573. } else {
  574. Yii::error("订单查询失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  575. util::fail('订单查询失败: ' . $result['errcode']);
  576. }
  577. } catch (\Exception $e) {
  578. Yii::error("订单查询异常:" . $e->getMessage(), 'intracity');
  579. util::fail("系统出错");
  580. }
  581. }
  582. /**
  583. * 取消订单
  584. * POST /intra-city/cancel-order
  585. */
  586. public function actionCancelOrder()
  587. {
  588. try {
  589. $post = Yii::$app->request->post();
  590. $adminId = $this->adminId;
  591. util::checkRepeatCommit($adminId, 4);
  592. $orderId = $post['orderId'] ?? 0;
  593. $order = OrderClass::getById($orderId, true);//todo 这时必须取全部字段,下面发货要用到,否则会出错,请勿修改
  594. if (empty($order)) {
  595. util::fail('没有找到订单');
  596. }
  597. if ($order->mainId != $this->mainId) {
  598. util::fail('不是你的订单');
  599. }
  600. if ($order->status != 3) {
  601. util::fail('不能取消');
  602. }
  603. if (in_array($order->sendStatus, [-2, 2, 3])) {
  604. //订单 没叫跑腿、已送达、已取消
  605. util::fail('不能取消哈,编号666');
  606. }
  607. $cgId = $order->purchaseId ?? 0;
  608. $cg = PurchaseClass::getById($cgId, true);
  609. if (empty($cg)) {
  610. util::fail('没有采购单信息');
  611. }
  612. $main = $this->main;
  613. $wxStoreId = $main->wxStoreId ?? '';
  614. $express = GhsExpressOrderClass::getByCondition(['orderId' => $orderId, 'status' => 1], true);
  615. if (empty($express)) {
  616. util::fail('没有找到有效配送单');
  617. }
  618. $wxOrderId = $express->wxOrderId ?? '';
  619. $storeOrderId = $express->storeOrderId ?? '';
  620. $result = IntraCityExpress::cancelOrder($wxOrderId, $storeOrderId, $wxStoreId);
  621. if (isset($result['errcode']) && $result['errcode'] === 0) {
  622. // 更新订单状态
  623. $order->sendStatus = 3;
  624. $order->save();
  625. $cg->sendStatus = 3;
  626. $cg->save();
  627. $params = ['staffId' => 0, 'staffName' => ''];
  628. OrderClass::cancelOrderSend($order, $params);
  629. $deductFee = $result['deductfee'] ?? 0;
  630. $express->status = 0;
  631. $express->cancelTime = date('Y-m-d H:i:s');
  632. $express->deductfee = $deductFee;
  633. $express->save();
  634. util::success($result, "订单取消成功");
  635. } else {
  636. Yii::error("订单取消失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  637. util::fail('取消失败,编号6693');
  638. }
  639. } catch (\Exception $e) {
  640. Yii::error("订单取消异常:" . $e->getMessage(), 'intraCity');
  641. util::fail("系统出错");
  642. }
  643. }
  644. /**
  645. * 微信回调接口
  646. */
  647. public function actionCallback()
  648. {
  649. $callbackData = Yii::$app->request->post();
  650. if (empty($callbackData)) {
  651. $postStr = file_get_contents('php://input');
  652. $callbackData = json_decode($postStr, true);
  653. if (empty($callbackData)) {
  654. util::fail('回调请求的数据为空');
  655. }
  656. }
  657. // 从配置中获取安全token
  658. // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  659. // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  660. // 记录回调日志
  661. Yii::info('同城配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'intraCity');
  662. $storeOrderId = $callbackData['store_order_id'] ?? 0;
  663. $wxStoreId = $callbackData['wx_store_id'] ?? ''; //微信门店编号
  664. if (empty($storeOrderId) || empty($wxStoreId)) {
  665. Yii::error("配送单回调接口的请求参数出错,storeOrderId = $storeOrderId, wxStoreId = $wxStoreId");
  666. return $this->asJson(['return_code' => 1, 'return_msg' => '请求参数不正确']);
  667. }
  668. try {
  669. // 使用 storeOrderId 与 wxStoreId 去查询 xhExpressOrder 表
  670. $eo = GhsExpressOrderClass::getByCondition(['storeOrderId' => $storeOrderId, 'wxStoreId' => $wxStoreId], true, false);
  671. if (empty($eo)) {
  672. Yii::error('订单不存在. store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId);
  673. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误']);
  674. }
  675. $orderId = $eo->orderId ?? 0;
  676. if (empty($orderId)) {
  677. $errRemind = '订单号为空,store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId;
  678. Yii::error($errRemind, 'intracity');
  679. noticeUtil::push($errRemind, '15280215347');
  680. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误2']);
  681. }
  682. $order = OrderClass::getById($orderId, true);//todo 注意,这里必须取全部字段,否则下面发错会出错!!!!!!!!
  683. if (empty($order)) {
  684. $errRemind = '没有找到订单,store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId;
  685. Yii::error($errRemind, 'intracity');
  686. noticeUtil::push($errRemind, '15280215347');
  687. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误3']);
  688. }
  689. $cgId = $order->purchaseId ?? 0;
  690. $cg = PurchaseClass::getById($cgId, true);
  691. if (empty($cg)) {
  692. $errRemind = '没有找到采购单,store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId;
  693. Yii::error($errRemind, 'intracity');
  694. noticeUtil::push($errRemind, '15280215347');
  695. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误4']);
  696. }
  697. $orderStatus = intval($callbackData['order_status']);
  698. //同个配送单,同个状态的,5秒内不允许重复请求,重要勿删
  699. $uniqueId = $storeOrderId . '_' . $orderStatus;
  700. util::checkRepeatCommit($uniqueId, 5);
  701. // ====================== 开启事务
  702. $tx = Yii::$app->db->beginTransaction();
  703. $msg = ''; // 发通知的内容(默认为空,根据情况生成。没有内容则不会发通知)
  704. switch ($orderStatus) {
  705. // 订单创建成功,已叫跑腿,等跑腿接单
  706. case IntraCityExpress::ORDER_STATUS_CREATED:
  707. if ($order->status == 3) {
  708. $eo->orderStatus = 1;//已叫跑腿,等跑腿接单
  709. $eo->save();
  710. $order->sendStatus = 0;//已叫跑腿,等跑腿接单
  711. $order->save();
  712. $cg->sendStatus = 0;//已叫跑腿,等跑腿接单
  713. $cg->save();
  714. }
  715. break;
  716. // 配送员接单
  717. case IntraCityExpress::ORDER_STATUS_ACCEPTED:
  718. if ($order->status == 3) {
  719. $eo->orderStatus = 4;//跑腿已接单
  720. $eo->save();
  721. $order->sendStatus = 1;//跑腿已接单
  722. $order->save();
  723. $cg->sendStatus = 1;//跑腿已接单
  724. $cg->save();
  725. }
  726. break;
  727. // 配送员到店
  728. case IntraCityExpress::ORDER_STATUS_ARRIVED:
  729. if ($order->status == 3) {
  730. $eo->orderStatus = 5;//跑腿到店
  731. $eo->save();
  732. $order->sendStatus = 4;//跑腿到店
  733. $order->save();
  734. $cg->sendStatus = 4;//跑腿到店
  735. $cg->save();
  736. }
  737. break;
  738. // 配送中
  739. case IntraCityExpress::ORDER_STATUS_DELIVERING:
  740. if ($order->status == 3) {
  741. $eo->orderStatus = 6;//跑腿配送中
  742. $eo->save();
  743. $order->sendStatus = 5;//跑腿配送中
  744. $order->save();
  745. $cg->sendStatus = 5;//跑腿配送中
  746. $cg->save();
  747. }
  748. break;
  749. // 配送完成
  750. case IntraCityExpress::ORDER_STATUS_COMPLETED:
  751. if ($order->status == 3) {
  752. $eo->orderStatus = 8;//配送完成
  753. $eo->save();
  754. $order->sendStatus = 2;//已送达
  755. $order->save();
  756. $cg->sendStatus = 2;//已送达
  757. $cg->save();
  758. $params = ['staffId' => 0, 'staffName' => ''];
  759. OrderClass::confirmReach($order, $params);
  760. }
  761. break;
  762. // 商家取消订单
  763. case IntraCityExpress::ORDER_STATUS_CANCELED_BY_MERCHANT:
  764. //取消已发货,回滚到待发货
  765. if ($order->status == 3) {
  766. $eo->orderStatus = 2;
  767. $eo->save();
  768. $order->sendStatus = 3;
  769. $order->save();
  770. $cg->sendStatus = 3;
  771. $cg->save();
  772. $params = ['staffId' => 0, 'staffName' => ''];
  773. OrderClass::cancelOrderSend($order, $params);
  774. $msg = '取消配送,订单编号 ';
  775. }
  776. break;
  777. // 配送方取消订单
  778. case IntraCityExpress::ORDER_STATUS_CANCELED_BY_DELIVERY:
  779. //取消已发货,回滚到待发货
  780. if ($order->status == 3) {
  781. $eo->orderStatus = 3;
  782. $eo->status = 0;
  783. $eo->save();
  784. $order->sendStatus = 3;
  785. $order->save();
  786. $cg->sendStatus = 3;
  787. $cg->save();
  788. $params = ['staffId' => 0, 'staffName' => ''];
  789. OrderClass::cancelOrderSend($order, $params);
  790. $msg = '配送方取消配送,订单编号 ';
  791. }
  792. break;
  793. // 配送员撤单
  794. case IntraCityExpress::ORDER_STATUS_WITHDRAWN:
  795. //todo 配送员撤单,目前我们认为会重新派单,先不要有任何操作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  796. $msg = '跑腿取消配送,订单编号 ';
  797. break;
  798. // 配送异常
  799. case IntraCityExpress::ORDER_STATUS_EXCEPTION:
  800. //取消已发货,回滚到待发货
  801. if ($order->status == 3) {
  802. $eo->orderStatus = 9;
  803. $eo->status = 0;
  804. $eo->save();
  805. $order->sendStatus = 3;
  806. $order->save();
  807. $cg->sendStatus = 3;
  808. $cg->save();
  809. $params = ['staffId' => 0, 'staffName' => ''];
  810. OrderClass::cancelOrderSend($order, $params);
  811. $msg = '订单配送异常,订单编号 ';
  812. }
  813. break;
  814. }
  815. // ====================== 事务提交
  816. $tx->commit();
  817. if (!empty($msg)) {
  818. $order = OrderClass::getById($orderId, true, 'id, shopId, customName, sendNum');
  819. $shop = ShopClass::getById($order->shopId, true, 'id, ptStyle, mainId');
  820. $result = $msg . $order->sendNum;
  821. WxMessageClass::expressErrorInform($shop, $order->customName, $orderId, $result);
  822. }
  823. // 成功状态的返回数据
  824. return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']);
  825. } catch (\Exception $e) {
  826. Yii::error('同城配送回调处理异常:' . $e->getMessage());
  827. return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误']);
  828. }
  829. }
  830. //模拟接口回调 ssh 20250826
  831. public function actionMockNotify()
  832. {
  833. if (getenv('YII_ENV') != 'production') {
  834. $post = Yii::$app->request->post();
  835. $orderStatus = $post['orderStatus'] ?? 0;
  836. $wxOrderId = $post['wxOrderId'] ?? '';
  837. $ptStyle = dict::getDict('ptStyle', 'hd');
  838. $merchant = WxOpenClass::getWxInfo();
  839. $ret = IntraCityExpress::mockNotify($orderStatus, $wxOrderId, '', '', $merchant, $ptStyle);
  840. util::success($ret);
  841. }
  842. util::complete();
  843. }
  844. }