TopicFavoriteListForm.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\models\Topic;
  9. use app\models\TopicFavorite;
  10. use yii\data\Pagination;
  11. use app\modules\client\models\ApiModel;
  12. class TopicFavoriteListForm extends ApiModel
  13. {
  14. public $store_id;
  15. public $user_id;
  16. public $page;
  17. public function rules()
  18. {
  19. return [
  20. [['page'], 'integer'],
  21. [['page'], 'default', 'value' => 1],
  22. ];
  23. }
  24. public function search()
  25. {
  26. if (!$this->validate()) {
  27. return $this->errorResponse;
  28. }
  29. $query = TopicFavorite::find()->alias('tf')->leftJoin(['t' => Topic::tableName()], 'tf.topic_id=t.id')
  30. ->where([
  31. 't.is_delete' => 0,
  32. 'tf.is_delete' => 0,
  33. 'tf.user_id' => $this->user_id,
  34. 't.store_id' => $this->store_id,
  35. ]);
  36. $count = $query->count();
  37. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1]);
  38. $list = $query->limit($pagination->limit)->offset($pagination->offset)->orderBy('tf.addtime DESC')->select('t.*')->asArray()->all();
  39. foreach ($list as $i => $item) {
  40. $read_count = intval($item['read_count'] + $item['virtual_read_count']);
  41. unset($list[$i]['read_count']);
  42. unset($list[$i]['virtual_read_count']);
  43. if ($read_count < 10000) {
  44. $read_count = $read_count . '人浏览';
  45. }
  46. if ($read_count >= 10000) {
  47. $read_count = intval($read_count / 10000) . '万+人浏览';
  48. }
  49. $goods_class = 'class="goods-link"';
  50. $goods_count = mb_substr_count($item['content'], $goods_class);
  51. unset($list[$i]['content']);
  52. $list[$i]['read_count'] = $read_count;
  53. if ($goods_count) {
  54. $list[$i]['goods_count'] = $goods_count . '件宝贝';
  55. }
  56. }
  57. return [
  58. 'code' => 0,
  59. 'data' => [
  60. 'list' => $list,
  61. 'totalCount' => $count,
  62. 'pageNo' => $this->page
  63. ],
  64. ];
  65. }
  66. }