IntraCityController.php 15 KB

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