| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php
- namespace hd\controllers;
- use Yii;
- use biz\ghs\classes\GhsClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\express\classes\DeliveryAuthTokenClass;
- use bizHd\order\classes\OrderClass;
- use bizHd\express\classes\HdDeliveryOrderClass;
- use hd\models\delivery\CancelOrderForm;
- use hd\models\delivery\CreateOrderForm;
- use biz\shop\classes\ShopClass;
- use common\components\delivery\services\DispatchService;
- use common\components\util;
- use yii\helpers\ArrayHelper;
- class DeliveryController extends BaseController
- {
- public function actionList()
- {
- $platformList = [];
- $platformLogos = [
- 'dada' => ['name' => '达达', 'logo' => '📦'],
- 'shunfeng' => ['name' => '顺丰同城', 'logo' => '🚚'],
- 'meituan' => ['name' => '美团', 'logo' => '🍱'],
- 'fengniao' => ['name' => '蜂鸟', 'logo' => '🍜'],
- 'huolala' => ['name' => '货拉拉跑腿', 'logo' => '🚛'],
- 'shansong' => ['name' => '闪送', 'logo' => '⚡'],
- 'didi' => ['name' => '滴滴', 'logo' => '🚗'],
- 'uu' => ['name' => 'UU跑腿', 'logo' => '🛵']
- ];
- $mainId = $this->mainId;
- $allDeliveries = DeliveryAuthTokenClass::getAllByCondition(['mainId' => $mainId]);
- foreach ($allDeliveries as $d) {
- $item = [
- 'id' => $d['platform'],
- 'logo' => $platformLogos[$d['platform']]['logo'],
- 'name' => $platformLogos[$d['platform']]['name'],
- 'is_authorized' => true,
- 'balance' => $d['balance'] / 100.0,
- 'access_type' => 'oauth'
- ];
- $platformList[] = $item;
- }
- util::success(['platformList' => $platformList]);
- }
- // 花店发货时 -- 获取多个平台报价
- public function actionHdAllDeliveryQuotes()
- {
- $post = Yii::$app->request->post();
- $orderTime = $post['pickupTime'];
- $weight = $post['weight'];
- $remark = $post['remark'] ?? '';
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 3);
- $orderId = intval($post['orderId']);
- $order = OrderClass::getById($orderId);
- if (empty($order)) {
- util::fail('没有找到订单');
- }
- if ($order['mainId'] != $this->mainId) {
- util::fail('不是你的订单');
- }
- if (empty($order['address'])) {
- util::fail('客户地址缺失');
- }
- if (empty($order['long']) || empty($order['lat'])) {
- util::fail('客户地址信息缺失');
- }
- if (empty($order['receiveMobile'])) {
- util::fail('收货人手机为空');
- }
- if (empty($order['receiveUserName'])) {
- util::fail('收货人名称为空');
- }
- $shop = ShopClass::getById($this->shopId);
- $order['weight'] = $weight;
- $order['remark'] = $remark;
- $ds = new DispatchService($this->mainId);
- $platformQuotes = $ds->getAllPlatformPrice($order, $shop, $orderTime);
- if (isset($platformQuotes['error'])) {
- util::fail($platformQuotes['error']);
- }
- // 格式化各平台报价为前端展示格式
- $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
- // 按 price 从低到高排序
- ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
- $ret['deliveryList'] = $deliveryList;
- util::success($ret, "success");
- }
- // 花店向批发买花时 -- 获取多个平台报价
- public function actionAllDeliveryQuotes()
- {
- $post = Yii::$app->request->post();
- $orderTime = date('Y-m-d H:i:s');
- // --------- $ghsShopId ----------
- $ghsId = $post['ghsId'] ?? 0;
- $ghs = GhsClass::getById($ghsId, true);
- if (empty($ghs)) {
- util::fail('获取批发商失败');
- }
- $ghsShopId = $ghs->shopId;
- // --------- $customId
- $customId = $ghs->customId ?? 0;
- $custom = CustomClass::getById($customId, true); // 批发的 custom
- if (empty($custom)) {
- util::fail('获取custom数据失败');
- }
- // 生成随机订单号,不要跟原来的混起来,跑腿-销售单-
- $prefix = 'PT-XSD-' . $ghs->mainId . '-';
- $orderSn = $prefix . round(microtime(true) * 1000);
- //构建出 Order 数据
- $order = [
- 'orderSn' => $orderSn,
- 'customName' => $custom['name'],
- 'customMobile' => $custom['mobile'],
- 'fullAddress' => $custom['fullAddress'],
- 'floor' => $custom['floor'],
- 'dist' => $custom['dist'],
- 'lat' => $custom['lat'],
- 'long' => $custom['long'],
- 'address' => $custom['address'], //'toAddress' => $order['address'],
- 'city' => $custom['city'],
- 'weight' => $post['weight'] ?? 1,
- 'remark' => $post['remark'] ?? '',
- 'prePrice' => floatval($post['totalPrice'] ?? 0),
- 'actPrice' => floatval($post['totalPrice'] ?? 0),
- ];
- $ghsShop = ShopClass::getById($ghsShopId);
- $ds = new DispatchService($ghs->mainId);
- $platformQuotes = $ds->getAllPlatformPrice($order, $ghsShop, $orderTime);
- if (isset($platformQuotes['error'])) {
- util::fail($platformQuotes['error']);
- }
- $deliveryList = [];
- // 格式化各平台报价为前端展示格式
- $deliveryList = $ds->formatPlatformQuotesForDisplay($platformQuotes);
- // 按 price 从低到高排序
- ArrayHelper::multisort($deliveryList, 'price', SORT_ASC);
- $ret['deliveryList'] = $deliveryList;
- util::success($ret, "success");
- }
- // 创建订单(真实下单)
- public function actionCreateOrder()
- {
- $form = new CreateOrderForm();
- $form->loadAndValidate();
- $post = $form->getAttributes();
- $orderId = $post['orderId'] ?? 0; // xhOrder.id
- $params = $post['allParams'] ?? '';
- $platform = $params['en_name'] ?? '';// 平台英文名,$post['platform'] 是中文
- if (empty($platform)) {
- util::fail('请选择跑腿');
- }
- $pickupTime = $post['pickupTime'] ?? '';
- $params['mainId'] = $this->mainId;
- $params['ip'] = Yii::$app->request->userIP;
- $params['weight'] = $post['weight'] ?? 1;
- $params['remark'] = $post['remark'] ?? '';
- $params['total_price_fen'] = $post['price'] ?? '';
- $params['pickupTime'] = $pickupTime['value'] ?? '';
- $Order = OrderClass::getById($orderId, true);
- if (empty($Order)) {
- util::fail('没有找到订单');
- }
- if ($Order->mainId != $this->mainId) {
- util::fail('不是你的订单');
- }
- if (!in_array($Order['sendStatus'], [-4, -2, -1])) {
- Yii::error('订单已经发过跑腿');
- util::fail('订单已经发过跑腿');
- }
- //避免连续点击的重复提交 ssh
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 3);
- $ds = new DispatchService($this->mainId, $platform);
- $ret = $ds->createOrder($Order, 'xhOrder', $this->shopId, $params);
- if (isset($ret['code']) && $ret['code'] == 0) {
- $gdo = HdDeliveryOrderClass::getByCondition(['mainId' => $this->mainId, 'orderId' => $ret['data']['orderId']]);
- if (empty($gdo)) {
- $data = [
- 'mainId' => $this->mainId,
- 'shopId' => $this->shopId,
- 'deliveryId' => $ret['platform'],
- 'hdOrderId' => $orderId,
- 'orderId' => $ret['data']['orderId'],
- 'distance' => $ret['data']['distance'],
- 'fee' => $ret['data']['fee'],
- 'orderStatus' => 0
- ];
- HdDeliveryOrderClass::add($data);
- } else {
- Yii::info('重复创建订单');
- }
- //更新订单
- $Order->sendDistance = $ret['data']['distance'];
- $Order->sendStatus = 0;
- $Order->deliveryId = $ret['platform'];
- $Order->sendType = 2;// 2指跑腿
- $Order->status = 3;// 3配送中
- $save = $Order->save();
- if ($save) {
- util::success($ret, "success");
- } else {
- util::fail('创建跑腿失败(更新订单失败)');
- }
- } else {
- if (isset($ret['msg'])) {
- util::fail($ret['msg']);
- }
- util::fail('创建跑腿失败');
- }
- }
- // 取消订单
- public function actionCancelOrder()
- {
- $form = new CancelOrderForm();
- $form->loadAndValidate();
- $post = $form->getAttributes();
- $orderId = $post['orderId'] ?? 0; // 平台的orderId,
- $platform = $post['platform'] ?? '';
- $hdOrderId = $post['hdOrderId'];
- // 先获取配送订单,确保 $gdo 始终可用
- $hdo = HdDeliveryOrderClass::getByCondition(['mainId' => $this->mainId, 'hdOrderId' => $hdOrderId, 'orderStatus!=' => -1], true);
- if (empty($hdo)) {
- util::fail('未找到对应订单: ' . $hdOrderId);
- }
- // 如果没有提供平台和订单ID,从配送订单中获取
- if ($platform == '' || $orderId == 0) {
- $orderId = $hdo->orderId;
- $platform = $hdo->deliveryId;
- }
- $ds = new DispatchService($this->mainId, $platform);
- $cancelCostCent = 0;
- if ($platform == 'fengniao') {
- //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
- $cancelParams = ['order_id' => $orderId, 'order_cancel_code' => $post['order_cancel_code']];
- $res = $ds->getAdapter()->preCancelOrder($cancelParams);
- if ($res['code'] != 0) {
- util::fail('运单状态不允许取消');
- }
- $cancelCostCent = $res['data']['actual_cancel_cost_cent']; //取消实际需金额(单位:分)
- }
- if ($platform == 'shunfeng') {
- //请求订单预取消接口,获取是否可取消。如果运单状态不允许取消,则直接返回
- $cancelParams = ['order_id' => $orderId, 'order_cancel_code' => $post['order_cancel_code']];
- $res = $ds->getAdapter()->preCancelOrder($cancelParams);
- if ($res['code'] != 0) {
- util::fail('运单状态不允许取消');
- }
- $post['order_type'] = 1; //查询订单ID类型 1、顺丰订单号 2、商家订单号
- }
- $post['order_cancel_role'] = 1; //1:商户取消, 2:用户取消
- $ret = $ds->cancelOrder($orderId, $post);
- if ($ret['code'] == 0) {
- // 更新跑腿订单与批发订单状态
- $hdo->orderStatus = -1;
- $hdo->cancelCost = $ret['platform'] !== 'fengniao' ? $ret['data']['deductionFee'] : $cancelCostCent; // 由于蜂鸟平台返回的扣费不一样造成
- if (!$hdo->save()) {
- Yii::error('保存配送订单失败: ' . json_encode($hdo->errors));
- util::fail('保存配送订单失败');
- }
- $Order = OrderClass::getById($hdOrderId, true, 'id, sendDistance, sendStatus, deliveryId');
- $Order->sendDistance = 0;
- $Order->sendStatus = -1;
- $Order->status = 2;
- //$Order->deliveryId = '';
- if (!$Order->save()) {
- Yii::error('保存批发订单失败: ' . json_encode($Order->errors));
- util::fail('保存批发订单失败');
- }
- util::success($ret['data'], "success");
- }
- util::fail('取消失败');
- }
- /**
- * 获取订单取消原因
- */
- public function actionCancelReason()
- {
- $post = Yii::$app->request->post();
- $hdOrderId = $post['hdOrderId'];
- $platform = $post['platform'];
- $hdo = HdDeliveryOrderClass::getByCondition(['mainId' => $this->mainId, 'hdOrderId' => $hdOrderId, 'orderStatus!=' => -1], true);
- if (!empty($hdo)) {
- $orderId = $hdo->orderId;
- //$platform = $gdo->deliveryId;
- } else {
- util::fail('未找到对应订单: ' . $hdOrderId);
- }
- $ds = new DispatchService($this->mainId, $platform);
- $ret = $ds->getCancelReasonList($orderId);
- if ($ret['code'] == 0) {
- util::success($ret['data']);
- } else {
- Yii::error('获取订单取消原因:' . json_encode($ret));
- util::fail($ret['msg']);
- }
- }
- public function actionAddress()
- {
- $post = Yii::$app->request->post();
- $adminId = $this->adminId;
- util::checkRepeatCommit($adminId, 4);
- $orderId = intval($post['orderId']);
- $order = OrderClass::getById($orderId, false, 'fullAddress');
- $shop = ShopClass::getById($this->shopId, false, 'fullAddress');
- $ret = [
- "send_address" => $shop['fullAddress'],
- "receive_address" => $order['fullAddress']
- ];
- util::success($ret, "success");
- }
- public function actionCancelAuth()
- {
- // 有平台有对应的取消授权接口,有的没有。
- // 有的就对接下,然后再删除数据。没有的直接删除数据就行。
- $platform = Yii::$app->request->get('platform');
- $delivery = DeliveryAuthTokenClass::getByCondition(['mainId' => $this->mainId, 'platform' => $platform]);
- if ($delivery) {
- if ($platform == 'shansong') {
- $auth = new \common\components\delivery\platform\shansong\Auth();
- $result = $auth->cancelAuthorization($delivery['accessToken']);
- if ($result['success']) {
- // 授权已取消
- } else {
- if ($result['message'] == '商户未授权,请授权后再次尝试') {
- Yii::info($this->mainId . '--商户未授权,请授权后再次尝试');
- } else {
- util::fail('取消授权失败:' . $result['message']);
- }
- }
- }
- } else {
- util::fail('取消授权失败,数据未找到');
- }
- $ret = DeliveryAuthTokenClass::deleteByCondition(['mainId' => $this->mainId, 'platform' => $platform]);
- if ($ret > 0) {
- util::success(['message' => '取消授权成功']);
- } else {
- util::fail('取消授权失败');
- }
- }
- public function actionGetAuthUrl()
- {
- $platform = Yii::$app->request->get('platformId');
- $Auth = new \common\components\delivery\services\AuthService();
- $mainId = $this->mainId;
- switch ($platform) {
- case 'shansong':
- $Auth->shansongAuth($mainId);
- break;
- case 'huolala':
- $Auth->huolalaAuth($mainId);
- break;
- case 'fengniao':
- $Auth->fengniaoAuth($mainId);
- break;
- case 'dada':
- $Auth->dadaAuth($mainId);
- break;
- case 'shunfeng':
- $Auth->shunfengAuth($mainId);
- break;
- case 'didi':
- break;
- default:
- util::error(-1, $platform . '不存在');
- break;
- }
- }
- }
|