Files
2026-06-11 12:15:29 +08:00

156 lines
5.1 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台(PHP版)
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用,可去除界面版权logo
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
// | github下载:https://github.com/likeshop-github/likeadmin
// | 访问官网:https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\api\lists\article;
use app\api\lists\BaseApiDataLists;
use app\api\logic\ArticleLogic;
use app\common\enum\YesNoEnum;
use app\common\lists\ListsSearchInterface;
use app\common\model\article\Article;
use app\common\model\article\ArticleCollect;
use app\common\service\article\ArticleImageProxyService;
use app\common\model\user\UserChannelConfig;
/**
* 文章列表
* Class ArticleLists
* @package app\api\lists\article
*/
class ArticleLists extends BaseApiDataLists implements ListsSearchInterface
{
/**
* @notes 搜索条件
* @return \string[][]
* @author 段誉
* @date 2022/9/16 18:54
*/
public function setSearch(): array
{
return [
'=' => ['cid']
];
}
/**
* @notes 自定查询条件
* @return array
* @author 段誉
* @date 2022/10/25 16:53
*/
public function queryWhere()
{
$where[] = ['is_show', '=', 1];
if (!empty($this->params['keyword'])) {
$where[] = ['title', 'like', '%' . $this->params['keyword'] . '%'];
}
if (isset($this->params['is_recommend']) && $this->params['is_recommend'] !== '') {
$where[] = ['is_recommend', '=', $this->params['is_recommend']];
}
if (!empty($this->params['is_mychannel'])) {
if ($this->userId) {
$config = UserChannelConfig::where('user_id', $this->userId)->findOrEmpty();
$channelIds = [];
if (!$config->isEmpty()) {
$raw = $config['channel_ids'];
if (is_string($raw)) {
$channelIds = json_decode($raw, true) ?: [];
} elseif (is_array($raw)) {
$channelIds = $raw;
}
}
if (!empty($channelIds)) {
$where[] = ['cid', 'in', $channelIds];
} else {
$where[] = ['id', '=', 0];
}
} else {
$where[] = ['id', '=', 0];
}
}
return $where;
}
/**
* @notes 获取文章列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 段誉
* @date 2022/9/16 18:55
*/
public function lists(): array
{
$orderRaw = 'published_at desc, id desc';
$sortType = $this->params['sort'] ?? 'default';
$cid = (int) ($this->params['cid'] ?? 0);
// 最新排序
if ($sortType == 'new') {
$orderRaw = 'published_at desc, id desc';
}
// 最热排序
if ($sortType == 'hot') {
$orderRaw = 'click_actual + click_virtual desc, id desc';
}
$field = 'id,cid,title,desc,content,author,image,is_video,duration,video_url,click_virtual,click_actual,create_time,ext';
$result = Article::field($field)
->withCount(['comments' => 'comment_count'])
->where($this->queryWhere())
->where($this->searchWhere)
->orderRaw($orderRaw)
->append(['click'])
->hidden(['click_virtual', 'click_actual'])
->limit($this->limitOffset, $this->limitLength)
->select()->toArray();
$articleIds = array_column($result, 'id');
$collectIds = ArticleCollect::where(['user_id' => $this->userId, 'status' => YesNoEnum::YES])
->whereIn('article_id', $articleIds)
->column('article_id');
foreach ($result as &$item) {
$item['collect'] = in_array($item['id'], $collectIds);
if (!empty($item['content'])) {
$item['content'] = mb_substr(strip_tags($item['content']), 0, 100);
}
$item['translated_content'] = $cid === 22 ? ArticleLogic::getCachedTranslatedContent($item) : '';
$item = ArticleImageProxyService::rewriteArticlePayload($item);
unset($item['ext']);
}
return $result;
}
/**
* @notes 获取文章数量
* @return int
* @author 段誉
* @date 2022/9/16 18:55
*/
public function count(): int
{
return Article::where($this->searchWhere)
->where($this->queryWhere())
->count();
}
}