wesmiler 2 vuotta sitten
vanhempi
commit
9649f5af62

+ 16 - 0
app/Http/Controllers/Api/v1/GoodsController.php

@@ -33,6 +33,22 @@ class GoodsController extends webApp
     }
     }
 
 
     /**
     /**
+     * 购买列表
+     * @return array
+     */
+    public function buyList()
+    {
+        try {
+            $params = request()->post();
+            $datas = GoodsService::make()->getBuyList($this->userId, $params);
+            return showJson(1010, true, $datas);
+        } catch (\Exception $exception){
+            RedisService::set("caches:request:error_goods_buy", ['trace'=>$exception->getTrace()], 7200);
+            return showJson(1018, false, ['error'=>env('APP_DEBUG')? $exception->getMessage() : '']);
+        }
+    }
+
+    /**
      * 分类
      * 分类
      * @return array
      * @return array
      */
      */

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

@@ -154,7 +154,7 @@ class GoodsService extends BaseService
      * @param $id
      * @param $id
      * @return array
      * @return array
      */
      */
-    public function getInfo($goodsId, $userId=0)
+    public function getInfo($goodsId, $userId=0, $updateView=true)
     {
     {
         $field = ['a.*'];
         $field = ['a.*'];
         $info = $this->model->from('goods as a')->with(['category','skuList'])
         $info = $this->model->from('goods as a')->with(['category','skuList'])
@@ -198,8 +198,10 @@ class GoodsService extends BaseService
                 unset($v);
                 unset($v);
             }
             }
 
 
-
-            $this->updateView($userId, $goodsId);
+            // 更新访问量
+            if($updateView){
+                $this->updateView($userId, $goodsId);
+            }
         }
         }
 
 
         return $info;
         return $info;
