| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\cyy\ApiResponse;
- use app\models\Cat;
- use app\modules\client\models\ApiModel;
- class CatListForm extends ApiModel
- {
- 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
- ];
- }
- }
|