| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- <?php
- namespace hd\controllers;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\expressUtil;
- use common\components\util;
- use Yii;
- use biz\shop\classes\ShopClass;
- use yii\web\Response;
- use common\components\IntraCityExpress;
- /**
- * 同城配送控制器
- *
- * 提供同城配送相关的API接口
- */
- class IntraCityController extends BaseController
- {
- /**
- * 禁用CSRF验证(用于接收微信回调)
- */
- public function beforeAction($action)
- {
- if ($action->id === 'callback') {
- $this->enableCsrfValidation = false;
- }
- return parent::beforeAction($action);
- }
- /**
- * 创建门店
- * POST /intra-city/create-store
- */
- public function actionCreateStore()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- try {
- $shopId = intval($this->shopId);
- if (empty($shopId)) {
- $shopId = $this->shopId;
- }
- $shop = ShopClass::getById($shopId, true, 'id, merchantName, address, lat, long, telephone');
- $storeData = [
- 'out_store_id' => $shop->id,
- 'store_name' => $shop->merchantName,
- 'store_address' => $shop->address,
- 'store_longitude' => $shop->long,
- 'store_latitude' => $shop->lat,
- 'store_phone' => $shop->telephone
- ];
- // 验证必填参数
- $requiredFields = ['out_store_id', 'store_name', 'store_address', 'store_longitude', 'store_latitude', 'store_phone'];
- foreach ($requiredFields as $field) {
- if (empty($storeData[$field])) {
- util::fail("缺少必填参数:{$field}");
- }
- }
- $result = IntraCityExpress::createStore($storeData);
- if ($result['errcode'] === 0) {
- util::success("门店创建成功", $result);
- } else {
- Yii::error("门店创建失败:" . $result['errmsg']);
- util::fail($result['errmsg']);
- }
- } catch (\Exception $e) {
- Yii::error("门店创建失败:" . $e->getMessage());
- util::fail("系统出错");
- }
- }
- /**
- * 查询门店创建情况
- */
- public function actionStore()
- {
- util::complete();
- }
- /**
- * 创建订单
- * POST /intra-city/create-order
- */
- public function actionCreateOrder()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- try {
- $request = Yii::$app->request;
- $orderData = $request->post();
- // 验证必填参数
- $requiredFields = [
- 'out_store_id', 'store_order_id', 'delivery_service_code',
- 'to_user_name', 'to_user_phone', 'to_user_address',
- 'to_user_longitude', 'to_user_latitude', 'goods_value',
- 'goods_weight'
- ];
- foreach ($requiredFields as $field) {
- if (empty($orderData[$field])) {
- util::fail("缺少必填参数:{$field}");
- }
- }
- $result = IntraCityExpress::createOrder($orderData);
- if (isset($result['errcode']) && $result['errcode'] === 0) {
- util::success('订单创建成功', [
- 'wx_order_id' => $result['wx_order_id'] ?? null,
- 'order_status' => $result['order_status'] ?? null,
- 'fee' => $result['fee'] ?? null,
- 'delivery_token' => $result['delivery_token'] ?? null,
- ]);
- } else {
- Yii::error("订单创建失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
- util::fail($result['errmsg'] ?? '订单创建失败', $result['errcode'] ?? -1);
- }
- } catch (\Exception $e) {
- Yii::error("订单创建异常:" . $e->getMessage(), 'intracity');
- util::fail("系统出错");
- }
- }
- /**
- * 查询订单
- * GET /intra-city/get-order
- */
- public function actionGetOrder()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- try {
- $request = Yii::$app->request;
- $wxOrderId = $request->get('wx_order_id');
- $outOrderId = $request->get('store_order_id'); // 修正为 store_order_id
- $outStoreId = $request->get('out_store_id');
- if (empty($wxOrderId) && (empty($outOrderId) || empty($outStoreId))) {
- util::fail("参数不足:wx_order_id 或 (store_order_id + out_store_id) 必须提供一组");
- }
- $result = IntraCityExpress::getOrder($wxOrderId, $outOrderId, $outStoreId);
- if (isset($result['errcode']) && $result['errcode'] === 0) {
- util::success("查询成功", $result);
- } else {
- Yii::error("订单查询失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
- util::fail($result['errmsg'] ?? '订单查询失败', $result['errcode'] ?? -1);
- }
- } catch (\Exception $e) {
- Yii::error("订单查询异常:" . $e->getMessage(), 'intracity');
- util::fail("系统出错");
- }
- }
- /**
- * 取消订单
- * POST /intra-city/cancel-order
- */
- public function actionCancelOrder()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- try {
- $request = Yii::$app->request;
- $wxOrderId = $request->post('wx_order_id');
- $outOrderId = $request->post('store_order_id'); // 修正为 store_order_id
- $outStoreId = $request->post('out_store_id');
- if (empty($wxOrderId) && (empty($outOrderId) || empty($outStoreId))) {
- util::fail("参数不足:wx_order_id 或 (store_order_id + out_store_id) 必须提供一组");
- }
- $result = IntraCityExpress::cancelOrder($wxOrderId, $outOrderId, $outStoreId);
- if (isset($result['errcode']) && $result['errcode'] === 0) {
- util::success("订单取消成功", $result);
- } else {
- Yii::error("订单取消失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
- util::fail($result['errmsg'] ?? '订单取消失败', $result['errcode'] ?? -1);
- }
- } catch (\Exception $e) {
- Yii::error("订单取消异常:" . $e->getMessage(), 'intracity');
- util::fail("系统出错");
- }
- }
- /**
- * 获取门店列表
- * GET /intra-city/store-list
- */
- public function actionStoreList()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- try {
- $request = Yii::$app->request;
- $offset = $request->get('offset', 0);
- $limit = $request->get('limit', 20);
- $result = IntraCityExpress::getStoreList($offset, $limit);
- if ($result['errcode'] === 0) {
- return [
- 'success' => true,
- 'message' => '查询成功',
- 'data' => $result
- ];
- } else {
- return [
- 'success' => false,
- 'message' => $result['errmsg'],
- 'error_code' => $result['errcode']
- ];
- }
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '系统错误:' . $e->getMessage()
- ];
- }
- }
- /**
- * 获取订单列表
- * GET /intra-city/order-list
- */
- public function actionOrderList()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- try {
- $request = Yii::$app->request;
- $offset = $request->get('offset', 0);
- $limit = $request->get('limit', 20);
- $result = IntraCityExpress::getOrderList($offset, $limit);
- if ($result['errcode'] === 0) {
- return [
- 'success' => true,
- 'message' => '查询成功',
- 'data' => $result
- ];
- } else {
- return [
- 'success' => false,
- 'message' => $result['errmsg'],
- 'error_code' => $result['errcode']
- ];
- }
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '系统错误:' . $e->getMessage()
- ];
- }
- }
- /**
- * 获取运力列表
- * GET /intra-city/delivery-list
- */
- public function actionDeliveryList()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- try {
- $result = IntraCityExpress::getDeliveryList();
- if ($result['errcode'] === 0) {
- return [
- 'success' => true,
- 'message' => '查询成功',
- 'data' => $result
- ];
- } else {
- return [
- 'success' => false,
- 'message' => $result['errmsg'],
- 'error_code' => $result['errcode']
- ];
- }
- } catch (\Exception $e) {
- return [
- 'success' => false,
- 'message' => '系统错误:' . $e->getMessage()
- ];
- }
- }
- /**
- * 微信回调接口
- * POST /intra-city/callback
- */
- public function actionCallback()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- try {
- $request = Yii::$app->request;
- $callbackData = $request->post();
- // 从配置中获取安全token
- $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
- $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
- // 记录回调日志
- Yii::info('同城配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE), 'intracity_callback');
- return $result;
- } catch (\Exception $e) {
- Yii::error('同城配送回调处理异常:' . $e->getMessage(), 'intracity_callback');
- return [
- 'return_code' => 1,
- 'return_msg' => '系统错误'
- ];
- }
- }
- /**
- * 模拟回调(测试用)
- * POST /intra-city/mock-notify
- */
- public function actionMockNotify()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- try {
- $request = Yii::$app->request;
- $orderStatus = $request->post('order_status');
- $wxOrderId = $request->post('wx_order_id');
- $outStoreId = $request->post('out_store_id');
- $outOrderId = $request->post('store_order_id'); // 修正为 store_order_id
- if (empty($orderStatus)) {
- util::fail("请提供订单状态");
- }
- if (empty($wxOrderId) && (empty($outStoreId) || empty($outOrderId))) {
- util::fail("参数不足:wx_order_id 或 (out_store_id + store_order_id) 必须提供一组");
- }
- $result = IntraCityExpress::mockNotify($orderStatus, $wxOrderId, $outStoreId, $outOrderId);
- if (isset($result['errcode']) && $result['errcode'] === 0) {
- util::success("模拟回调成功", $result);
- } else {
- Yii::error("模拟回调失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
- util::fail($result['errmsg'] ?? '模拟回调失败', $result['errcode'] ?? -1);
- }
- } catch (\Exception $e) {
- Yii::error("模拟回调异常:" . $e->getMessage(), 'intracity');
- util::fail("系统出错");
- }
- }
- /**
- * 获取订单状态常量
- * GET /intra-city/order-status-list
- */
- public function actionOrderStatusList()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- $statusList = [
- IntraCityExpress::ORDER_STATUS_CREATED => '订单创建成功',
- IntraCityExpress::ORDER_STATUS_CANCELED_BY_MERCHANT => '商家取消订单',
- IntraCityExpress::ORDER_STATUS_CANCELED_BY_DELIVERY => '配送方取消订单',
- IntraCityExpress::ORDER_STATUS_ACCEPTED => '配送员接单',
- IntraCityExpress::ORDER_STATUS_ARRIVED => '配送员到店',
- IntraCityExpress::ORDER_STATUS_DELIVERING => '配送中',
- IntraCityExpress::ORDER_STATUS_WITHDRAWN => '配送员撤单',
- IntraCityExpress::ORDER_STATUS_COMPLETED => '配送完成',
- IntraCityExpress::ORDER_STATUS_EXCEPTION => '配送异常',
- ];
- return [
- 'success' => true,
- 'message' => '查询成功',
- 'data' => $statusList
- ];
- }
- /**
- * 获取物品类型常量
- * GET /intra-city/goods-type-list
- */
- public function actionGoodsTypeList()
- {
- Yii::$app->response->format = Response::FORMAT_JSON;
- $goodsTypeList = [
- IntraCityExpress::GOODS_TYPE_FAST_FOOD => '快餐',
- IntraCityExpress::GOODS_TYPE_MEDICINE => '药品',
- IntraCityExpress::GOODS_TYPE_GENERAL => '百货',
- IntraCityExpress::GOODS_TYPE_FRESH => '生鲜',
- IntraCityExpress::GOODS_TYPE_WINE => '酒品',
- IntraCityExpress::GOODS_TYPE_DOCUMENT => '文件',
- IntraCityExpress::GOODS_TYPE_CAKE => '蛋糕',
- IntraCityExpress::GOODS_TYPE_FLOWER => '鲜花',
- IntraCityExpress::GOODS_TYPE_DIGITAL => '数码',
- IntraCityExpress::GOODS_TYPE_CLOTHING => '服装',
- IntraCityExpress::GOODS_TYPE_AUTO_PARTS => '汽配',
- IntraCityExpress::GOODS_TYPE_JEWELRY => '珠宝',
- IntraCityExpress::GOODS_TYPE_DRINK => '饮料',
- IntraCityExpress::GOODS_TYPE_LICENSE => '证照',
- IntraCityExpress::GOODS_TYPE_PET => '宠物用品',
- IntraCityExpress::GOODS_TYPE_MATERNITY => '母婴用品',
- IntraCityExpress::GOODS_TYPE_COSMETICS => '美妆用品',
- IntraCityExpress::GOODS_TYPE_HOME => '家居建材',
- IntraCityExpress::GOODS_TYPE_OTHER => '其他',
- ];
- return [
- 'success' => true,
- 'message' => '查询成功',
- 'data' => $goodsTypeList
- ];
- }
- }
|