wesmiler пре 2 месеци
родитељ
комит
d78ecf0a4c

+ 6 - 2
app/Http/Controllers/Api/v1/GoodsController.php

@@ -33,8 +33,12 @@ class GoodsController extends webApp
         $params =request()->post();
         $goods = isset($params['goods']) && $params['goods'] ? $params['goods'] : [];
         $ids = $goods ? array_column($goods, 'id') : [];
-        $datas = GoodsService::make()->getOrderGoods($ids, $goods, '', $this->userId);
-        return message(1010, true, $datas);
+        if($datas = GoodsService::make()->getOrderGoods($ids, $goods, $this->userId)){
+            return message(1010, true, $datas);
+        }else{
+            return message(GoodsService::make()->getError(), false);
+        }
+
     }
 
     /**

+ 27 - 4
app/Services/Api/GoodsService.php

@@ -15,6 +15,7 @@ use App\Models\GoodsCategoryModel;
 use App\Models\GoodsCollectModel;
 use App\Models\GoodsModel;
 use App\Models\GoodsSkuModel;
+use App\Models\MemberModel;
 use App\Services\BaseService;
 use App\Services\RedisService;
 
@@ -217,11 +218,25 @@ class GoodsService extends BaseService
     /**
      * @param $ids
      * @param $goods
-     * @param $orderNo
+     * @param $userId
+     * @param $orderNo 订单号
      * @return array|false
      */
-    public function getOrderGoods($ids, $goods, $orderNo='', $userId=0)
+    public function getOrderGoods($ids, $goods, $userId, $orderNo='')
     {
+        // 用户信息
+        if(empty($orderNo)){
+            $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
+                ->select(['id', 'discount_point', 'status'])
+                ->first();
+            $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
+            $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
+            if (empty($userInfo) || $status != 1) {
+                $this->error = 1045;
+                return false;
+            }
+        }
+
         $list = $this->model->whereIn('id', $ids)
             ->where(['status'=>1,'mark'=>1])
             ->select(['id as goods_id','goods_name','category_id','store_id','delivery_fee','sku_type','price','stock','unit','weight','thumb','sku_type'])
@@ -230,7 +245,7 @@ class GoodsService extends BaseService
         if($list){
             $skus = GoodsSkuModel::whereIn('goods_id', $ids)->select(['id','sku_name','price','stock'])->get()->keyBy('id');
             $skus = $skus?$skus->toArray() :[];
-            $result = ['store_id'=>0,'delivery_fee'=>0,'total'=>0,'count'=>0,'goods'=>[]];
+            $result = ['store_id'=>0,'discount_total'=>0,'delivery_fee'=>0,'goods_total'=>0,'order_total'=>0,'count'=>0,'goods'=>[]];
             foreach ($list as $item){
                 $item['order_no'] = $orderNo;
                 $id = isset($item['goods_id'])?$item['goods_id']:0;
@@ -276,9 +291,17 @@ class GoodsService extends BaseService
                     $item['total'] = $total;
                     $item['thumb'] = $orderNo?get_image_path($item['thumb']):$item['thumb'];
                     $result['goods'][] = $item;
-                    $result['total'] += $total;
+                    $result['goods_total'] += $total;
+                    $result['order_total'] += $total;
                     $result['count']++;
                 }
+
+                // 会员折扣
+                $orderTotal = $result['order_total'];
+                if($discountPoint>0 && $discountPoint<1){
+                    $result['order_total'] = moneyFormat((1-$discountPoint) * $result['order_total'],2);
+                    $result['discount_total'] = moneyFormat($orderTotal - $result['order_total'],2);
+                }
             }
 
             return $result;

+ 25 - 32
app/Services/Api/OrderService.php

@@ -208,10 +208,29 @@ class OrderService extends BaseService
                 return false;
             }
 
+            // 用户信息
+            RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
+            $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
+                ->select(['id', 'openid', 'mobile', 'nickname','discount_point', 'realname', 'balance', 'status'])
+                ->first();
+            $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
+            $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
+            $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
+            if (empty($userInfo) || $status != 1) {
+                $this->error = 1045;
+                RedisService::clear($cacheLockKey);
+                return false;
+            }
+
+            if (empty($openid)) {
+                $this->error = '用户微信未授权,请重新授权登录';
+                RedisService::clear($cacheLockKey);
+                return false;
+            }
+
             // 商品数据
             $orderNo = get_order_num('JK');
-            RedisService::set($cacheLockKey, ['params' => $params, 'user_id' => $userId], rand(3, 5));
-            $result = GoodsService::make()->getOrderGoods($ids, $goods, $orderNo, $userId);
+            $result = GoodsService::make()->getOrderGoods($ids, $goods, $userId, $orderNo);
             if (empty($result)) {
                 RedisService::clear($cacheLockKey);
                 $this->error = GoodsService::make()->getError();
@@ -219,7 +238,9 @@ class OrderService extends BaseService
             }
 
             $orderGoods = isset($result['goods']) ? $result['goods'] : [];
-            $orderTotal = isset($result['total']) ? $result['total'] : 0;
+            $goodsTotal = isset($result['goods_total']) ? $result['goods_total'] : 0; // 商品总价
+            $orderTotal = isset($result['order_total']) ? $result['order_total'] : 0; // 订单结算金额(折后)
+            $discountTotal = isset($result['discount_total']) ? $result['discount_total'] : 0; // 折扣金额
             $orderCount = isset($result['count']) ? $result['count'] : 0;
             $deliveryFee = isset($result['delivery_fee']) ? $result['delivery_fee'] : 0; // 运费
             $storeId = isset($result['store_id']) ? $result['store_id'] : 0; // 企业/商家
@@ -241,25 +262,6 @@ class OrderService extends BaseService
                 return false;
             }
 
-            // 用户信息
-            $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
-                ->select(['id', 'openid', 'mobile', 'nickname','discount_point', 'realname', 'balance', 'status'])
-                ->first();
-            $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
-            $openid = isset($userInfo['openid']) ? $userInfo['openid'] : '';
-            $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
-            if (empty($userInfo) || $status != 1) {
-                $this->error = 1045;
-                RedisService::clear($cacheLockKey);
-                return false;
-            }
-
-            if (empty($openid)) {
-                $this->error = '用户微信未授权,请重新授权登录';
-                RedisService::clear($cacheLockKey);
-                return false;
-            }
-
             // 收货地址信息
             $addressInfo = MemberAddressService::make()->getBindInfo($userId, $addressId);
             $realname = isset($addressInfo['realname']) ? $addressInfo['realname'] : '';
@@ -280,25 +282,16 @@ class OrderService extends BaseService
             $bonusRate = $bonusRate > 0 && $bonusRate <= 100 ? $bonusRate : $storeBonusRate;
             $bonus = moneyFormat($orderTotal * $bonusRate / 100, 2);
 
-            // 折扣
-            $discountTotal = 0;
-            $total = $orderTotal;
-            if($discountPoint>0 && $discountPoint<1){
-                $orderTotal = moneyFormat((1-$discountPoint) * $discountPoint,2);
-                $discountTotal = moneyFormat($total - $orderTotal,2);
-            }
-
             if (env('PAY_DEBUG')) {
                 $orderTotal = 0.1;
             }
 
-
             // 订单数据
             $order = [
                 'order_no' => $orderNo,
                 'user_id' => $userId,
                 'store_id' => $storeId,
-                'total' => $total, // 商品总价
+                'total' => $goodsTotal, // 商品总价
                 'num' => $orderCount,
                 'pay_total' => $orderTotal, // 折扣后商品总价
                 'discount_point' => $discountPoint,