| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\models\Cat;
- use yii\base\Model;
- class CatListForm extends Model
- {
- public $store_id;
- public $limit;
- public function rules()
- {
- return [
- [['store_id', 'limit'], 'integer'],
- ];
- }
- public function search()
- {
- if (!$this->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
- ];
- }
- }
|