ArticleLists.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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\workerapi\lists;
  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. */
  31. public function setSearch(): array
  32. {
  33. return [
  34. '=' => ['cid']
  35. ];
  36. }
  37. /**
  38. * @notes 自定查询条件
  39. * @return array
  40. */
  41. public function queryWhere()
  42. {
  43. $where[] = ['is_show', '=', 1];
  44. $where['cid'] = 3;//工程师端-重要公告
  45. if (!empty($this->params['keyword'])) {
  46. $where[] = ['title', 'like', '%' . $this->params['keyword'] . '%'];
  47. }
  48. return $where;
  49. }
  50. /**
  51. * @notes 获取文章列表
  52. * @return array
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function lists(): array
  58. {
  59. $orderRaw = 'sort desc, id desc';
  60. $sortType = $this->params['sort'] ?? 'default';
  61. // 最新排序
  62. if ($sortType == 'new') {
  63. $orderRaw = 'id desc';
  64. }
  65. // 最热排序
  66. if ($sortType == 'hot') {
  67. $orderRaw = 'click_actual + click_virtual desc, id desc';
  68. }
  69. $field = 'id,cid,title,desc,image,click_virtual,click_actual,create_time';
  70. $result = Article::field($field)
  71. ->where($this->queryWhere())
  72. ->where($this->searchWhere)
  73. ->orderRaw($orderRaw)
  74. ->append(['click'])
  75. ->hidden(['click_virtual', 'click_actual'])
  76. ->limit($this->limitOffset, $this->limitLength)
  77. ->select()->toArray();
  78. $articleIds = array_column($result, 'id');
  79. $collectIds = ArticleCollect::where(['user_id' => $this->userId, 'status' => YesNoEnum::YES])
  80. ->whereIn('article_id', $articleIds)
  81. ->column('article_id');
  82. foreach ($result as &$item) {
  83. $item['collect'] = in_array($item['id'], $collectIds);
  84. }
  85. return $result;
  86. }
  87. /**
  88. * @notes 获取文章数量
  89. * @return int
  90. */
  91. public function count(): int
  92. {
  93. return Article::where($this->searchWhere)
  94. ->where($this->queryWhere())
  95. ->count();
  96. }
  97. }