| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace hd\controllers;
- use biz\ghs\classes\GhsClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\express\classes\DeliveryAuthTokenClass;
- use Yii;
- 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 = [
- 'data' => ['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 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);
- if (empty($custom)) {
- util::fail('获取custom数据失败');
- }
- $weight = $post['weight'] ?? 1;
- // 生成随机订单号
- //$snData = ['shopId' => $ghsShopId, 'mainId' => $ghs->mainId, 'ghsId' => $ghsId, 'customId' => $customId];
- //$orderSn = orderSn::getGhsOrderSn($snData);
- $prefix = 'XSD_CS-';
- $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' => $weight,
- 'remark' => $post['remark'] ?? '',
- 'prePrice' => 10, // TODO 要从前端获取
- 'actPrice' => 10, // TODO
- ];
- $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 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;
- }
- }
- }
|