CatListForm.php 2.6 KB

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