@@ -207,6 +209,82 @@ class GoodsService extends BaseService
     }
     }
 
 
     /**
     /**
+     * 获取要购买或结算的商品列表
+     * @param $userId
+     * @param array $params
+     * @return array|false|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
+     */
+    public function getBuyList($userId, $params=[])
+    {
+        $goodsId = isset($params['goods_id'])? $params['goods_id'] : 0;
+        $skuId = isset($params['sku_id'])? $params['sku_id'] : 0;
+        $num = isset($params['num'])? $params['num'] : 0;
+        $cartIds = isset($params['cart_ids'])? $params['cart_ids'] : '';
+        $cartIds = $cartIds? explode('|', $cartIds) : [];
+        if(empty($goodsId) && empty($cartIds)){
+            $this->error = 2901;
+            return false;
+        }
+
+        if($goodsId && (empty($skuId) || $num<=0)){
+            $this->error = 2901;
+            return false;
+        }
+
+        $goods = [];
+        if($goodsId){
+            $info = $this->getInfo($goodsId, $userId, false);
+            if($info){
+                $info['num'] = $num;
+                $info['sku'] = GoodsSkuModel::where(['goods_id'=> $goodsId,'sku_id'=> $skuId,'mark'=>1])->first();
+                $goods[] = $info;
+            }
+        }else {
+            $goods = CartsModel::with(['sku'])->from('carts as a')
+                ->leftJoin('goods as b','b.goods_id','=','a.goods_id')
+                ->leftJoin('goods_sku as c','c.sku_id','=','a.sku_id')
+                ->whereIn('a.id', $cartIds)
+                ->where(['a.status' => 1, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1])
+                ->where('b.cost_price', '>', 0)
+                ->select(['b.goods_id','b.merch_id','b.goods_name','b.supply_type','b.main_img','b.cost_price','b.retail_price','b.limit_num','b.lowest_num','b.brand_name','a.num','a.sku_id'])
+                ->get();
+            if($goods){
+
+                // 价格等参数格式化
+                $locale = RedisService::get("caches:locale:lang_{$userId}");
+                $locale = $locale ? $locale : session('locale_lang');
+                $locale = $locale ? $locale : 'zh-cn';
+                $supplyList = config('goods.supplyList');
+
+                $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
+                if($usdtPrice<=0){
+                    $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
+                    $usdtPrice = $usdtCnyPrice>0 && $usdtCnyPrice< 100? $usdtCnyPrice : 0;
+                }
+                $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
+                $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
+
+                foreach ($goods as &$item) {
+                    $item['detail_img'] = isset($item['detail_img']) && $item['detail_img'] ? json_decode($item['detail_img'], true) : [];
+                    $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
+                    $item['usdt_price'] = $usdtPrice;
+                    $item['xd_price_rate'] = $xdPrice;
+                    $item['original_price'] = $item['cost_price'];
+                    $item['cost_price'] = $usdtPrice > 0 ? moneyFormat($item['cost_price'] / $usdtPrice * $xdPrice, 2) : $item['cost_price'];
+                }
+                unset($item);
+            }
+        }
+
+        if(empty($goods)){
+            $this->error = 2901;
+            return false;
+        }
+
+        return $goods;
+    }
+
+    /**
      * 添加/更新购物车
      * 添加/更新购物车
      * @param $userId
      * @param $userId
      * @param $goodsId
      * @param $goodsId
@@ -263,7 +341,7 @@ class GoodsService extends BaseService
             ->leftJoin('goods_sku as c','c.sku_id','=','a.sku_id')
             ->leftJoin('goods_sku as c','c.sku_id','=','a.sku_id')
             ->where(['a.status' => 1, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1])
             ->where(['a.status' => 1, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1])
             ->where('b.cost_price', '>', 0)
             ->where('b.cost_price', '>', 0)
-            ->select(['b.*','a.num','a.sku_id'])
+            ->select(['b.goods_id','b.merch_id','b.goods_name','b.supply_type','b.main_img','b.cost_price','b.retail_price','b.limit_num','b.lowest_num','b.brand_name','a.num','a.sku_id'])
             ->orderBy('a.create_time','desc')
             ->orderBy('a.create_time','desc')
             ->limit($pageSize)
             ->limit($pageSize)
             ->get();
             ->get();
@@ -326,6 +404,9 @@ class GoodsService extends BaseService
         return $data;
         return $data;
     }
     }
 
 
+    /**
+     * 运费
+     */
     public function getFreight($userId, $addressId,$skuList)
     public function getFreight($userId, $addressId,$skuList)
     {
     {
         $cacheKey = "caches:goods:freight:{$userId}_{$addressId}_".md5(json_encode($skuList,256));
         $cacheKey = "caches:goods:freight:{$userId}_{$addressId}_".md5(json_encode($skuList,256));

+ 1 - 0
resources/lang/zh-cn/api.php

@@ -174,6 +174,7 @@ return [
     '2811'=> '申请入驻成功,请耐心等候审核',
     '2811'=> '申请入驻成功,请耐心等候审核',
     '2812'=> '账号入驻,请先申请入驻并审核通过后使用',
     '2812'=> '账号入驻,请先申请入驻并审核通过后使用',
 
 
+    '2901'=> '请先选择购买或结算的商品',
 
 
     
     
     '我正在直播,快来看看吧'=>'我正在直播,快来看看吧',
     '我正在直播,快来看看吧'=>'我正在直播,快来看看吧',

+ 1 - 0
routes/api.php

@@ -144,6 +144,7 @@ Route::prefix('v1')->group(function(){
 
 
     // 商品
     // 商品
     Route::post('/goods/index', [\App\Http\Controllers\Api\v1\GoodsController::class, 'index']);
     Route::post('/goods/index', [\App\Http\Controllers\Api\v1\GoodsController::class, 'index']);
+    Route::post('/goods/buyList', [\App\Http\Controllers\Api\v1\GoodsController::class, 'buyList']);
     Route::post('/goods/category', [\App\Http\Controllers\Api\v1\GoodsController::class, 'category']);
     Route::post('/goods/category', [\App\Http\Controllers\Api\v1\GoodsController::class, 'category']);
     Route::post('/goods/info', [\App\Http\Controllers\Api\v1\GoodsController::class, 'info']);
     Route::post('/goods/info', [\App\Http\Controllers\Api\v1\GoodsController::class, 'info']);
     Route::post('/goods/freight', [\App\Http\Controllers\Api\v1\GoodsController::class, 'freight']);
     Route::post('/goods/freight', [\App\Http\Controllers\Api\v1\GoodsController::class, 'freight']);