IntraCityController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\express\classes\ExpressOrderClass;
  4. use bizHd\goods\classes\GoodsClass;
  5. use bizHd\order\classes\OrderClass;
  6. use bizHd\order\classes\OrderGoodsClass;
  7. use bizHd\order\classes\OrderItemClass;
  8. use bizHd\shop\classes\ShopExtClass;
  9. use bizHd\user\classes\UserClass;
  10. use common\components\noticeUtil;
  11. use common\components\util;
  12. use hd\models\IntraCity\StoreFeeForm;
  13. use Yii;
  14. use biz\shop\classes\ShopClass;
  15. use yii\helpers\Json;
  16. use yii\web\Response;
  17. use common\components\IntraCityExpress;
  18. /**
  19. * 同城配送控制器
  20. *
  21. * 提供同城配送相关的API接口
  22. */
  23. class IntraCityController extends BaseController
  24. {
  25. public $guestAccess = ['callback'];
  26. /**
  27. * 创建门店
  28. * POST /intra-city/create-store
  29. */
  30. public function actionCreateStore()
  31. {
  32. Yii::$app->response->format = Response::FORMAT_JSON;
  33. try {
  34. $shopId = intval($this->shopId);
  35. if (empty($shopId)) {
  36. $shopId = $this->shopId;
  37. }
  38. $shop = ShopClass::getById($shopId, true, 'id, merchantName, province, city, dist, address, floor, lat, long, telephone');
  39. $storeData = [
  40. 'out_store_id' => $shop->id,
  41. 'store_name' => $shop->merchantName,
  42. 'address_info' => [
  43. 'province' => $shop->province,
  44. 'city' => $shop->city,
  45. 'area' => $shop->dist,
  46. 'street' => $shop->address,
  47. 'house' => $shop->floor,
  48. 'lat' => $shop->lat,
  49. 'lng' => $shop->long,
  50. 'phone' => $shop->telephone
  51. ]
  52. ];
  53. // 验证必填参数
  54. $requiredFields = ['out_store_id', 'store_name', 'address_info'];
  55. foreach ($requiredFields as $field) {
  56. if (empty($storeData[$field])) {
  57. util::fail("缺少必填参数:{$field}");
  58. }
  59. }
  60. // 创建门店前,检查是否已经 开通门店权限 (有wxStoreId,即说明开通了)
  61. $shopExt = ShopExtClass::getByCondition(['shopId'=>$shopId], false, false, 'id, wxStoreId');
  62. // 查询是否存在 wx_store_id,不存在则执行apply
  63. if ($shopExt['wxStoreId'] == '') {
  64. $re = IntraCityExpress::applyStore();
  65. if (!is_array($re)) {
  66. $re = Json::decode($re, true);
  67. }
  68. if ($re['errcode'] != 0) {
  69. Yii::error("开通门店权限失败:" . $re['errmsg']);
  70. util::fail('开通门店权限失败');
  71. }
  72. }
  73. $result = IntraCityExpress::createStore($storeData);
  74. if ($result['errcode'] === 0) {
  75. util::success("门店创建成功", $result);
  76. } else {
  77. Yii::error("门店创建失败:" . $result['errmsg']);
  78. util::fail($result['errmsg']);
  79. }
  80. } catch (\Exception $e) {
  81. Yii::error("门店创建失败:" . $e->getMessage());
  82. util::fail("系统出错");
  83. }
  84. }
  85. /**
  86. * 查询门店创建情况
  87. */
  88. public function actionStore()
  89. {
  90. $shopId = intval($this->shopId);
  91. if (empty($shopId)) {
  92. util::fail("门店不存在");
  93. }
  94. $result = IntraCityExpress::getStore($shopId);
  95. if ($result['errcode'] === 0) {
  96. $store = $result['store_list'][0];
  97. util::success($store, "门店查询成功");
  98. } else {
  99. Yii::error("门店查询失败:" . $result['errmsg']);
  100. util::fail('门店查询失败');
  101. }
  102. }
  103. /**
  104. * 更新门店
  105. */
  106. public function actionUpdateStore()
  107. {
  108. $shopId = intval($this->shopId);
  109. if (empty($shopId)) {
  110. util::fail("门店不存在");
  111. }
  112. $shop = ShopClass::getById($shopId, true, 'id, merchantName, province, city, dist, address, floor, lat, long, telephone');
  113. $storeData = [
  114. 'out_store_id' => $shopId,
  115. 'store_name' => $shop->merchantName,
  116. 'address_info' => [
  117. 'province' => $shop->province,
  118. 'city' => $shop->city,
  119. 'area' => $shop->dist,
  120. 'street' => $shop->address,
  121. 'house' => $shop->floor,
  122. 'lat' => $shop->lat,
  123. 'lng' => $shop->long,
  124. 'phone' => $shop->telephone
  125. ]
  126. ];
  127. $result = IntraCityExpress::updateStore($shopId, $storeData);
  128. if ($result['errcode'] === 0) {
  129. util::success("门店更新成功", $result);
  130. } else {
  131. Yii::error("门店更新失败:" . $result['errmsg']);
  132. util::fail('门店更新失败');
  133. }
  134. }
  135. /**
  136. * 门店运费充值
  137. */
  138. public function actionStoreCharge()
  139. {
  140. $post = Yii::$app->request->post();
  141. $amount = floatval($post['amount']);
  142. $amountTurnFen = $amount * 100;
  143. if ($amountTurnFen < 5000) {
  144. util::fail('50元起充');
  145. }
  146. $serviceTransIds = ['SFTC', 'DADA'];
  147. $expressName = 'SFTC';
  148. if (isset($post['expressId']) && in_array($post['expressId'], $serviceTransIds)) {
  149. $expressName = $post['expressId'];
  150. }
  151. $shopId = intval($this->shopId);
  152. if (empty($shopId)) {
  153. util::fail("门店不存在");
  154. }
  155. $shopExt = ShopExtClass::getByCondition(['shopId'=>$shopId], false, false, 'id, wxStoreId');
  156. if ($shopExt['wxStoreId'] == '') {
  157. util::fail('微信门店编号为空');
  158. }
  159. $wxStoreId = $shopExt['wxStoreId'];
  160. $result = IntraCityExpress::storeCharge($wxStoreId, $expressName, $amountTurnFen);
  161. if ($result['errcode'] === 0) {
  162. util::success($result, "门店余额充值单已生成");
  163. } else {
  164. Yii::error("门店余额充值失败:" . $result['errmsg']);
  165. util::fail('门店余额充值失败');
  166. }
  167. }
  168. /**
  169. * 门店余额查询
  170. */
  171. public function actionStoreBalance()
  172. {
  173. $shopId = intval($this->shopId);
  174. if (empty($shopId)) {
  175. util::fail("门店不存在");
  176. }
  177. $shopId = intval($this->shopId);
  178. if (empty($shopId)) {
  179. util::fail("门店不存在");
  180. }
  181. $shopExt = ShopExtClass::getByCondition(['shopId'=>$shopId], false, false, 'id, wxStoreId');
  182. if ($shopExt['wxStoreId'] == '') {
  183. util::fail('微信门店编号为空');
  184. }
  185. $wxStoreId = $shopExt['wxStoreId'];
  186. $result = IntraCityExpress::getBalance($wxStoreId);
  187. if ($result['errcode'] === 0) {
  188. $data = ['all_balance' => $result['all_balance']/100.0];
  189. foreach ($result['balance_detail'] as $item) {
  190. if ($item['service_trans_id'] == 'DADA') {
  191. $data['dada_balance'] = $item['balance'] / 100.0;
  192. }
  193. if ($item['service_trans_id'] == 'SFTC') {
  194. $data['sf_balance'] = $item['balance'] / 100.0;
  195. }
  196. }
  197. util::success($data, "门店余额查询成功");
  198. } else {
  199. Yii::error("门店余额查询失败:" . $result['errmsg']);
  200. util::fail('门店余额查询失败');
  201. }
  202. }
  203. /**
  204. * 查询运费
  205. */
  206. public function actionStoreFee()
  207. {
  208. $post = Yii::$app->request->post();
  209. // 创建表单验证模型
  210. $form = new StoreFeeForm();
  211. $form->setScenario('store_fee');
  212. $form->load($post, ''); // 直接从 post 数据加载,不使用模型名作为前缀
  213. // 执行表单验证
  214. $form->validateForm();
  215. $orderId = $post['orderId'];
  216. $order = OrderClass::getById($orderId);
  217. if (empty($order)) {
  218. util::fail('订单出错');
  219. }
  220. if ($order['mainId'] != $this->mainId) {
  221. util::fail('订单出错');
  222. }
  223. $sn = $order['orderSn'];
  224. $shopId = intval($this->shopId);
  225. if (empty($shopId)) {
  226. util::fail("门店不存在");
  227. }
  228. $shopExt = ShopExtClass::getByCondition(['shopId'=>$shopId], false, false, 'id, wxStoreId');
  229. $itemInfoList = OrderItemClass::getListBySn($sn);
  230. $orderType = 2; // 默认是 2。说明:1花束订单 2花材订单 3花束和花材都有
  231. if (empty($itemInfoList)) {
  232. $orderType = 1;
  233. }
  234. $cargoName = '花材'; // 商品名称,默认是花材
  235. $goodsInfoList = OrderGoodsClass::getListBySn($sn);
  236. if (count($goodsInfoList) == 1) {
  237. $goodsInfo = $goodsInfoList[0];
  238. $cargoName = $goodsInfo['name'];
  239. } else {
  240. if ($orderType != 2) {
  241. util::fail("订单商品数量不正确");
  242. }
  243. }
  244. $cargo = [
  245. 'cargo_name' => $cargoName, // 商品名称 -- $order 中没有,要在 goodsInfo 中取
  246. 'cargo_weight' => intval($post['weight'] * 1000), // 单位:克 -- 把千克转换为克
  247. 'cargo_price' => intval($post['price'] * 100), // 单位:分 -- 把元转换为分
  248. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  249. 'cargo_num' => intval($post['packageNum'])
  250. ];
  251. // 将高德地图坐标转换为腾讯地图坐标
  252. $originalLng = doubleval($post['user_lng']);
  253. $originalLat = doubleval($post['user_lat']);
  254. $convertedCoord = \common\components\mapUtil::convertCoordinateByApi($originalLng, $originalLat);
  255. $orderData = [
  256. //'out_store_id' => $shopId,
  257. 'wx_store_id' => $shopExt['wxStoreId'],
  258. 'user_name' => $post['user_name'],
  259. 'user_phone' => $post['user_phone'],
  260. 'user_lng' => $convertedCoord['lng'],
  261. 'user_lat' => $convertedCoord['lat'],
  262. 'user_address' => $post['user_address'],
  263. 'cargo' => $cargo,
  264. //'use_sandbox' => 1 // 如果不需要沙箱,use_sandbox就不传就好了 -- 踩坑人的经验
  265. //'use_sandbox' => getenv('YII_ENV') == 'production' ? 0 : 1 // TODO 根据环境变量判断是否使用沙盒环境
  266. ];
  267. $result = IntraCityExpress::preAddOrder($orderData);
  268. if ($result['errcode'] === 0) {
  269. util::success($result, "运费查询成功");
  270. } else {
  271. Yii::error("运费查询失败:" . json_encode($result));
  272. util::fail('运费查询失败' . json_encode($result));
  273. }
  274. }
  275. /**
  276. * 创建订单
  277. * POST /intra-city/create-order
  278. */
  279. public function actionCreateOrder()
  280. {
  281. try {
  282. $post = Yii::$app->request->post();
  283. $orderId = $post['orderId'];
  284. $order = OrderClass::getById($orderId);
  285. if (empty($order)) {
  286. util::fail('订单出错');
  287. }
  288. if ($order['mainId'] != $this->mainId) {
  289. util::fail('订单出错');
  290. }
  291. $sn = $order['orderSn'];
  292. $itemInfoList = OrderItemClass::getListBySn($sn, 'id, name, cover, num');
  293. $itemList = [];
  294. $orderType = 2; // 默认是 2。说明:1花束订单 2花材订单 3花束和花材都有
  295. if (empty($itemInfoList)) {
  296. $orderType = 1;
  297. $goodsInfoList = OrderGoodsClass::getListBySn($sn);
  298. $itemCount = 0;
  299. foreach ($goodsInfoList as $itemInfo) {
  300. $itemList[] = [
  301. 'item_name' => $itemInfo['name'],
  302. 'item_pic_url' => $itemInfo['bigCover'],
  303. 'count' => $itemInfo['num'],
  304. ];
  305. $itemCount += $itemInfo['num'];
  306. }
  307. $post['packageNum'] = $itemCount;
  308. } else {
  309. $itemCount = 0;
  310. foreach ($itemInfoList as $itemInfo) {
  311. $itemList[] = [
  312. 'item_name' => $itemInfo['name'],
  313. 'item_pic_url' => $itemInfo['bigCover'],
  314. 'count' => $itemInfo['num'],
  315. ];
  316. $itemCount += $itemInfo['num'];
  317. }
  318. $post['packageNum'] = $itemCount;
  319. }
  320. $shopId = intval($this->shopId);
  321. if (empty($shopId)) {
  322. util::fail("门店不存在");
  323. }
  324. $shopExt = ShopExtClass::getByCondition(['shopId'=>$shopId], false, false, 'id, wxStoreId');
  325. $cargoName = '花材'; // 商品名称,默认是花材
  326. $goodsInfoList = OrderGoodsClass::getListBySn($sn);
  327. if (count($goodsInfoList) == 1) {
  328. $goodsInfo = $goodsInfoList[0];
  329. $cargoName = $goodsInfo['name'];
  330. } else {
  331. if ($orderType != 2) {
  332. Yii::error('订单商品数量不正确');
  333. util::fail("订单商品数量不正确");
  334. }
  335. }
  336. // 通过 userId 获取 openid(小程序OpenId)
  337. $user = UserClass::getById($order['userId'], false, 'id, miniOpenId');
  338. if (empty($user)) {
  339. Yii::error('获取用户openId出错');
  340. util::fail('数据出错');
  341. }
  342. $cargo = [
  343. 'cargo_name' => $cargoName,
  344. 'cargo_weight' => intval($post['weight'] * 1000),
  345. 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
  346. 'cargo_num' => intval($post['packageNum']),
  347. 'cargo_price' => intval($order['actPrice'] * 100), // $order 中有各个价格,请注意
  348. 'item_list' => $itemList,
  349. ];
  350. $callbackUrl = Yii::$app->params['hdHost'] . '/intra-city/callback';
  351. $orderData = [
  352. //'out_store_id' => $shopId,
  353. 'wx_store_id' => $shopExt['wxStoreId'],
  354. 'store_order_id' => $orderId, //同一个门店订单编号要保证唯一,相同的订单号会重入
  355. 'user_openid' => $user['miniOpenId'],
  356. 'user_lng' => $post['user_lng'],
  357. 'user_lat' => $post['user_lat'],
  358. 'user_address' => $post['user_address'],
  359. 'user_name' => $post['user_name'],
  360. 'user_phone' => $post['user_phone'],
  361. //'order_seq' => '', // 用于配送员快速寻找到匹配的商品(非必传)
  362. 'verify_code_type' => 0,
  363. 'order_detail_path' => '/admin/order/detail?id=' . $orderId, // TODO 使用花掌柜的地址,后期要重新选个地址
  364. 'callback_url' => $callbackUrl, // 订单状态回调地址(非必传)
  365. //'use_sandbox' => getenv('YII_ENV') == 'production' ? 0 : 1, // 是否使用沙箱(非必传)
  366. 'cargo' => $cargo
  367. ];
  368. $result = IntraCityExpress::createOrder($orderData);
  369. if (isset($result['errcode']) && $result['errcode'] === 0) {
  370. OrderClass::updateById($orderId, ['sendStatus'=>1]); // 更新订单状态为配送中
  371. // 保存进 xhExpressOrder 表
  372. $orderData = [
  373. 'mainId' => intval($this->mainId),
  374. 'shopId' => intval($this->shopId),
  375. 'deliveryId' => isset($result['service_trans_id']) ? $result['service_trans_id'] : '',
  376. 'wxOrderId' => isset($result['wx_order_id']) ? $result['wx_order_id'] : '',
  377. 'wxStoreId' => isset($result['wx_store_id']) ? $result['wx_store_id'] : '',
  378. 'storeOrderId' => isset($result['store_order_id']) ? $result['store_order_id'] : '',
  379. 'transOrderId' => isset($result['trans_order_id']) ? $result['trans_order_id'] : '',
  380. 'distance' => isset($result['distance']) ? $result['distance'] : 0,
  381. 'fee' => isset($result['fee']) ? $result['fee'] : 0,
  382. 'fetchCode' => isset($result['fetch_code']) ? $result['fetch_code'] : '',
  383. 'orderSeq' => isset($result['order_seq']) ? $result['order_seq'] : ''
  384. ];
  385. ExpressOrderClass::add($orderData);
  386. util::success('订单创建成功', ['fee' => $result['fee']]);
  387. } else {
  388. Yii::error("订单创建失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  389. util::fail($result['errmsg'] ?? '订单创建失败', $result['errcode'] ?? -1);
  390. }
  391. } catch (\Exception $e) {
  392. Yii::error("订单创建异常:" . $e->getMessage(), 'intracity');
  393. util::fail("系统出错");
  394. }
  395. }
  396. /**
  397. * 查询订单
  398. * GET /intra-city/get-order
  399. */
  400. public function actionGetOrder()
  401. {
  402. try {
  403. $shopId = intval($this->shopId);
  404. if (empty($shopId)) {
  405. util::fail("门店不存在");
  406. }
  407. $shopExt = ShopExtClass::getByCondition(['shopId'=>$shopId], false, false, 'id, wxStoreId');
  408. $post = Yii::$app->request->post();
  409. $wxOrderId = $post['wx_order_id'];
  410. $storeOrderId = $post['orderId']; // 即 orderId
  411. $wxStoreId = isset($post['wx_store_id']) ? $post['wx_order_id'] : $shopExt['wxStoreId'];
  412. if (empty($wxOrderId) && (empty($outOrderId) || empty($outStoreId))) {
  413. util::fail("参数不足:wx_order_id 或 (store_order_id + wx_store_id) 必须提供一组");
  414. }
  415. $order = OrderClass::getById($storeOrderId, false, 'id, mainId, sendStatus');
  416. if ($order['mainId'] != $this->mainId){
  417. util::fail('订单未找到');
  418. }
  419. $result = IntraCityExpress::queryOrder($wxStoreId, $storeOrderId, $wxOrderId);
  420. if (isset($result['errcode']) && $result['errcode'] === 0) {
  421. util::success("查询成功", $result);
  422. } else {
  423. Yii::error("订单查询失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  424. util::fail($result['errmsg'] ?? '订单查询失败', $result['errcode'] ?? -1);
  425. }
  426. } catch (\Exception $e) {
  427. Yii::error("订单查询异常:" . $e->getMessage(), 'intracity');
  428. util::fail("系统出错");
  429. }
  430. }
  431. /**
  432. * 取消订单
  433. * POST /intra-city/cancel-order
  434. */
  435. public function actionCancelOrder()
  436. {
  437. try {
  438. $post = Yii::$app->request->post();
  439. $shopId = intval($this->shopId);
  440. if (empty($shopId)) {
  441. util::fail("门店不存在");
  442. }
  443. $shopExt = ShopExtClass::getByCondition(['shopId'=>$shopId], false, false, 'id, wxStoreId');
  444. $wxOrderId = isset($post['wx_order_id']) ? $post['wx_order_id'] : '';
  445. $storeOrderId = $post['orderId']; //配送订单创建成功后,也会返回的 store_order_id。 即 orderId
  446. $wxStoreId = isset($post['wx_store_id']) ? $post['wx_order_id'] : $shopExt['wxStoreId'];
  447. if (empty($wxOrderId) && (empty($storeOrderId) || empty($wxStoreId))) {
  448. util::fail("参数不足:wx_order_id 或 (store_order_id + wx_store_id) 必须提供一组");
  449. }
  450. $order = OrderClass::getById($storeOrderId, false, 'id, mainId, sendStatus');
  451. if ($order['mainId'] != $this->mainId){
  452. util::fail('订单未找到');
  453. }
  454. if ($order['sendStatus'] != 1) {
  455. util::fail('配送单状态不是配送中');
  456. }
  457. $result = IntraCityExpress::cancelOrder($wxOrderId, $storeOrderId, $wxStoreId);
  458. if (isset($result['errcode']) && $result['errcode'] === 0) {
  459. // 更新订单状态
  460. OrderClass::updateById($storeOrderId, ['sendStatus'=>3]);//设置为取消状态
  461. $eo = ExpressOrderClass::getByCondition(['storeOrderId'=>$storeOrderId, 'wxStoreId'=>$wxStoreId], true);
  462. if(!empty($eo)) {
  463. $eo->status = 3;
  464. $eo->cancelTime = date('Y-m-d H:i:s');
  465. $eo->deductfee = $result['deductfee'];
  466. $eo->save();
  467. }
  468. util::success("订单取消成功", $result);
  469. } else {
  470. Yii::error("订单取消失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  471. util::fail($result['errmsg'] ?? '订单取消失败', $result['errcode'] ?? -1);
  472. }
  473. } catch (\Exception $e) {
  474. Yii::error("订单取消异常:" . $e->getMessage(), 'intracity');
  475. util::fail("系统出错");
  476. }
  477. }
  478. /**
  479. * 获取门店列表
  480. * GET /intra-city/store-list
  481. */
  482. public function actionStoreList()
  483. {
  484. Yii::$app->response->format = Response::FORMAT_JSON;
  485. try {
  486. $request = Yii::$app->request;
  487. $offset = $request->get('offset', 0);
  488. $limit = $request->get('limit', 20);
  489. $list = [];
  490. if ($list) {
  491. } else {
  492. }
  493. } catch (\Exception $e) {
  494. return [
  495. 'success' => false,
  496. 'message' => '系统错误:' . $e->getMessage()
  497. ];
  498. }
  499. }
  500. /**
  501. * 获取订单列表
  502. * GET /intra-city/order-list
  503. */
  504. public function actionOrderList()
  505. {
  506. Yii::$app->response->format = Response::FORMAT_JSON;
  507. try {
  508. $request = Yii::$app->request;
  509. $offset = $request->get('offset', 0);
  510. $limit = $request->get('limit', 20);
  511. $result = ExpressOrderClass::getOrderList($offset, $limit);
  512. if ($result['errcode'] === 0) {
  513. return [
  514. ];
  515. } else {
  516. return [
  517. ];
  518. }
  519. } catch (\Exception $e) {
  520. return [
  521. 'success' => false,
  522. 'message' => '系统错误:' . $e->getMessage()
  523. ];
  524. }
  525. }
  526. /**
  527. * 微信回调接口
  528. * POST /intra-city/callback
  529. */
  530. public function actionCallback()
  531. {
  532. try {
  533. $callbackData = Yii::$app->request->post();
  534. // 从配置中获取安全token
  535. // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  536. // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  537. // 记录回调日志
  538. Yii::info('同城配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), ' --- intracity_callback');
  539. $orderId = isset($callbackData['store_order_id']) ? intval($callbackData['store_order_id']) : 0;
  540. $wxStoreId = $callbackData['wx_store_id'];
  541. // 使用 store_order_id 与 wx_store_id 去查询 xhExpressOrder 表
  542. $eo = ExpressOrderClass::getByCondition(['store_order_id'=>$orderId, 'wx_order_id'=>$wxStoreId], true, false);
  543. if (empty($eo)) {
  544. Yii::error('订单不存在.store_order_id: '.$orderId.',wx_store_id: '.$wxStoreId);
  545. return [
  546. 'return_code' => 1,
  547. 'return_msg' => '系统错误'
  548. ];
  549. }
  550. $orderStatus = intval($callbackData['order_status']);
  551. switch ($orderStatus) {
  552. case IntraCityExpress::ORDER_STATUS_CREATED: // 订单创建成功
  553. $eo->order_status = 1;
  554. $eo->status = 1;
  555. break;
  556. case IntraCityExpress::ORDER_STATUS_CANCELED_BY_MERCHANT: // 商家取消订单
  557. $eo->order_status = 2;
  558. break;
  559. case IntraCityExpress::ORDER_STATUS_CANCELED_BY_DELIVERY: // 配送方取消订单
  560. $eo->order_status = 3;
  561. break;
  562. case IntraCityExpress::ORDER_STATUS_ACCEPTED: // 配送员接单
  563. $eo->order_status = 4;
  564. break;
  565. case IntraCityExpress::ORDER_STATUS_ARRIVED: // 配送员到店
  566. $eo->order_status = 5;
  567. break;
  568. case IntraCityExpress::ORDER_STATUS_DELIVERING: // 配送中
  569. $eo->order_status = 6;
  570. break;
  571. case IntraCityExpress::ORDER_STATUS_WITHDRAWN: // 配送员撤单
  572. $eo->order_status = 7;
  573. break;
  574. case IntraCityExpress::ORDER_STATUS_COMPLETED: // 配送完成
  575. $eo->order_status = 8;
  576. $eo->status = 2;
  577. break;
  578. case IntraCityExpress::ORDER_STATUS_EXCEPTION: // 配送异常
  579. $eo->order_status = 9;
  580. break;
  581. }
  582. $re = $eo->save();
  583. if (!$re) {
  584. noticeUtil::push('同城配送回调更新订单状态失败. store_order_id:'.$orderId.',wx_store_id:'.$wxStoreId.',status:'.$eo->status);
  585. Yii::error('更新订单状态失败. store_order_id:'.$orderId.',wx_store_id:'.$wxStoreId.',status:'.$eo->status);
  586. return [
  587. 'return_code' => 0,
  588. 'return_msg' => 'OK'
  589. ];
  590. }
  591. $result = [
  592. 'return_code' => 0,
  593. 'return_msg' => 'OK'
  594. ];
  595. return $result;
  596. } catch (\Exception $e) {
  597. Yii::error('同城配送回调处理异常:' . $e->getMessage(), 'intracity_callback');
  598. return [
  599. 'return_code' => 1,
  600. 'return_msg' => '系统错误'
  601. ];
  602. }
  603. }
  604. /**
  605. * 模拟回调(测试用)
  606. * POST /intra-city/mock-notify
  607. */
  608. public function actionMockNotify()
  609. {
  610. Yii::$app->response->format = Response::FORMAT_JSON;
  611. try {
  612. $request = Yii::$app->request;
  613. $orderStatus = $request->post('order_status');
  614. $wxOrderId = $request->post('wx_order_id');
  615. $outStoreId = $request->post('out_store_id');
  616. $outOrderId = $request->post('store_order_id'); // 修正为 store_order_id
  617. if (empty($orderStatus)) {
  618. util::fail("请提供订单状态");
  619. }
  620. if (empty($wxOrderId) && (empty($outStoreId) || empty($outOrderId))) {
  621. util::fail("参数不足:wx_order_id 或 (out_store_id + store_order_id) 必须提供一组");
  622. }
  623. $result = IntraCityExpress::mockNotify($orderStatus, $wxOrderId, $outStoreId, $outOrderId);
  624. if (isset($result['errcode']) && $result['errcode'] === 0) {
  625. util::success("模拟回调成功", $result);
  626. } else {
  627. Yii::error("模拟回调失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  628. util::fail($result['errmsg'] ?? '模拟回调失败', $result['errcode'] ?? -1);
  629. }
  630. } catch (\Exception $e) {
  631. Yii::error("模拟回调异常:" . $e->getMessage(), 'intracity');
  632. util::fail("系统出错");
  633. }
  634. }
  635. }