TopicListForm.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\cyy\ApiResponse;
  9. use app\models\Topic;
  10. use yii\data\Pagination;
  11. use app\models\TopicType;
  12. use app\modules\alliance\models\ApiModel;
  13. class TopicListForm extends ApiModel
  14. {
  15. public $store_id;
  16. public $page;
  17. public $limit = 20;
  18. public $type;
  19. public $is_chosen;
  20. public function rules()
  21. {
  22. return [
  23. [['page' ,'limit'], 'integer'],
  24. [['page'], 'default', 'value' => 1],
  25. [['limit'], 'default', 'value' => 20],
  26. ['type', 'integer'],
  27. ['is_chosen','integer'],
  28. ];
  29. }
  30. public function search()
  31. {
  32. $topic_list_cache_key = 'default_topic_list_' . $this->store_id .'_'. $this->type . '_' . $this->page . '_' . $this->limit;
  33. if ($list = cache()->get($topic_list_cache_key)) {
  34. return [
  35. 'code' => 0,
  36. 'msg' => 'success',
  37. 'data' => ['list' => $list]
  38. ];
  39. }
  40. if ($this->type === '-1') {
  41. $query = Topic::find()->where(['store_id' => $this->store_id, 'is_delete' => 0,'is_chosen' =>1]);
  42. } elseif ($this->type) {
  43. $query = Topic::find()->where(['store_id' => $this->store_id, 'is_delete' => 0])->andWhere('type=:type', [':type' => $this->type]);
  44. } else {
  45. $query = Topic::find()->where(['store_id' => $this->store_id, 'is_delete' => 0]);
  46. }
  47. $query->andWhere([
  48. 'is_show' => 1
  49. ]);
  50. $count = $query->count();
  51. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  52. $list = $query->orderBy('sort ASC,created_at DESC')->limit($pagination->limit)->offset($pagination->offset)
  53. ->select('id,title,sub_title,cover_pic,read_count,virtual_read_count,virtual_favorite_count,created_at as addtime, layout, content')->asArray()->all();
  54. foreach ($list as $i => $item) {
  55. $read_count = intval($item['read_count'] + $item['virtual_read_count']);
  56. unset($list[$i]['read_count']);
  57. unset($list[$i]['virtual_read_count']);
  58. if ($read_count < 10000) {
  59. $read_count = $read_count . '人浏览';
  60. }
  61. if ($read_count >= 10000) {
  62. $read_count = intval($read_count / 10000) . '万+人浏览';
  63. }
  64. $goods_class = 'class="goods-link"';
  65. $goods_count = mb_substr_count($item['content'], $goods_class);
  66. unset($list[$i]['content']);
  67. $list[$i]['read_count'] = $read_count;
  68. if ($goods_count) {
  69. $list[$i]['goods_count'] = $goods_count . '件宝贝';
  70. }
  71. }
  72. cache()->set($topic_list_cache_key, $list, 60);
  73. return [
  74. 'code' => 0,
  75. 'msg' => 'success',
  76. 'data' => ['list' => $list],
  77. // 'sql' => $query->createCommand()->getRawSql()
  78. ];
  79. }
  80. }