IntraCityController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\wx\classes\WxOpenClass;
  4. use common\components\dict;
  5. use common\components\expressUtil;
  6. use common\components\util;
  7. use Yii;
  8. use biz\shop\classes\ShopClass;
  9. use yii\web\Response;
  10. use common\components\IntraCityExpress;
  11. /**
  12. * 同城配送控制器
  13. *
  14. * 提供同城配送相关的API接口
  15. */
  16. class IntraCityController extends BaseController
  17. {
  18. /**
  19. * 禁用CSRF验证(用于接收微信回调)
  20. */
  21. public function beforeAction($action)
  22. {
  23. if ($action->id === 'callback') {
  24. $this->enableCsrfValidation = false;
  25. }
  26. return parent::beforeAction($action);
  27. }
  28. /**
  29. * 创建门店
  30. * POST /intra-city/create-store
  31. */
  32. public function actionCreateStore()
  33. {
  34. Yii::$app->response->format = Response::FORMAT_JSON;
  35. try {
  36. $shopId = intval($this->shopId);
  37. if (empty($shopId)) {
  38. $shopId = $this->shopId;
  39. }
  40. $shop = ShopClass::getById($shopId, true, 'id, merchantName, address, lat, long, telephone');
  41. $storeData = [
  42. 'out_store_id' => $shop->id,
  43. 'store_name' => $shop->merchantName,
  44. 'store_address' => $shop->address,
  45. 'store_longitude' => $shop->long,
  46. 'store_latitude' => $shop->lat,
  47. 'store_phone' => $shop->telephone
  48. ];
  49. // 验证必填参数
  50. $requiredFields = ['out_store_id', 'store_name', 'store_address', 'store_longitude', 'store_latitude', 'store_phone'];
  51. foreach ($requiredFields as $field) {
  52. if (empty($storeData[$field])) {
  53. util::fail("缺少必填参数:{$field}");
  54. }
  55. }
  56. $result = IntraCityExpress::createStore($storeData);
  57. if ($result['errcode'] === 0) {
  58. util::success("门店创建成功", $result);
  59. } else {
  60. Yii::error("门店创建失败:" . $result['errmsg']);
  61. util::fail($result['errmsg']);
  62. }
  63. } catch (\Exception $e) {
  64. Yii::error("门店创建失败:" . $e->getMessage());
  65. util::fail("系统出错");
  66. }
  67. }
  68. /**
  69. * 查询门店创建情况
  70. */
  71. public function actionStore()
  72. {
  73. util::complete();
  74. }
  75. /**
  76. * 创建订单
  77. * POST /intra-city/create-order
  78. */
  79. public function actionCreateOrder()
  80. {
  81. Yii::$app->response->format = Response::FORMAT_JSON;
  82. try {
  83. $request = Yii::$app->request;
  84. $orderData = $request->post();
  85. // 验证必填参数
  86. $requiredFields = [
  87. 'out_store_id', 'store_order_id', 'delivery_service_code',
  88. 'to_user_name', 'to_user_phone', 'to_user_address',
  89. 'to_user_longitude', 'to_user_latitude', 'goods_value',
  90. 'goods_weight'
  91. ];
  92. foreach ($requiredFields as $field) {
  93. if (empty($orderData[$field])) {
  94. util::fail("缺少必填参数:{$field}");
  95. }
  96. }
  97. $result = IntraCityExpress::createOrder($orderData);
  98. if (isset($result['errcode']) && $result['errcode'] === 0) {
  99. util::success('订单创建成功', [
  100. 'wx_order_id' => $result['wx_order_id'] ?? null,
  101. 'order_status' => $result['order_status'] ?? null,
  102. 'fee' => $result['fee'] ?? null,
  103. 'delivery_token' => $result['delivery_token'] ?? null,
  104. ]);
  105. } else {
  106. Yii::error("订单创建失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  107. util::fail($result['errmsg'] ?? '订单创建失败', $result['errcode'] ?? -1);
  108. }
  109. } catch (\Exception $e) {
  110. Yii::error("订单创建异常:" . $e->getMessage(), 'intracity');
  111. util::fail("系统出错");
  112. }
  113. }
  114. /**
  115. * 查询订单
  116. * GET /intra-city/get-order
  117. */
  118. public function actionGetOrder()
  119. {
  120. Yii::$app->response->format = Response::FORMAT_JSON;
  121. try {
  122. $request = Yii::$app->request;
  123. $wxOrderId = $request->get('wx_order_id');
  124. $outOrderId = $request->get('store_order_id'); // 修正为 store_order_id
  125. $outStoreId = $request->get('out_store_id');
  126. if (empty($wxOrderId) && (empty($outOrderId) || empty($outStoreId))) {
  127. util::fail("参数不足:wx_order_id 或 (store_order_id + out_store_id) 必须提供一组");
  128. }
  129. $result = IntraCityExpress::getOrder($wxOrderId, $outOrderId, $outStoreId);
  130. if (isset($result['errcode']) && $result['errcode'] === 0) {
  131. util::success("查询成功", $result);
  132. } else {
  133. Yii::error("订单查询失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  134. util::fail($result['errmsg'] ?? '订单查询失败', $result['errcode'] ?? -1);
  135. }
  136. } catch (\Exception $e) {
  137. Yii::error("订单查询异常:" . $e->getMessage(), 'intracity');
  138. util::fail("系统出错");
  139. }
  140. }
  141. /**
  142. * 取消订单
  143. * POST /intra-city/cancel-order
  144. */
  145. public function actionCancelOrder()
  146. {
  147. Yii::$app->response->format = Response::FORMAT_JSON;
  148. try {
  149. $request = Yii::$app->request;
  150. $wxOrderId = $request->post('wx_order_id');
  151. $outOrderId = $request->post('store_order_id'); // 修正为 store_order_id
  152. $outStoreId = $request->post('out_store_id');
  153. if (empty($wxOrderId) && (empty($outOrderId) || empty($outStoreId))) {
  154. util::fail("参数不足:wx_order_id 或 (store_order_id + out_store_id) 必须提供一组");
  155. }
  156. $result = IntraCityExpress::cancelOrder($wxOrderId, $outOrderId, $outStoreId);
  157. if (isset($result['errcode']) && $result['errcode'] === 0) {
  158. util::success("订单取消成功", $result);
  159. } else {
  160. Yii::error("订单取消失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  161. util::fail($result['errmsg'] ?? '订单取消失败', $result['errcode'] ?? -1);
  162. }
  163. } catch (\Exception $e) {
  164. Yii::error("订单取消异常:" . $e->getMessage(), 'intracity');
  165. util::fail("系统出错");
  166. }
  167. }
  168. /**
  169. * 获取门店列表
  170. * GET /intra-city/store-list
  171. */
  172. public function actionStoreList()
  173. {
  174. Yii::$app->response->format = Response::FORMAT_JSON;
  175. try {
  176. $request = Yii::$app->request;
  177. $offset = $request->get('offset', 0);
  178. $limit = $request->get('limit', 20);
  179. $result = IntraCityExpress::getStoreList($offset, $limit);
  180. if ($result['errcode'] === 0) {
  181. return [
  182. 'success' => true,
  183. 'message' => '查询成功',
  184. 'data' => $result
  185. ];
  186. } else {
  187. return [
  188. 'success' => false,
  189. 'message' => $result['errmsg'],
  190. 'error_code' => $result['errcode']
  191. ];
  192. }
  193. } catch (\Exception $e) {
  194. return [
  195. 'success' => false,
  196. 'message' => '系统错误:' . $e->getMessage()
  197. ];
  198. }
  199. }
  200. /**
  201. * 获取订单列表
  202. * GET /intra-city/order-list
  203. */
  204. public function actionOrderList()
  205. {
  206. Yii::$app->response->format = Response::FORMAT_JSON;
  207. try {
  208. $request = Yii::$app->request;
  209. $offset = $request->get('offset', 0);
  210. $limit = $request->get('limit', 20);
  211. $result = IntraCityExpress::getOrderList($offset, $limit);
  212. if ($result['errcode'] === 0) {
  213. return [
  214. 'success' => true,
  215. 'message' => '查询成功',
  216. 'data' => $result
  217. ];
  218. } else {
  219. return [
  220. 'success' => false,
  221. 'message' => $result['errmsg'],
  222. 'error_code' => $result['errcode']
  223. ];
  224. }
  225. } catch (\Exception $e) {
  226. return [
  227. 'success' => false,
  228. 'message' => '系统错误:' . $e->getMessage()
  229. ];
  230. }
  231. }
  232. /**
  233. * 获取运力列表
  234. * GET /intra-city/delivery-list
  235. */
  236. public function actionDeliveryList()
  237. {
  238. Yii::$app->response->format = Response::FORMAT_JSON;
  239. try {
  240. $result = IntraCityExpress::getDeliveryList();
  241. if ($result['errcode'] === 0) {
  242. return [
  243. 'success' => true,
  244. 'message' => '查询成功',
  245. 'data' => $result
  246. ];
  247. } else {
  248. return [
  249. 'success' => false,
  250. 'message' => $result['errmsg'],
  251. 'error_code' => $result['errcode']
  252. ];
  253. }
  254. } catch (\Exception $e) {
  255. return [
  256. 'success' => false,
  257. 'message' => '系统错误:' . $e->getMessage()
  258. ];
  259. }
  260. }
  261. /**
  262. * 微信回调接口
  263. * POST /intra-city/callback
  264. */
  265. public function actionCallback()
  266. {
  267. Yii::$app->response->format = Response::FORMAT_JSON;
  268. try {
  269. $request = Yii::$app->request;
  270. $callbackData = $request->post();
  271. // 从配置中获取安全token
  272. $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
  273. $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
  274. // 记录回调日志
  275. Yii::info('同城配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'intracity_callback');
  276. return $result;
  277. } catch (\Exception $e) {
  278. Yii::error('同城配送回调处理异常:' . $e->getMessage(), 'intracity_callback');
  279. return [
  280. 'return_code' => 1,
  281. 'return_msg' => '系统错误'
  282. ];
  283. }
  284. }
  285. /**
  286. * 模拟回调(测试用)
  287. * POST /intra-city/mock-notify
  288. */
  289. public function actionMockNotify()
  290. {
  291. Yii::$app->response->format = Response::FORMAT_JSON;
  292. try {
  293. $request = Yii::$app->request;
  294. $orderStatus = $request->post('order_status');
  295. $wxOrderId = $request->post('wx_order_id');
  296. $outStoreId = $request->post('out_store_id');
  297. $outOrderId = $request->post('store_order_id'); // 修正为 store_order_id
  298. if (empty($orderStatus)) {
  299. util::fail("请提供订单状态");
  300. }
  301. if (empty($wxOrderId) && (empty($outStoreId) || empty($outOrderId))) {
  302. util::fail("参数不足:wx_order_id 或 (out_store_id + store_order_id) 必须提供一组");
  303. }
  304. $result = IntraCityExpress::mockNotify($orderStatus, $wxOrderId, $outStoreId, $outOrderId);
  305. if (isset($result['errcode']) && $result['errcode'] === 0) {
  306. util::success("模拟回调成功", $result);
  307. } else {
  308. Yii::error("模拟回调失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
  309. util::fail($result['errmsg'] ?? '模拟回调失败', $result['errcode'] ?? -1);
  310. }
  311. } catch (\Exception $e) {
  312. Yii::error("模拟回调异常:" . $e->getMessage(), 'intracity');
  313. util::fail("系统出错");
  314. }
  315. }
  316. /**
  317. * 获取订单状态常量
  318. * GET /intra-city/order-status-list
  319. */
  320. public function actionOrderStatusList()
  321. {
  322. Yii::$app->response->format = Response::FORMAT_JSON;
  323. $statusList = [
  324. IntraCityExpress::ORDER_STATUS_CREATED => '订单创建成功',
  325. IntraCityExpress::ORDER_STATUS_CANCELED_BY_MERCHANT => '商家取消订单',
  326. IntraCityExpress::ORDER_STATUS_CANCELED_BY_DELIVERY => '配送方取消订单',
  327. IntraCityExpress::ORDER_STATUS_ACCEPTED => '配送员接单',
  328. IntraCityExpress::ORDER_STATUS_ARRIVED => '配送员到店',
  329. IntraCityExpress::ORDER_STATUS_DELIVERING => '配送中',
  330. IntraCityExpress::ORDER_STATUS_WITHDRAWN => '配送员撤单',
  331. IntraCityExpress::ORDER_STATUS_COMPLETED => '配送完成',
  332. IntraCityExpress::ORDER_STATUS_EXCEPTION => '配送异常',
  333. ];
  334. return [
  335. 'success' => true,
  336. 'message' => '查询成功',
  337. 'data' => $statusList
  338. ];
  339. }
  340. /**
  341. * 获取物品类型常量
  342. * GET /intra-city/goods-type-list
  343. */
  344. public function actionGoodsTypeList()
  345. {
  346. Yii::$app->response->format = Response::FORMAT_JSON;
  347. $goodsTypeList = [
  348. IntraCityExpress::GOODS_TYPE_FAST_FOOD => '快餐',
  349. IntraCityExpress::GOODS_TYPE_MEDICINE => '药品',
  350. IntraCityExpress::GOODS_TYPE_GENERAL => '百货',
  351. IntraCityExpress::GOODS_TYPE_FRESH => '生鲜',
  352. IntraCityExpress::GOODS_TYPE_WINE => '酒品',
  353. IntraCityExpress::GOODS_TYPE_DOCUMENT => '文件',
  354. IntraCityExpress::GOODS_TYPE_CAKE => '蛋糕',
  355. IntraCityExpress::GOODS_TYPE_FLOWER => '鲜花',
  356. IntraCityExpress::GOODS_TYPE_DIGITAL => '数码',
  357. IntraCityExpress::GOODS_TYPE_CLOTHING => '服装',
  358. IntraCityExpress::GOODS_TYPE_AUTO_PARTS => '汽配',
  359. IntraCityExpress::GOODS_TYPE_JEWELRY => '珠宝',
  360. IntraCityExpress::GOODS_TYPE_DRINK => '饮料',
  361. IntraCityExpress::GOODS_TYPE_LICENSE => '证照',
  362. IntraCityExpress::GOODS_TYPE_PET => '宠物用品',
  363. IntraCityExpress::GOODS_TYPE_MATERNITY => '母婴用品',
  364. IntraCityExpress::GOODS_TYPE_COSMETICS => '美妆用品',
  365. IntraCityExpress::GOODS_TYPE_HOME => '家居建材',
  366. IntraCityExpress::GOODS_TYPE_OTHER => '其他',
  367. ];
  368. return [
  369. 'success' => true,
  370. 'message' => '查询成功',
  371. 'data' => $goodsTypeList
  372. ];
  373. }
  374. }