CatListForm.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\cyy\ApiResponse;
  9. use app\models\Cat;
  10. use app\modules\client\models\ApiModel;
  11. class CatListForm extends ApiModel
  12. {
  13. public $store_id;
  14. public $limit;
  15. public function rules()
  16. {
  17. return [
  18. [['store_id', 'limit'], 'integer'],
  19. ];
  20. }
  21. public function search()
  22. {
  23. if (!$this->validate()) {
  24. return [
  25. 'code' => 1,
  26. 'msg' => $this->getErrorSummary(false)[0],
  27. ];
  28. }
  29. $cat_list_cache_key = 'default_cat_list_cache_' . $this->store_id . '_' . $this->limit;
  30. if ($list = cache()->get($cat_list_cache_key)) {
  31. return [
  32. 'code' => 0,
  33. 'msg' => 'success',
  34. 'data' => ['list' => $list]
  35. ];
  36. }
  37. $query = Cat::find()->where([
  38. 'is_delete' => 0,
  39. 'parent_id' => 0,
  40. 'is_show' => 1,
  41. ]);
  42. if ($this->store_id > 0) {
  43. $query->andWhere(['store_id' => $this->store_id]);
  44. }
  45. if ($this->limit) {
  46. $query->limit($this->limit);
  47. }
  48. $query->orderBy('sort ASC');
  49. $list = $query->select('id,store_id,parent_id,name,pic_url,big_pic_url,advert_pic,advert_url')->asArray()->all();
  50. foreach ($list as $i => $item) {
  51. $sub_list = Cat::find()->where([
  52. 'is_delete' => 0,
  53. 'parent_id' => $item['id'],
  54. 'is_show' => 1,
  55. ])->orderBy('sort ASC')
  56. ->select('id,store_id,parent_id,name,pic_url,big_pic_url')->asArray()->all();
  57. $list[$i]['list'] = $sub_list ? $sub_list : [];
  58. if($list[$i]['list']){
  59. foreach ($list[$i]['list'] as $k => $item2) {
  60. $sub_list2 = Cat::find()->where([
  61. 'is_delete' => 0,
  62. 'parent_id' => $item2['id'],
  63. 'is_show' => 1,
  64. ])->orderBy('sort ASC')
  65. ->select('id,store_id,parent_id,name,pic_url,big_pic_url')->asArray()->all();
  66. $list[$i]['list'][$k]['list'] = $sub_list2 ? $sub_list2 : [];
  67. }
  68. }
  69. }
  70. $data = [
  71. 'list' => $list
  72. ];
  73. cache()->set($cat_list_cache_key, $list, 600);
  74. return [
  75. 'code' => 0,
  76. 'msg' => 'success',
  77. 'data' => $data
  78. ];
  79. }
  80. }