IntraCityController.php 14 KB

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