validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $cat_list_cache_key = 'default_cat_list_cache_' . $this->store_id . '_' . $this->limit; if ($list = cache()->get($cat_list_cache_key)) { return [ 'code' => 0, 'msg' => 'success', 'data' => ['list' => $list] ]; } $query = Cat::find()->where([ 'is_delete' => 0, 'parent_id' => 0, 'is_show' => 1, ]); if ($this->store_id > 0) { $query->andWhere(['store_id' => $this->store_id]); } if ($this->limit) { $query->limit($this->limit); } $query->orderBy('sort ASC'); $list = $query->select('id,store_id,parent_id,name,pic_url,big_pic_url,advert_pic,advert_url')->asArray()->all(); foreach ($list as $i => $item) { $sub_list = Cat::find()->where([ 'is_delete' => 0, 'parent_id' => $item['id'], 'is_show' => 1, ])->orderBy('sort ASC') ->select('id,store_id,parent_id,name,pic_url,big_pic_url')->asArray()->all(); $list[$i]['list'] = $sub_list ? $sub_list : []; if($list[$i]['list']){ foreach ($list[$i]['list'] as $k => $item2) { $sub_list2 = Cat::find()->where([ 'is_delete' => 0, 'parent_id' => $item2['id'], 'is_show' => 1, ])->orderBy('sort ASC') ->select('id,store_id,parent_id,name,pic_url,big_pic_url')->asArray()->all(); $list[$i]['list'][$k]['list'] = $sub_list2 ? $sub_list2 : []; } } } $data = [ 'list' => $list ]; cache()->set($cat_list_cache_key, $list, 600); return [ 'code' => 0, 'msg' => 'success', 'data' => $data ]; } }