wesmiler 2 年之前
父節點
當前提交
e383706d87
共有 4 個文件被更改,包括 83 次插入4 次删除
  1. 16 0
      app/Http/Controllers/Api/v1/GoodsController.php
  2. 9 0
      app/Models/CartsModel.php
  3. 57 4
      app/Services/Api/GoodsService.php
  4. 1 0
      routes/api.php

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

@@ -84,4 +84,20 @@ class GoodsController extends webApp
             return showJson(1002, true, $result);
         }
     }
+
+    /**
+     * 购物车列表
+     * @return array
+     */
+    public function cartList()
+    {
+        try {
+            $pageSize = request()->post('pageSize', 30);
+            $datas = GoodsService::make()->getCartList($this->userId, $pageSize);
+            return showJson(1010, true, $datas);
+        } catch (\Exception $exception){
+            RedisService::set("caches:request:error_cart_index", ['trace'=>$exception->getTrace()], 7200);
+            return showJson(1018, false, ['error'=>env('APP_DEBUG')? $exception->getMessage() : '']);
+        }
+    }
 }

+ 9 - 0
app/Models/CartsModel.php

@@ -23,5 +23,14 @@ class CartsModel extends BaseModel
     // 设置数据表
     protected $table = 'carts';
 
+    /**
+     * 规格
+     * @return \Illuminate\Database\Eloquent\Relations\HasOne
+     */
+    public function sku()
+    {
+        return $this->hasOne(GoodsSkuModel::class, 'sku_id','sku_id')
+            ->where(['mark'=>1]);
+    }
 
 }

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

@@ -231,18 +231,67 @@ class GoodsService extends BaseService
         $cartId = CartsModel::where(['user_id'=> $userId,'goods_id'=> $goodsId,'sku_id'=>$skuId])->value('id');
         if($cartId){
             CartsModel::where(['id'=> $cartId])->update(['num'=> $num,'status'=> $status,'mark'=>1,'update_time'=>time()]);
-            RedisService::clear("caches:members:carts_{$userId}");
+            RedisService::clear("caches:members:cartList:{$userId}");
+            RedisService::clear("caches:members:cartCount:{$userId}");
             return ['id'=> $cartId];
         }else{
             $cartId = CartsModel::insertGetId(['user_id'=> $userId,'goods_id'=>$goodsId,'sku_id'=>$skuId,'num'=> $num,'status'=> $status,'mark'=>1,'create_time'=>time()]);
-            RedisService::clear("caches:members:carts_{$userId}");
+            RedisService::clear("caches:members:cartList:{$userId}");
+            RedisService::clear("caches:members:cartCount:{$userId}");
             return ['id'=> $cartId];
         }
     }
 
-    public function getCartList($)
+    public function getCartList($userId, $pageSize = 30)
     {
+        $cacheKey = "caches:members:cartList:{$userId}";
+        $cacheCountKey = "caches:members:cartCount:{$userId}";
+        $datas = RedisService::get($cacheKey);
+        if($datas){
+            return $datas;
+        }
+
+        $datas = 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')
+            ->where(['a.status' => 1, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1])
+            ->where('b.cost_price', '>', 0)
+            ->select(['b.*','a.num','a.sku_id'])
+            ->orderBy('a.create_time','desc')
+            ->limit($pageSize)
+            ->get();
+        if($datas){
+
+            // 价格等参数格式化
+            $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 ($datas 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);
+
+            RedisService::set($cacheCountKey, count($datas), rand(300, 600));
+            RedisService::set($cacheKey, $datas, rand(300, 600));
+        }
+
 
+        return $datas;
     }
 
     /**
@@ -258,7 +307,11 @@ class GoodsService extends BaseService
             return $data;
         }
 
-        $data = CartsModel::where(['user_id'=> $userId,'status'=>1,'mark'=>1])->count('id');
+        $data = CartsModel::from('carts as a')
+            ->leftJoin('goods as b','b.goods_id','=','a.goods_id')
+            ->where(['a.status' => 1, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1])
+            ->where('b.cost_price', '>', 0)
+            ->count('a.id');
         if($data){
             RedisService::set($cacheKey, $data, rand(300, 600));
         }

+ 1 - 0
routes/api.php

@@ -149,6 +149,7 @@ Route::prefix('v1')->group(function(){
     Route::post('/goods/freight', [\App\Http\Controllers\Api\v1\GoodsController::class, 'freight']);
 
     // 购物车
+    Route::post('/cart/index', [\App\Http\Controllers\Api\v1\GoodsController::class, 'cartList']);
     Route::post('/cart/update', [\App\Http\Controllers\Api\v1\GoodsController::class, 'updateCart']);
 
     // 订单