林琦海 5 лет назад
Родитель
Сommit
c9be710ef5

+ 62 - 0
app/ghs/controllers/ExpressController.php

@@ -7,8 +7,13 @@
 namespace ghs\controllers;
 
 
+use bizGhs\custom\classes\CustomClass;
+use bizGhs\custom\services\CustomService;
 use bizGhs\express\services\MiniExpressService;
+use bizGhs\merchant\services\ShopService;
+use bizGhs\product\services\ProductService;
 use common\components\util;
+use Yii;
 
 class ExpressController extends BaseController
 {
@@ -24,7 +29,64 @@ class ExpressController extends BaseController
     //配送费预估
     public function actionPreAddOrder()
     {
+        $post = Yii::$app->request->post();
+        $expressId = $post['expressId']??0;
+        $ghsItemInfo = $post['itemInfo']??'';
+        if(empty($ghsItemInfo)){
+            util::fail('请选择花材');
+        }
+        $ghsItemInfo = json_decode($ghsItemInfo,true); // [{productId:0,itemPrice:1,bigNum:1,smallNum:0}]
 
+        //计算总价格,总重量
+        $cargoInfo = ProductService::groupCargo($ghsItemInfo);
+
+        $shop_order_id = time();
+        $shop_no = 1; //与快递签约时的门店ID(应该是固定的)
+        $openid  = $this->admin['miniOpenId'];
+
+        //客户id
+        $customId = $post['customId']??0;
+        //获取客户name
+        $customInfo = CustomService::getCustomInfo($customId);
+        CustomClass::valid($customInfo, $this->sjId);
+
+        //当前门店的地址信息
+        $shopInfo = ShopService::getById($this->shopId);
+        $sender  =[
+            'name'=>$shopInfo['shopName'],
+            'city'=>$shopInfo['city']??'',
+            'address'=>$shopInfo['address']??'',
+            'address_detail'=>$shopInfo['floor']??'',
+            'phone'=>$shopInfo['telephone']??'',
+            'lng'=>$shopInfo['long']??'',
+            'lat'=>$shopInfo['lat']??'',
+            'coordinate_type'=>0,
+        ];
+        $receiver = [
+            'name'=>$customInfo['name'],
+            'city'=>$post['receiveCity']??'',
+            'address'=>$post['receiveAddress']??'',
+            'address_detail'=>$post['receiveFloor']??'',
+            'phone'=>$customInfo['mobile']??'',
+            'lng'=>$post['receiveLong']??'',
+            'lat'=>$post['receiveLat']??'',
+            'coordinate_type'=>0,
+        ];
+        $cargo = [
+            'goods_value'=>$cargoInfo['totalPrice'], //总价格  元
+            'goods_weight'=>$cargoInfo['totalWeight'], //总重量 kg
+            'cargo_first_class'=>'鲜花',
+            'cargo_second_class'=>'鲜花',
+        ];
+        $orderInfo = [];
+        $shop = [
+            'wxa_path'=>'/shop',
+            'img_url'=>'http://www.baidu.com/test.png',
+            'goods_name'=>'鲜花',
+            'goods_count'=>count($ghsItemInfo),
+        ];
+        $res =  MiniExpressService::preAddOrder($expressId,$shop_no,$shop_order_id,$openid,$sender,$receiver,$cargo,$orderInfo,$shop);
+        util::success(['fee'=>$res['fee']]);
     }
 
     //

+ 6 - 3
biz-ghs/express/services/MiniExpressService.php

@@ -64,7 +64,7 @@ class MiniExpressService
 
     //下单
     /**
-     * @param string $expressId  哪家快递 xhExpress 主键ID
+     * @param int $expressId  哪家快递 xhExpress 主键ID
      * @param string $shopNo  门店ID(商家ID)
      * @param string $orderSn  订单号
      * @param string $openId  下单用户的openid
@@ -129,7 +129,7 @@ class MiniExpressService
                 'goods_count'=>1,
             ],
      */
-    public static function addOrder(string $expressId,string $shopNo,string $orderSn,string $openId,array $sender,array $receiver,array $cargo,array $orderInfo,array $shop, string $subBizId='')
+    public static function addOrder(int $expressId,string $shopNo,string $orderSn,string $openId,array $sender,array $receiver,array $cargo,array $orderInfo,array $shop, string $subBizId='')
     {
         $merchant = self::getGhsWxInfo();
         //通过  $expressId, 获取  appKey, appSecret
@@ -328,7 +328,7 @@ class MiniExpressService
       "delivery_token": "1111111"
     }
      */
-    public static function preAddOrder(string $expressId,string $shopNo,string $orderSn,string $openId,array $sender,array $receiver,array $cargo,array $orderInfo,array $shop, string $subBizId='')
+    public static function preAddOrder(int $expressId,string $shopNo,string $orderSn,string $openId,array $sender,array $receiver,array $cargo,array $orderInfo,array $shop, string $subBizId='')
     {
         $merchant = self::getGhsWxInfo();
         //通过  $expressId, 获取  appKey, appSecret
@@ -422,6 +422,9 @@ class MiniExpressService
 
     public static function handleReturn($res)
     {
+        if(isset($res['errcode'])){
+            util::fail($res['errmsg']);
+        }
         if($res['resultcode'] != 0 ){
             if (getenv('YII_ENV', 'local') == 'production') {
                 util::fail('出错啦!请联系客服');

+ 30 - 0
biz-ghs/product/services/ProductService.php

@@ -3,6 +3,8 @@ namespace bizGhs\product\services;
 
 
 use bizGhs\base\services\BaseService;
+use bizGhs\product\classes\ProductClass;
+use common\services\xhItemService;
 
 class ProductService extends BaseService
 {
@@ -81,4 +83,32 @@ class ProductService extends BaseService
     }
 
 
+    //根据ghsItemInfo [{productId:0,itemPrice:1,bigNum:1,smallNum:0}] 计算总价格,总重量
+    public static function groupCargo($ghsItemInfo)
+    {
+        $productIds = array_column($ghsItemInfo, 'productId');
+        $productData = ProductClass::getProductByIds($productIds);
+        $productData = array_column($productData, NULL, 'id');
+        $itemIds =  array_column($productData,'itemId');
+        $itemData = xhItemService::getByIds($itemIds);
+        $itemData = array_column($itemData, NULL, 'id');
+        $data = [
+            'totalPrice'=>0,
+            'totalWeight'=>0,
+        ];
+        foreach($ghsItemInfo as $v){
+            $productId = $v['productId'];
+            $itemId = $productData[$productId]['itemId'];
+            $unitNum = $itemData[$itemId]['unitNum']??0;
+            $unitWeight = $itemData[$itemId]['unitWeight']??0;// 每扎重量
+            $itemNum = ProductClass::mergeItemNum($v['bigNum'],$v['smallNum'],$unitNum); // 总共多少扎
+            $weight = bcmul($unitWeight,$itemNum,2);
+            $itemPrice =$productData[$productId]['price']??0; //当时售卖的价格
+            $price = bcmul($itemNum,$itemPrice,2);
+            $data['totalWeight'] = bcadd($data['totalWeight'],$weight,2);
+            $data['totalPrice'] =bcadd($data['totalPrice'],$price,2);
+        }
+        return $data;
+    }
+
 }