qingjian/app/model/Article.php

244 lines
7.3 KiB
PHP
Raw Normal View History

2021-08-06 10:50:55 +00:00
<?php
namespace app\model;
class Article extends Base
{
//获取最高访问的文章列表
public static function getMostVisited($limit = 5)
{
if ($limit <= 0) {
$limit = 5;
}
return self::order('views', 'desc')
->limit($limit)
->select()
->toArray();
}
//获取栏目下最新记录
public static function getLatestByCategory($categoryId, $limit = 5)
{
if (empty($categoryId)) {
return [];
}
if ($limit <= 0) {
$limit = 5;
}
return self::where('category_id', $categoryId)
->order('create_time', 'desc')
->limit($limit)
->select()
->toArray();
}
//根据文章ID和栏目ID获取下一篇文章
public static function getNextArticleByIdAndCategoryId($id, $categoryId)
{
return self::where('id', '<', $id)
->where('category_id', $categoryId)
->where('status', 1)
->order('sort desc')
->findOrEmpty()
->toArray();
}
//根据文章ID和栏目ID获取上一篇文章
public static function getPrevArticleByIdAndCategoryId($id, $categoryId)
{
return self::where('id', '>', $id)
->where('category_id', $categoryId)
->where('status', 1)
->order('sort asc')
->findOrEmpty()
->toArray();
}
//根据栏目ID获取文章列表
public static function getListByCategory($categoryId, $limit = 10)
{
return self::where('category_id', $categoryId)
->where('status', 1)
->order("sort desc")
->limit($limit)
->select()
->toArray();
}
//根据栏目ID获取文章列表
public static function getListByCategoryTop($categoryId, $limit = 10)
{
$where = [
["category_id", "=", $categoryId],
["status", "=", 1],
["top", "=", 1],
];
return self::where($where)
->order("sort desc")
->limit($limit)
->select()
->toArray();
}
public static function getNewsAllList($pageSize)
{
$where = [
["category_id", "in", self::newsChildrenIds]
];
return self::where($where)
->order("sort desc")
->paginate($pageSize, false);
}
//根据栏目ID获取文章分页列表
public static function getListPageByCategory($categoryId, $per = 20, $keyword = '')
{
$where = [
['category_id', '=', $categoryId],
['status', '=', 1],
];
$param['category_id'] = $categoryId;
if ($keyword != '') {
$where[] = ['title', 'like', '%' . $keyword . '%'];
$param['keyword'] = $keyword;
}
$paginate = [
'list_rows' => $per,
'query' => $param
];
return self::where($where)
->order("sort desc")
->paginate($paginate, false);
}
//根据栏目ID获取文章分页列表
public static function getListCount($categoryId)
{
$where = [
['category_id', '=', $categoryId],
['status', '=', 1],
];
$param['category_id'] = $categoryId;
return self::where($where)
->order("sort desc")
->count();
}
public static function onAfterInsert($article)
{
$article->sort = $article->id;
$article->create_time = $article->update_time = time();
$auth = session('auth');
$article->created = $article->updated = $auth['userName'];
$article->save();
}
/**
* 获取文章列表
* @param $categoryId 分类ID
* @param int $per 每页数量
* @param string $keyword 关键词
* @param array $param 文章类型:置顶、热门、推荐 ['top','hot','recommend']
* @param int $status 文章状态,-1表示不限制
* @return \think\Paginator
* @throws \think\db\exception\DbException
*/
public static function getList($categoryId, $per = 20, $keyword = '', $param = [], $recommend_flag = '')
{
$whereMap = [];
$pageParam = [];
if (is_numeric($categoryId) && $categoryId > 0) {
$whereMap[] = ['category_id', '=', $categoryId];
$pageParam['category_id'] = $categoryId;
}
if (!empty($keyword)) {
$whereMap[] = ['title', 'like', '%' . $keyword . '%'];
$pageParam['keyword'] = $keyword;
}
if (!empty($recommend_flag)) {
$whereMap[] = ['recommend_flag', '=', $recommend_flag];
$pageParam['recommend_flag'] = $recommend_flag;
}
if (is_array($param) && count($param) > 0) {
$pageParam['param'] = $param;
foreach ($param as $vo) {
if (in_array($vo, ['top', 'hot', 'recommend'], true)) {
$whereMap[] = ["{$vo}", '=', 1];
}
}
}
$paginate = [
'list_rows' => $per,
'query' => $pageParam
];
return self::when(count($whereMap) > 0, function ($query) use ($whereMap) {
$query->where($whereMap);
})
->order("sort desc")
->paginate($paginate, false);
}
//获取文章涉及到的图片
public static function getFilesInUse()
{
$items = self::select()->toArray();
$data = [];
foreach ($items as $item) {
$src = trim($item['src']);
if (!empty($src)) {
$key = getKeyByPath($src);
$data[$key] = $src;
}
$imgs = getImageUrlFromText($item['content']);
if (!empty($imgs)) {
$data = array_merge($data, $imgs);
}
$videos = getVideoUrlFromText($item['content']);
if (!empty($videos)) {
$data = array_merge($data, $videos);
}
}
return $data;
}
//获取文章
public static function getFinancingArticle($category_id = 0, $recommend_flag = '', $limit = 2)
{
$where = [
["category_id", "=", $category_id],
["hot", "=", 1],
["status", "=", "1"],
];
if (!empty($recommend_flag)) {
$where[] = ["recommend_flag", "=", $recommend_flag];
}
return self::where($where)->limit($limit)->order("sort desc")->select()->toArray();
}
public static function getTopArticle($categoryId, $limit = 1)
{
$where = [
["top", "=", 1],
["status", "=", "1"],
];
if (is_array($categoryId)) {
$where[] = ["category_id", "in", $categoryId];
} else {
$where[] = ["category_id", "=", $categoryId];
}
return self::where($where)->limit($limit)->order("sort desc")->select()->toArray();
}
public function setImgsAttr($value)
{
return json_encode($value);
}
// ----------页面函数
}