wesmiler пре 3 месеци
родитељ
комит
00b6c2a2e5

+ 2 - 2
app/Http/Controllers/Api/v1/StoreController.php

@@ -32,9 +32,9 @@ class StoreController extends webApp
     public function goods()
     {
         $params =request()->post();
-        $pageSize = request()->post('pageSize', 15);
         try {
-            $datas = StoreService::make()->getGoodsList($params, $pageSize);
+            $params['user_id'] = $this->userId;
+            $datas = StoreService::make()->getGoodsList($params);
             return showJson(1010, true, $datas);
         }catch (\Exception $exception) {
             $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];

+ 1 - 1
app/Models/StoreModel.php

@@ -59,7 +59,7 @@ class StoreModel extends BaseModel
     public function member()
     {
         return $this->hasOne(MemberModel::class, 'id', 'user_id')
-            ->select(['id', 'realname', 'nickname', 'mobile', 'status']);
+            ->select(['id', 'realname', 'nickname', 'mobile','parents', 'status']);
     }
 
     /**

+ 72 - 9
app/Services/Api/StoreService.php

@@ -11,6 +11,7 @@
 
 namespace App\Services\Api;
 
+use App\Models\MemberModel;
 use App\Models\StoreCategoryModel;
 use App\Models\StoreModel;
 use App\Services\BaseService;
@@ -91,23 +92,22 @@ class StoreService extends BaseService
      * @param int $pageSize
      * @return array
      */
-    public function getGoodsList($params, $pageSize = 15)
+    public function getGoodsList($params, $pageSize = 7)
     {
         $page = request()->post('page',1);
-        $cacheKey = "caches:goods:index_{$page}_{$pageSize}:".md5(json_encode($params));
+        $userId = isset($params['user_id'])? $params['user_id'] : 0;
+        $cacheKey = "caches:stores:goods_{$userId}_{$page}_{$pageSize}:".md5(json_encode($params));
         $list = RedisService::get($cacheKey);
         if($list){
-            return [
-                'pageSize' => $pageSize,
-                'total' => isset($list['total']) ? $list['total'] : 0,
-                'list' => isset($list['data']) ? $list['data'] : []
-            ];
+            return $list;
         }
         $where = ['a.mark' => 1];
         $status = isset($params['status']) && $params['status']>0 ? $params['status'] : 1;
         if ($status > 0) {
             $where['a.status'] = $status;
         }
+
+        $storeId = $this->getStoreId($userId);
         $list = $this->model->with(['goods'=>function($query)use($params){
             $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
             if ($categoryId>0) {
@@ -136,14 +136,18 @@ class StoreService extends BaseService
                         ->orWhere('goods.tags', 'like', "%{$keyword}%");
                 }
             })
-            ->where(function ($query) use ($params) {
+            ->where(function ($query) use ($params,$storeId) {
                 $categoryId = isset($params['category_id']) ? $params['category_id'] : 0;
                 if ($categoryId>0) {
                     $query->where('goods.category_id', $categoryId);
                 }
+
+                // 商家
+                if ($storeId>0) {
+                    $query->where('goods.store_id', $storeId);
+                }
             })
             ->select(['a.id','a.logo','a.name','a.user_id','a.status','a.sort','goods.id as goods_id'])
-            ->groupBy('a.id')
             ->orderBy('a.sort', 'desc')
             ->orderBy('a.id', 'desc')
             ->paginate($pageSize > 0 ? $pageSize : 9999999);
@@ -165,6 +169,65 @@ class StoreService extends BaseService
     }
 
     /**
+     * 获取显示商家ID
+     * @param $userId
+     * @return array|int|mixed
+     */
+    public function getStoreId($userId)
+    {
+        $cacheKey = "caches:storeId:id_{$userId}";
+        $data = RedisService::get($cacheKey);
+        if($data){
+            return $data;
+        }
+
+        // 用户信息
+        $userInfo = MemberModel::with(['store'])->where(['id'=>$userId,'mark'=>1])
+            ->select(['id', 'realname', 'nickname','parents', 'status'])
+            ->first();
+        $store = isset($userInfo['store'])? $userInfo['store'] : [];
+        $parents = isset($userInfo['parents'])? $userInfo['parents'] : '';
+        $parents = $parents? trim($parents,',') : '';
+        $ids = $parents? explode(',', $parents) : [];
+        $ids = array_reverse($ids);
+        $storeId = isset($store['id'])? $store['id'] : 0;
+        $storeStatus = isset($store['status'])? $store['status'] : 0;
+        $storeMark = isset($store['mark'])? $store['mark'] : 0;
+        if(empty($userInfo)){
+            return 0;
+        }
+
+        // 用户自己是商家
+        if($storeId>0 && $storeStatus==1 && $storeMark==1){
+            RedisService::set($cacheKey, $storeId, 7 * 86400);
+            return $storeId;
+        }
+
+        // 用户上级有商家
+        $parentStoreInfo = $this->model->whereIn('user_id',$ids)
+            ->where(['status'=>1,'mark'=>1])
+            ->select(['id','name'])
+            ->orderByRaw(DB::raw("FIND_IN_SET(id,{$parents}) asc"))
+            ->first();
+        $storeId = isset($parentStoreInfo['id'])?$parentStoreInfo['id'] : 0;
+
+        $params = request()->all();
+        $system = isset($params['system']) ? $params['system'] : [];
+        $system = $system && !is_array($system) ? json_decode($system, true) : $system;
+        $uuid = isset($system['uuid']) ? $system['uuid'] : '';
+        $uuKey = "caches:storeId:uu_{$uuid}";
+        if($parentStoreInfo && $storeId>0){
+            RedisService::set($cacheKey, $storeId, 7 * 86400);
+            return $storeId;
+        }else if($uuid && $storeId = RedisService::get($uuKey)){
+            return $storeId;
+        }
+
+        return 0;
+
+    }
+
+    /**
      * 行业分类
      * @return array|mixed
      */