ArticleLists.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\lists\article;
  15. use app\api\lists\BaseApiDataLists;
  16. use app\common\enum\YesNoEnum;
  17. use app\common\lists\ListsSearchInterface;
  18. use app\common\model\article\Article;
  19. use app\common\model\article\ArticleCollect;
  20. /**
  21. * 文章列表
  22. * Class ArticleLists
  23. * @package app\api\lists\article
  24. */
  25. class ArticleLists extends BaseApiDataLists implements ListsSearchInterface
  26. {
  27. /**
  28. * @notes 搜索条件
  29. * @return \string[][]
  30. * @author 段誉
  31. * @date 2022/9/16 18:54
  32. */
  33. public function setSearch(): array
  34. {
  35. return [
  36. '=' => ['cid']
  37. ];
  38. }
  39. /**
  40. * @notes 自定查询条件
  41. * @return array
  42. * @author 段誉
  43. * @date 2022/10/25 16:53
  44. */
  45. public function queryWhere()
  46. {
  47. $where[] = ['is_show', '=', 1];
  48. if (!empty($this->params['keyword'])) {
  49. $where[] = ['title', 'like', '%' . $this->params['keyword'] . '%'];
  50. }
  51. return $where;
  52. }
  53. /**
  54. * @notes 获取文章列表
  55. * @return array
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. * @author 段誉
  60. * @date 2022/9/16 18:55
  61. */
  62. public function lists(): array
  63. {
  64. $orderRaw = 'sort desc, id desc';
  65. $sortType = $this->params['sort'] ?? 'default';
  66. // 最新排序
  67. if ($sortType == 'new') {
  68. $orderRaw = 'id desc';
  69. }
  70. // 最热排序
  71. if ($sortType == 'hot') {
  72. $orderRaw = 'click_actual + click_virtual desc, id desc';
  73. }
  74. $field = 'id,cid,title,desc,image,click_virtual,click_actual,create_time';
  75. $result = Article::field($field)
  76. ->where($this->queryWhere())
  77. ->where($this->searchWhere)
  78. ->orderRaw($orderRaw)
  79. ->append(['click'])
  80. ->hidden(['click_virtual', 'click_actual'])
  81. ->limit($this->limitOffset, $this->limitLength)
  82. ->select()->toArray();
  83. $articleIds = array_column($result, 'id');
  84. $collectIds = ArticleCollect::where(['user_id' => $this->userId, 'status' => YesNoEnum::YES])
  85. ->whereIn('article_id', $articleIds)
  86. ->column('article_id');
  87. foreach ($result as &$item) {
  88. $item['collect'] = in_array($item['id'], $collectIds);
  89. }
  90. return $result;
  91. }
  92. /**
  93. * @notes 获取文章数量
  94. * @return int
  95. * @author 段誉
  96. * @date 2022/9/16 18:55
  97. */
  98. public function count(): int
  99. {
  100. return Article::where($this->searchWhere)
  101. ->where($this->queryWhere())
  102. ->count();
  103. }
  104. }