IntraCityController.php 30 KB

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