| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798 |
- <?php
- namespace hd\controllers;
- use bizHd\express\classes\ExpressOrderClass;
- use bizHd\order\classes\OrderClass;
- use bizHd\order\classes\OrderGoodsClass;
- use bizHd\order\classes\OrderItemClass;
- use bizHd\shop\classes\MainClass;
- use bizHd\user\classes\UserClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use hd\models\IntraCity\StoreFeeForm;
- use Yii;
- use biz\shop\classes\ShopClass;
- use common\components\IntraCityExpress;
- use yii\db\Query;
- use biz\wx\classes\WxMessageClass;
- /**
- * 同城配送控制器
- *
- * 提供同城配送相关的API接口
- */
- class IntraCityController extends BaseController
- {
- public $guestAccess = ['callback'];
- public function actionApply()
- {
- IntraCityExpress::applyStore();
- }
- /**
- * 创建门店
- * POST /intra-city/create-store
- */
- public function actionCreateStore()
- {
- $shop = $this->shop;
- $shopName = $shop->shopName ?? '';
- $sjName = $shop->merchantName ?? '';
- $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 3);
- if (empty($shop->lat) || empty($shop->long)) {
- util::fail('没有经纬度,请修改地址');
- }
- if (empty($shop->address)) {
- util::fail('地址缺失,请修改');
- }
- //区是必传,如果没有区,先用市来代替,比如:淘花里中山店,他就是没有区
- $dist = !empty($shop->dist) ? $shop->dist : '市区';
- $telephone = !empty($shop->telephone) ? $shop->telephone : $shop->mobile;
- $storeData = [
- 'out_store_id' => $this->mainId, //自定义门店编号 -- 从 shopId 改为 mainId
- 'store_name' => $name,
- 'address_info' => [
- 'province' => $shop->province,
- 'city' => $shop->city,
- 'area' => $dist,
- 'street' => '',
- 'house' => $shop->address . $shop->floor,
- 'lat' => $shop->lat,
- 'lng' => $shop->long,
- 'phone' => $telephone
- ]
- ];
- // 验证必填参数
- $requiredFields = ['out_store_id', 'store_name', 'address_info'];
- foreach ($requiredFields as $field) {
- if (empty($storeData[$field])) {
- util::fail("缺少必填参数:{$field}");
- }
- }
- // 创建门店前,检查是否已经 开通门店权限 (有wxStoreId,即说明开通了)
- $main = $this->main;
- // 查询是否存在 wx_store_id,不存在则执行apply
- if (!empty($main->wxStoreId)) {
- util::fail('已经创建门店,请刷新');
- }
- $result = IntraCityExpress::createStore($storeData);
- if ($result['errcode'] === 0) {
- // 保存微信门店编号(wx_store_id)
- $main->wxStoreId = $result['wx_store_id'];
- $main->save();
- util::success($result, "门店创建成功");
- } else {
- Yii::error("门店创建失败:" . $result['errmsg']);
- $errMsg = $result['errmsg'] ?? '';
- noticeUtil::push("跑腿创建门店报错:" . $errMsg, '15280215347');
- util::fail('创建失败,请联系客服');
- }
- }
- /**
- * 查询门店创建情况
- */
- public function actionStore()
- {
- $shopId = $this->shopId;
- // 首先从门店扩展表查询是否已经创建
- //$shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true, false, 'id, wxStoreId');
- $shopExt = MainClass::getById($this->mainId, true, 'id, wxStoreId');
- if ($shopExt->wxStoreId == '') { // 不存在,则从微信接口查询
- $result = IntraCityExpress::getStore($this->mainId);
- if ($result['errcode'] === 0) {
- $store = $result['store_list'][0];
- // 保存 wx_store_id
- $shopExt->wxStoreId = $store['wx_store_id'];
- $shopExt->save();
- util::success($store, "门店查询成功");
- } else {
- Yii::error("门店查询失败:" . $result['errmsg']);
- util::fail('门店查询失败');
- }
- } else {
- $field = 'province, city, dist, lat, long, floor, address';
- $shop = ShopClass::getById($shopId, false, $field);
- $data = array_merge($shop, $shopExt->toArray());
- util::success($data, "门店查询成功");
- }
- }
- /**
- * 更新门店
- */
- public function actionUpdateStore()
- {
- // 首先从门店扩展表查询是否已经创建
- $main = $this->main;
- $wxStoreId = $main->wxStoreId;
- if (empty($wxStoreId)) {
- util::fail('未创建门店,请先创建');
- }
- $shop = $this->shop;
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 3);
- if (empty($shop->lat) || empty($shop->long)) {
- util::fail('没有经纬度,请修改地址');
- }
- if (empty($shop->address)) {
- util::fail('地址缺失,请修改');
- }
- $shopName = $shop->shopName ?? '';
- $sjName = $shop->merchantName ?? '';
- $name = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
- //区是必传,如果没有区,先用市来代替,比如:淘花里中山店,他就是没有区
- $dist = !empty($shop->dist) ? $shop->dist : '市区';
- $telephone = !empty($shop->telephone) ? $shop->telephone : $shop->mobile;
- $storeData = [
- 'keys' => ['wx_store_id' => $wxStoreId],
- 'content' => [
- 'store_name' => $name,
- 'address_info' => [
- 'province' => $shop->province,
- 'city' => $shop->city,
- 'area' => $dist,
- 'street' => '',
- 'house' => $shop->address . $shop->floor,
- 'lat' => $shop->lat,
- 'lng' => $shop->long,
- 'phone' => $telephone
- ],
- //"order_pattern" => 2,
- //"service_trans_prefer" => "SFTC" //order_pattern = 2时必填
- ]
- ];
- $result = IntraCityExpress::updateStore($storeData);
- if ($result['errcode'] === 0) {
- util::success($result, "门店更新成功");
- } else {
- $errMsg = $result['errmsg'] ?? '';
- Yii::error("门店更新失败:" . $errMsg);
- noticeUtil::push("花掌柜,跑腿修改门店报错:" . $errMsg, '15280215347');
- util::fail('更新失败,请联系客服');
- }
- }
- /**
- * 门店运费充值
- */
- public function actionStoreCharge()
- {
- $post = Yii::$app->request->post();
- $amount = floatval($post['amount']);
- $amountTurnFen = $amount * 100;
- if ($amountTurnFen < 5000) {
- util::fail('50元起充');
- }
- $expressId = $post['expressId'];
- $serviceTransIds = ['SFTC', 'DADA'];
- if (!in_array($expressId, $serviceTransIds)) {
- util::fail('快递动力ID出错');
- }
- //$shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], false, false, 'id, wxStoreId');
- $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
- if ($shopExt['wxStoreId'] == '') {
- util::fail('微信门店编号为空');
- }
- $wxStoreId = $shopExt['wxStoreId'];
- $result = IntraCityExpress::storeCharge($wxStoreId, $expressId, $amountTurnFen);
- if ($result['errcode'] === 0) {
- util::success($result, "门店余额充值单已生成");
- } else {
- Yii::error("门店余额充值失败:" . $result['errmsg']);
- util::fail('门店余额充值失败');
- }
- }
- /**
- * 门店余额查询
- */
- public function actionStoreBalance()
- {
- //$shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], false, false, 'id, wxStoreId');
- $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
- if ($shopExt['wxStoreId'] == '') {
- util::fail('微信门店编号为空');
- }
- $wxStoreId = $shopExt['wxStoreId'];
- $result = IntraCityExpress::getBalance($wxStoreId);
- if ($result['errcode'] === 0) {
- $data = ['all_balance' => $result['all_balance'] / 100.0];
- foreach ($result['balance_detail'] as $item) {
- if ($item['service_trans_id'] == 'DADA') {
- $data['dada_balance'] = $item['balance'] / 100.0;
- }
- if ($item['service_trans_id'] == 'SFTC') {
- $data['sf_balance'] = $item['balance'] / 100.0;
- }
- }
- util::success($data, "门店余额查询成功");
- } else {
- Yii::error("门店余额查询失败:" . $result['errmsg']);
- util::fail('门店余额查询失败');
- }
- }
- /**
- * 查询运费
- */
- public function actionStoreFee()
- {
- $post = Yii::$app->request->post();
- // 创建表单验证模型
- $form = new StoreFeeForm();
- $form->setScenario('store_fee');
- $form->load($post, ''); // 直接从 post 数据加载,不使用模型名作为前缀
- // 执行表单验证
- $form->validateForm();
- $orderId = $post['orderId'];
- $order = OrderClass::getById($orderId);
- if (empty($order)) {
- util::fail('订单出错');
- }
- if ($order['mainId'] != $this->mainId) {
- util::fail('不是你的订单哈。');
- }
- $shopId = intval($this->shopId);
- if (empty($shopId)) {
- util::fail("门店不存在");
- }
- $weight = $post['weight'] ?? 0;
- $price = $post['price'] ?? 0;
- $packageNum = $post['packageNum'] ?? 0;
- $main = $this->main;
- $wxStoreId = $main->wxStoreId ?? '';
- if (empty($wxStoreId)) {
- util::fail('你还没有开通跑腿');
- }
- $cargoName = '花材'; // 商品名称,默认是花材
- $cargo = [
- 'cargo_name' => $cargoName, // 商品名称 -- $order 中没有,要在 goodsInfo 中取
- 'cargo_weight' => intval($weight * 1000), // 单位:克 -- 把千克转换为克
- 'cargo_price' => intval($price * 100), // 单位:分 -- 把元转换为分
- 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
- 'cargo_num' => intval($packageNum)
- ];
- $originalLng = doubleval($post['user_lng']);
- $originalLat = doubleval($post['user_lat']);
- // $useSandBox = getenv('YII_ENV') == 'production' ? 0 : 1;// 根据环境变量判断是否使用沙盒环境
- $orderData = [
- //'out_store_id' => $shopId,
- 'wx_store_id' => $wxStoreId,
- 'user_name' => $post['user_name'],
- 'user_phone' => $post['user_phone'],
- 'user_lng' => $originalLng,
- 'user_lat' => $originalLat,
- 'user_address' => $post['user_address'],
- 'cargo' => $cargo,
- // 'use_sandbox' => $useSandBox,//正式环境不能传这个参数,会报错
- ];
- //多处有用到这个方法,需修改请同步修改,搜索关键词 intra_city_express_pre_add
- $result = IntraCityExpress::preAddOrder($orderData);
- if ($result['errcode'] === 0) {
- util::success($result, "查询成功");
- } else {
- $errMsg = $result['errmsg'] ?? '';
- $errCode = $result['errcode'] ?? '';
- noticeUtil::push("运费查询失败了哦:" . $errMsg . ",编号:" . $errCode, '15280215347');
- util::fail('查询失败,编号673');
- }
- }
- /**
- * 创建订单
- * POST /intra-city/create-order
- */
- public function actionCreateOrder()
- {
- $post = Yii::$app->request->post();
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 4);
- $orderId = intval($post['orderId']);
- $field = 'mainId, status, sendStatus, repeat, forward, orderSn, userId, actPrice, sendNum, salt';
- $order = OrderClass::getById($orderId, false, $field);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if ($order['mainId'] != $this->mainId) {
- util::fail('不是你的订单');
- }
- // 不能创建配送的几种订单状态
- if ($order['status'] != 2) {
- util::fail('待发货订单才能发跑腿,请刷新页面');
- }
- //如果不是没鹛跑腿和取消了,则不能再叫跑腿,有多处,关键词no_call_express
- $notCanCreateSendStatus = [-2, 3];
- if (!in_array($order['sendStatus'], $notCanCreateSendStatus)) {
- util::fail('不能叫跑腿,请刷新页面,编号966');
- }
- if ($order['repeat'] == 1) {
- util::fail('收款码订单不支持跑腿');
- }
- if ($order['forward'] == 1) {
- util::fail('售后付款单不支持跑腿');
- }
- $packageNum = $post['packageNum'] ?? 0;
- if ($packageNum <= 0) {
- util::fail('请填写包裹数量');
- }
- $sn = $order['orderSn'];
- $salt = $order['salt'] ?? "";
- $orderType = $order['orderType'] ?? 1;
- $goodsNum = $order['goodsNum'] ?? 0;
- $goodsNum = $goodsNum > 0 ? $goodsNum : 1;
- $itemList = [];
- $itemInfo = OrderGoodsClass::getByCondition(['orderSn' => $sn], true);
- if (!empty($itemInfo)) {
- $cover = $itemInfo->cover ?? '';
- $itemList = [['item_name' => '花束', 'item_pic_url' => imgUtil::groupImg($cover), 'count' => $goodsNum]];
- }
- if (empty($itemList)) {
- $itemInfo = OrderItemClass::getByCondition(['orderSn' => $sn], true);
- if (!empty($itemInfo)) {
- $cover = $itemInfo->cover ?? '';
- $itemList = [['item_name' => '花材', 'item_pic_url' => imgUtil::groupImg($cover), 'count' => $goodsNum]];
- }
- }
- if (empty($itemList)) {
- $itemList = [['item_name' => '花材', 'item_pic_url' => imgUtil::groupImg(''), 'count' => $goodsNum]];
- }
- $cargoName = $orderType == 1 ? '花束' : '花材';
- $main = $this->main;
- $wxStoreId = $main->wxStoreId ?? '';
- if (empty($wxStoreId)) {
- util::fail('你还没有开通跑腿');
- }
- // 通过 userId 获取 openid(小程序OpenId)
- $user = UserClass::getById($order['userId'], false, 'id, miniOpenId');
- if (empty($user)) {
- util::fail('用户信息缺失');
- }
- $miniOpenId = $user['miniOpenId'] ?? '';
- if (empty($miniOpenId)) {
- $admin = $this->admin;
- $miniOpenId = $admin->miniOpenId ?? '';
- }
- if (empty($miniOpenId)) {
- util::fail('没有找到用户的微信数据');
- }
- $weight = $post['weight'] ?? 0;
- if ($weight <= 0) {
- util::fail('请填写重量');
- }
- $cargo = [
- 'cargo_name' => $cargoName,
- 'cargo_weight' => intval($weight * 1000),
- 'cargo_type' => IntraCityExpress::GOODS_TYPE_FLOWER,
- 'cargo_num' => intval($packageNum),
- 'cargo_price' => intval($order['actPrice'] * 100), // $order 中有各个价格,请注意
- 'item_list' => $itemList,
- ];
- $shopId = $this->shopId;
- $mainId = $this->mainId;
- $snData = ['shopId' => $shopId, 'mainId' => $mainId];
- $storeOrderId = orderSn::getExpressSn($snData);
- $userAddress = $post['user_address'] ?? '';
- if (empty($userAddress)) {
- util::fail('地址不能为空');
- }
- $callbackUrl = Yii::$app->params['hdHost'] . '/intra-city/callback';
- $orderData = [
- 'wx_store_id' => $wxStoreId,
- 'store_order_id' => $storeOrderId, //同一个门店订单编号要保证唯一,相同的订单号会重入
- 'user_openid' => $miniOpenId,
- 'user_lng' => $post['user_lng'],
- 'user_lat' => $post['user_lat'],
- 'user_address' => $userAddress,
- 'user_name' => $post['user_name'],
- 'user_phone' => $post['user_phone'],
- 'order_seq' => $order['sendNum'], // 用于配送员快速寻找到匹配的商品(非必传) -- 对应 xhOrder 表的 sendNum
- 'verify_code_type' => 0,
- 'order_detail_path' => '/admin/order/info?id=' . $orderId . '&salt=' . $salt,
- 'callback_url' => $callbackUrl, // 订单状态回调地址(非必传)
- // 'use_sandbox' => 0,//正式环境不能传这个参数,会报错
- 'cargo' => $cargo
- ];
- $result = IntraCityExpress::createOrder($orderData);
- if (isset($result['errcode']) && $result['errcode'] === 0) {
- $deliveryId = $result['service_trans_id'] ?? '';
- $sendDistance = $result['distance'] ?? 0;
- //订单状态:配送中。配送平台状态:已收到申请,待确认。
- OrderClass::updateById($orderId, ['sendStatus' => -1, 'deliveryId' => $deliveryId, 'sendDistance' => $sendDistance, 'sendType' => 2, 'status' => 3]);
- // 保存进 xhExpressOrder 表
- $fee = $result['fee'] ?? 0;
- $orderData = [
- 'mainId' => $this->mainId,
- 'shopId' => $this->shopId,
- 'deliveryId' => $deliveryId,
- 'wxOrderId' => $result['wx_order_id'] ?? '',
- 'wxStoreId' => $result['wx_store_id'] ?? '',
- 'storeOrderId' => $result['store_order_id'] ?? '',
- 'orderId' => $orderId,
- 'transOrderId' => $result['trans_order_id'] ?? '',
- 'distance' => $result['distance'] ?? 0,
- 'fee' => $fee,
- 'fetchCode' => $result['fetch_code'] ?? '',
- 'orderSeq' => $result['order_seq'] ?? '',
- 'status' => 1,//有订单可以取消
- 'orderStatus' => 0,//已提交申请,待确认
- ];
- ExpressOrderClass::add($orderData);
- $hdName = $order['hdName'] ?? '';
- $customName = $order['customName'] ?? '';
- noticeUtil::push("零售订单,发跑跑腿,获取运费:{$fee} 重量:{$weight} 距离:{$sendDistance} 订单号:{$orderId} 花店:{$hdName} 客户:{$customName} 收货地址:{$userAddress}", '15280215347');
- util::success(['fee' => $result['fee']], '订单创建成功');
- } else {
- noticeUtil::push("门店 -- " . $this->mainId . ",订单创建失败:" . json_encode($result));
- Yii::error("订单创建失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
- $errorMsg = $result['errmsg'] ?? '';
- noticeUtil::push("跑腿单创建失败:" . $errorMsg, '15280215347');
- util::fail('提交失败,请确认余额是否充足');
- }
- }
- /**
- * 查询订单
- * GET /intra-city/get-order
- */
- public function actionGetOrder()
- {
- try {
- //$shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], false, false, 'id, wxStoreId');
- $shopExt = MainClass::getById($this->mainId, false, 'id, wxStoreId');
- if (empty($shopExt)) {
- util::fail("微信门店不存在");
- }
- $post = Yii::$app->request->post();
- $wxOrderId = isset($post['wx_order_id']) ? $post['wx_order_id'] : '';
- $orderId = $post['orderId'];
- //$wxStoreId = isset($post['wx_store_id']) ? $post['wx_order_id'] : $shopExt['wxStoreId'];
- $wxStoreId = isset($post['wx_store_id']) ? $post['wx_store_id'] : $shopExt['wxStoreId'];
- if (empty($wxOrderId) && (empty($orderId) || empty($wxStoreId))) {
- util::fail("参数不足:wx_order_id 或 (order_id + wx_store_id) 必须提供一组");
- }
- $order = OrderClass::getById($orderId, false, 'id, mainId, sendStatus');
- if ($order['mainId'] != $this->mainId) {
- util::fail('不是你的订单!');
- }
- // 检查是否存在重复的订单:如果存在,则使用最后一个订单的 store_order_id
- $storeOrderId = $orderId; // ------------------------------------- 注意区分:$storeOrderId , $orderId 。它们有可能不相同
- $query = (new Query())
- ->from('xhExpressOrder')
- ->where(['orderId' => $orderId])
- ->andWhere(['status' => 1])
- ->orderBy('addTime DESC');
- $esDatas = $query->all();
- $count = count($esDatas);
- if ($count > 0) {
- $es = $esDatas[0];
- $storeOrderId = $es['storeOrderId'];
- if ($count > 1) {
- noticeUtil::push('同城配送订单重复多于1个. store_order_id:' . $orderId . ',wx_store_id:' . $wxStoreId);
- }
- }
- $result = IntraCityExpress::queryOrder($wxStoreId, $storeOrderId, $wxOrderId);
- if (isset($result['errcode']) && $result['errcode'] === 0) {
- util::success($result, "查询成功");
- } else {
- Yii::error("订单查询失败:" . ($result['errmsg'] ?? '未知错误'), 'intracity');
- util::fail('订单查询失败: ' . $result['errcode']);
- }
- } catch (\Exception $e) {
- Yii::error("订单查询异常:" . $e->getMessage(), 'intracity');
- util::fail("系统出错");
- }
- }
- /**
- * 取消订单
- * POST /intra-city/cancel-order
- */
- public function actionCancelOrder()
- {
- $post = Yii::$app->request->post();
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 4);
- $orderId = $post['orderId'];
- $order = OrderClass::getById($orderId, true, 'id, mainId, sendStatus,status');
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if ($order->mainId != $this->mainId) {
- util::fail('不是你的订单');
- }
- $main = $this->main;
- $wxStoreId = $main->wxStoreId ?? '';
- if ($order->status != 3) {
- util::fail('不能取消');
- }
- if (in_array($order->sendStatus, [-2, 2, 3])) {
- //订单 没叫跑腿、已送达、已取消
- util::fail('不能取消哈,编号896');
- }
- $express = ExpressOrderClass::getByCondition(['orderId' => $orderId, 'status' => 1], true);
- if (empty($express)) {
- util::fail('没有找到有效配送单');
- }
- $wxOrderId = $express->wxOrderId ?? '';
- $storeOrderId = $express->storeOrderId ?? '';
- $result = IntraCityExpress::cancelOrder($wxOrderId, $storeOrderId, $wxStoreId);
- if (isset($result['errcode']) && $result['errcode'] === 0) {
- // 更新订单状态
- $order->sendStatus = 3;
- $order->status = 2;
- $order->save();
- $deductFee = $result['deductfee'] ?? 0;
- $express->status = 0;
- $express->cancelTime = date('Y-m-d H:i:s');
- $express->deductfee = $deductFee;
- $express->save();
- util::success($result, "取消成功");
- } else {
- util::fail('取消失败');
- }
- }
- /**
- * 获取订单列表
- * GET /intra-city/order-list
- */
- public function actionOrderList()
- {
- }
- /**
- * 微信回调接口
- * POST /intra-city/callback
- */
- public function actionCallback()
- {
- $callbackData = Yii::$app->request->post();
- if (empty($callbackData)) {
- $postStr = file_get_contents('php://input');
- // 处理转义字符,将JSON字符串正确解析为数组
- $postStr = stripslashes($postStr);
- $callbackData = json_decode($postStr, true);
- if (empty($callbackData)) {
- util::fail('回调请求的数据为空');
- }
- }
- // 从配置中获取安全token
- // $token = Yii::$app->params['wx_intracity_token'] ?? 'your_token_here';
- // $result = IntraCityExpress::handleOrderCallback($callbackData, $token);
- // 记录回调日志
- Yii::info('同城配送回调:' . json_encode($callbackData, JSON_UNESCAPED_UNICODE));
- $storeOrderId = $callbackData['store_order_id'] ?? 0;
- $wxStoreId = $callbackData['wx_store_id'] ?? ''; //微信门店编号
- if (empty($storeOrderId) || empty($wxStoreId)) {
- return $this->asJson(['return_code' => 1, 'return_msg' => '请求参数不正确']);
- }
- try {
- // 使用 storeOrderId 与 wxStoreId 去查询 xhExpressOrder 表
- $eo = ExpressOrderClass::getByCondition(['storeOrderId' => $storeOrderId, 'wxStoreId' => $wxStoreId], true, false);
- if (empty($eo)) {
- Yii::error('订单不存在.store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId);
- return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误']);
- }
- $orderId = $eo->orderId ?? 0;
- if (empty($orderId)) {
- $errRemind = '订单号为空,store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId;
- Yii::error($errRemind, 'intracity');
- noticeUtil::push($errRemind, '15280215347');
- return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误2']);
- }
- $order = OrderClass::getById($orderId, true, 'id,mainId,status,sendStatus');
- if (empty($order)) {
- $errRemind = '没有找到订单,store_order_id: ' . $storeOrderId . ',wx_store_id: ' . $wxStoreId;
- Yii::error($errRemind, 'intracity');
- noticeUtil::push($errRemind, '15280215347');
- return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误3']);
- }
- $orderStatus = intval($callbackData['order_status']);
- //同个配送单,同个状态的,5秒内不允许重复请求,重要勿删
- $uniqueId = $storeOrderId . '_' . $orderStatus;
- util::checkRepeatCommit($uniqueId, 5);
- $tx = Yii::$app->db->beginTransaction(); // ====================== 开启事务
- $msg = ''; // 发通知的内容(默认为空,根据情况生成。没有内容则不会发通知)
- switch ($orderStatus) {
- // 订单创建成功,已叫跑腿,等跑腿接单
- case IntraCityExpress::ORDER_STATUS_CREATED:
- if ($order->status == 3) {
- $eo->orderStatus = 1;
- $eo->save();
- $order->sendStatus = 0;
- $order->save();
- }
- break;
- // 配送员接单
- case IntraCityExpress::ORDER_STATUS_ACCEPTED:
- if ($order->status == 3) {
- $eo->orderStatus = 4;
- $eo->save();
- $order->sendStatus = 1;
- $order->save();
- }
- break;
- // 配送员到店
- case IntraCityExpress::ORDER_STATUS_ARRIVED:
- if ($order->status == 3) {
- $eo->orderStatus = 5;
- $eo->save();
- $order->sendStatus = 4;
- $order->save();
- }
- break;
- // 配送中
- case IntraCityExpress::ORDER_STATUS_DELIVERING:
- if ($order->status == 3) {
- $eo->orderStatus = 6;
- $eo->save();
- $order->sendStatus = 5;
- $order->save();
- }
- break;
- // 配送完成
- case IntraCityExpress::ORDER_STATUS_COMPLETED:
- if ($order->status == 3) {
- $eo->orderStatus = 8;
- $eo->save();
- $order->status = 4;//配送完成
- $order->sendStatus = 2;//配送完成
- $order->save();
- }
- break;
- // 商家取消订单
- case IntraCityExpress::ORDER_STATUS_CANCELED_BY_MERCHANT:
- if ($order->status == 3) {
- $eo->orderStatus = 2;
- $eo->save();
- $order->status = 2;//取消配送
- $order->sendStatus = 3;//取消配送
- $order->save();
- $msg = '取消配送,订单编号 ';
- }
- break;
- // 配送员撤单
- case IntraCityExpress::ORDER_STATUS_WITHDRAWN:
- //配送员撤单,目前我们认为会重新派单,先不要有任何操作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- $msg = '跑腿取消配送,订单编号 ';
- break;
- // 配送方取消订单
- case IntraCityExpress::ORDER_STATUS_CANCELED_BY_DELIVERY:
- if ($order->status == 3) {
- $eo->orderStatus = 3;
- $eo->status = 0;
- $eo->save();
- $order->status = 2;//取消配送
- $order->sendStatus = 3;//取消配送
- $order->save();
- $msg = '配送方取消配送,订单编号 ';
- }
- break;
- // 配送异常
- case IntraCityExpress::ORDER_STATUS_EXCEPTION:
- if ($order->status == 3) {
- $eo->orderStatus = 9;
- $eo->status = 0;
- $eo->save();
- $order->status = 2;//取消配送
- $order->sendStatus = 3;//取消配送
- $order->save();
- $msg = '订单配送异常,订单编号 ';
- }
- break;
- }
- if (!empty($msg)) { // $msg 不为空,则发通知
- $order = OrderClass::getById($orderId, true, 'id, shopId, customName, sendNum');
- $shop = ShopClass::getById($order->shopId, true, 'id, ptStyle, mainId');
- $result = $msg . $order->sendNum;
- WxMessageClass::expressErrorInform($shop, $order->customName, $orderId, $result);
- }
- $tx->commit(); // ====================== 事务提交
- return $this->asJson(['return_code' => 0, 'return_msg' => 'OK']); // 成功状态的返回数据
- } catch (\Exception $e) {
- Yii::error('同城配送回调处理异常:' . $e->getMessage());
- return $this->asJson(['return_code' => 1, 'return_msg' => '系统错误']);
- }
- }
- //模拟接口回调 ssh 20250826
- public function actionMockNotify()
- {
- if (getenv('YII_ENV') != 'production') {
- $post = Yii::$app->request->post();
- $orderStatus = $post['orderStatus'] ?? 0;
- $wxOrderId = $post['wxOrderId'] ?? '';
- $ptStyle = dict::getDict('ptStyle', 'hd');
- $merchant = WxOpenClass::getWxInfo();
- $ret = IntraCityExpress::mockNotify($orderStatus, $wxOrderId, '', '', $merchant, $ptStyle);
- util::success($ret);
- }
- util::complete();
- }
- }
|