TaoLer/app/common/model/Article.php

507 lines
15 KiB
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
declare (strict_types = 1);
2020-01-01 13:17:19 +08:00
namespace app\common\model;
use think\Model;
use think\model\concern\SoftDelete;
2020-11-25 15:29:48 +08:00
use think\facade\Cache;
use think\facade\Config;
2024-04-14 12:06:40 +08:00
use think\db\Query;
2020-01-01 13:17:19 +08:00
class Article extends Model
{
protected $autoWriteTimestamp = true; //开启自动时间戳
protected $createTime = 'create_time';
protected $updateTime = 'update_time';
//开启自动设置
protected $auto = [];
//仅新增有效
protected $insert = ['create_time','status'=>1,'is_top'=>0,'is_hot'=>0];
//仅更新有效
protected $update = ['update_time'];
//软删除
use SoftDelete;
protected $deleteTime = 'delete_time';
protected $defaultSoftDelete = 0;
//文章关联栏目表
public function cate()
{
2022-09-23 11:03:10 +08:00
return $this->belongsTo(Cate::class);
2020-01-01 13:17:19 +08:00
}
//文章关联评论
public function comments()
{
2020-02-12 15:08:52 +08:00
return $this->hasMany(Comment::class);
2020-01-01 13:17:19 +08:00
}
2021-05-24 15:05:30 +08:00
//文章关联收藏
public function collection()
{
return $this->hasMany(Collection::class);
}
2022-08-02 21:13:36 +08:00
//文章关联用户点赞
public function userzan()
{
return $this->hasMany(UserZan::class);
}
2021-05-24 15:05:30 +08:00
2020-01-01 13:17:19 +08:00
//文章关联用户
public function user()
{
return $this->belongsTo('User','user_id','id');
}
2022-08-16 15:29:04 +08:00
//文章关联Tag表
public function taglist()
{
return $this->hasMany(Taglist::class);
}
/**
* 添加
* @param array $data
2023-03-16 22:54:11 +08:00
* @return int|string|array
*/
public function add(array $data)
2020-01-01 13:17:19 +08:00
{
2023-03-16 22:35:59 +08:00
$superAdmin = User::where('id', $data['user_id'])->value('auth');
2022-08-02 21:13:36 +08:00
// 超级管理员无需审核
2021-10-12 16:46:49 +08:00
$data['status'] = $superAdmin ? 1 : Config::get('taoler.config.posts_check');
$msg = $data['status'] ? '发布成功' : '发布成功,请等待审核';
2020-01-01 13:17:19 +08:00
$result = $this->save($data);
2023-05-05 11:57:54 +08:00
if($result) {
2022-08-16 15:29:04 +08:00
return ['code' => 1, 'msg' => $msg, 'data' => ['status' => $data['status'], 'id'=> $this->id]];
2020-01-01 13:17:19 +08:00
} else {
2022-08-16 15:29:04 +08:00
return ['code' => -1, 'msg'=> '添加文章失败'];
2020-01-01 13:17:19 +08:00
}
}
/**
* 编辑
* @param array $data
* @return int|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function edit(array $data)
2020-01-01 13:17:19 +08:00
{
2022-08-02 21:13:36 +08:00
$article = $this::find($data['id']);
2020-01-01 13:17:19 +08:00
$result = $article->save($data);
if($result) {
return 1;
} else {
2020-11-23 17:03:25 +08:00
return 'edit_error';
2020-01-01 13:17:19 +08:00
}
}
2020-11-25 15:29:48 +08:00
/**
* 获取置顶文章
* @param int $num 列表数量
2020-11-25 15:29:48 +08:00
* @return mixed|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getArtTop(int $num)
2020-11-25 15:29:48 +08:00
{
2023-05-05 11:58:42 +08:00
return Cache::remember('topArticle', function() use($num){
2024-04-14 12:08:53 +08:00
$topIdArr = $this::where(['is_top' => 1, 'status' => 1])->limit($num)->column('id');
2024-04-14 12:06:40 +08:00
return $this::field('id,title,title_color,cate_id,user_id,create_time,is_top,pv,upzip,has_img,has_video,has_audio,read_type')
2024-04-14 12:08:53 +08:00
->where('id', 'in', $topIdArr)
2023-05-05 11:58:42 +08:00
->with([
2024-04-14 12:06:40 +08:00
'cate' => function (Query $query) {
2024-04-14 12:10:25 +08:00
$query->field('id,catename,ename');
2023-05-05 11:58:42 +08:00
},
2024-04-14 12:06:40 +08:00
'user' => function (Query $query) {
2023-05-05 11:58:42 +08:00
$query->field('id,name,nickname,user_img');
}
])->withCount(['comments'])
2024-04-14 12:08:53 +08:00
->order('id', 'desc')
2023-05-05 11:58:42 +08:00
->append(['url'])
->select()
->toArray();
},60);
2020-11-25 15:29:48 +08:00
}
/**
* 获取首页文章列表
* @param int $num 列表显示数量
2020-11-25 15:29:48 +08:00
* @return mixed|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getArtList(int $num)
2020-11-25 15:29:48 +08:00
{
2023-05-05 11:58:42 +08:00
return Cache::remember('indexArticle', function() use($num){
2024-04-14 12:06:40 +08:00
$data = $this::field('id,title,title_color,cate_id,user_id,content,create_time,is_hot,pv,jie,upzip,has_img,has_video,has_audio,read_type,art_pass')
2020-11-25 15:29:48 +08:00
->with([
2024-04-14 12:06:40 +08:00
'cate' => function(Query $query){
2024-04-14 12:10:25 +08:00
$query->field('id,catename,ename,detpl');
2020-11-25 15:29:48 +08:00
},
2024-04-14 12:06:40 +08:00
'user' => function(Query $query){
2023-05-05 11:58:42 +08:00
$query->field('id,name,nickname,user_img');
2020-11-25 15:29:48 +08:00
} ])
2022-08-02 21:13:36 +08:00
->withCount(['comments'])
2023-05-05 11:58:42 +08:00
->where('status', '=', 1)
2024-04-14 12:08:53 +08:00
->order('id','desc')
2022-08-02 21:13:36 +08:00
->limit($num)
2024-04-14 12:08:53 +08:00
->hidden(['art_pass'])
2022-08-02 21:13:36 +08:00
->append(['url'])
2024-04-14 12:06:40 +08:00
->select();
return $data->hidden(['art_pass'])->toArray();
2023-05-05 11:58:42 +08:00
},30);
2022-08-02 21:13:36 +08:00
2020-11-25 15:29:48 +08:00
}
/**
* 热点文章
2024-04-14 12:08:53 +08:00
* @param int $num 热点列表数量 评论或者pv排序
* @return \think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getArtHot(int $num)
2020-11-25 15:29:48 +08:00
{
2024-04-14 12:08:53 +08:00
return Cache::remember('article_hot', function() use($num){
$comments = Comment::field('article_id,count(*) as count')
2024-04-14 12:10:25 +08:00
->hasWhere('article',['status' => 1])
2024-04-14 12:08:53 +08:00
->group('article_id')
->order('count','desc')
->limit($num)
->select();
$idArr = [];
foreach($comments as $v) {
$idArr[] = $v->article_id;
}
$where = [];
if(count($idArr)) {
// 评论数
$where = [
['id', 'in', $idArr]
];
$artHot = $this::field('id,cate_id,title,create_time')
->withCount('comments')
->where($where)
//->whereYear('create_time')
// ->order('comments_count','desc')
->append(['url'])
->select();
} else {
// pv数
$where = [
['status', '=', 1]
];
$artHot = $this::field('id,cate_id,title,create_time')
->withCount('comments')
->where($where)
->whereMonth('create_time')
->order('pv','desc')
->limit($num)
->append(['url'])
->select();
}
return $artHot;
}, 3600);
2020-11-25 15:29:48 +08:00
}
/**
* 获取详情
* @param int $id 文章id
2023-05-05 11:58:42 +08:00
* @return mixed
* @throws \Throwable
*/
2022-08-02 21:13:36 +08:00
public function getArtDetail(int $id)
2020-11-25 15:29:48 +08:00
{
2023-05-05 11:58:42 +08:00
return Cache::remember('article_'.$id, function() use($id){
2020-11-25 15:29:48 +08:00
//查询文章
2023-05-05 12:03:45 +08:00
return $this::field('id,title,content,status,cate_id,user_id,goods_detail_id,is_top,is_hot,is_reply,pv,jie,upzip,downloads,keywords,description,read_type,art_pass,title_color,create_time,update_time')
2022-08-16 15:29:04 +08:00
->where(['status'=>1])
->with([
2024-04-14 12:06:40 +08:00
'cate' => function(Query $query){
2024-04-14 12:10:25 +08:00
$query->field('id,catename,ename');
2020-11-25 15:29:48 +08:00
},
2024-04-14 12:06:40 +08:00
'user' => function(Query $query){
2022-08-02 21:13:36 +08:00
$query->field('id,name,nickname,user_img,area_id,vip,city')->withCount(['article','comments']);
2020-11-25 15:29:48 +08:00
}
2022-08-02 21:13:36 +08:00
])
->withCount(['comments'])
2024-04-14 12:06:40 +08:00
->hidden(['art_pass'])
2022-08-02 21:13:36 +08:00
->append(['url'])
2022-08-16 15:29:04 +08:00
->find($id);
2023-05-05 11:58:42 +08:00
}, 600);
2020-11-25 15:29:48 +08:00
}
/**
2023-05-05 11:58:42 +08:00
* 分类数据
* @param string $ename
* @param string $type all\top\hot\jie 分类类型
2023-05-05 11:58:42 +08:00
* @param int $page
* @return mixed
* @throws \Throwable
*/
2022-01-07 14:45:35 +08:00
public function getCateList(string $ename, string $type, int $page = 1)
{
2023-05-05 11:58:42 +08:00
switch ($type) {
//查询文章,15个分1页
case 'jie':
$where[] = ['jie','=', 1];
break;
case 'hot':
$where[] = ['is_hot','=', 1];
break;
case 'top':
$where[] = ['is_top' ,'=', 1];
break;
case 'wait':
$where[] = ['jie','=', 0];
break;
}
2024-04-14 12:08:53 +08:00
2023-05-05 11:58:42 +08:00
$where[] = ['status', '=', 1];
2024-04-14 12:08:53 +08:00
return Cache::remember('cate_list_'.$ename.$type.$page, function() use($where,$ename,$page){
$cateId = Cate::where('ename',$ename)->value('id');
if($cateId){
$where[] = ['cate_id' ,'=', $cateId];
}
$list = $this::field('id')->where($where)->order(['id'=>'desc'])->paginate([
'list_rows' => 15,
'page' => $page
])->toArray();
$idArr = [];
if($list['total'] > 0) {
foreach($list['data'] as $v) {
$idArr[] = $v['id'];
}
}
$data = $this::field('id,cate_id,user_id,title,content,title_color,create_time,is_top,is_hot,pv,jie,upzip,has_img,has_video,has_audio,read_type,art_pass')
2022-08-02 21:13:36 +08:00
->with([
'cate' => function($query) {
2023-05-05 11:58:42 +08:00
$query->field('id,catename,ename');
2022-08-02 21:13:36 +08:00
},
'user' => function($query){
2023-05-05 11:58:42 +08:00
$query->field('id,name,nickname,user_img,vip');
2022-08-02 21:13:36 +08:00
}
])->withCount(['comments'])
2024-04-14 12:08:53 +08:00
->where('id','in',$idArr)
->order(['id'=>'desc'])
->append(['url'])
->hidden(['art_pass'])
->select()
->toArray();
return [
'total' => $list['total'],
'per_page' => $list['per_page'],
'current_page' => $list['current_page'],
'last_page' => $list['last_page'],
'data' => $data
];
2023-05-05 11:58:42 +08:00
}, 600);
}
2022-08-02 21:13:36 +08:00
// 获取用户发帖列表
public function getUserArtList(int $id) {
$userArtList = $this::field('id,cate_id,title,create_time,pv,is_hot')
->with(['cate' => function($query){
2024-04-14 12:10:25 +08:00
$query->where(['status'=>1])->field('id,ename');
2022-08-02 21:13:36 +08:00
}])
->where(['user_id' => $id,'status' => 1])
2024-04-14 12:08:53 +08:00
->order('id','desc')
2022-08-02 21:13:36 +08:00
->append(['url'])
2024-04-01 10:22:13 +08:00
->limit(25)
2022-08-02 21:13:36 +08:00
->cache(3600)
->select()
->toArray();
return $userArtList;
}
// 获取搜索文章
public function getSearchKeyWord(string $keywords)
{
//全局查询条件
$map = []; //所有的查询条件封装到数组中
//条件1
$map[] = ['status','=',1]; //这里等号不能省略
if(!empty($keywords)){
//条件2
$map[] = ['title','like','%'.$keywords.'%'];
$res = Article::where($map)
->withCount('comments')
2024-04-14 12:08:53 +08:00
->order('id','desc')
2022-08-02 21:13:36 +08:00
->append(['url'])
->paginate(10);
2024-04-01 10:22:13 +08:00
return $res;
2022-08-02 21:13:36 +08:00
}
}
/**
* 标签列表
*
* @param [type] $tagId 标签id
* @param [type] $limit 输出数量
2023-03-16 22:54:11 +08:00
* @return array
*/
2022-08-16 15:29:04 +08:00
public function getAllTags($tagId)
2022-08-02 21:13:36 +08:00
{
2024-04-01 10:24:38 +08:00
$arrId = Taglist::where('tag_id', $tagId)->column('article_id');
$allTags = $this::field('id,user_id,cate_id,title,create_time,is_hot')->where('id','in', $arrId)
2022-08-16 15:29:04 +08:00
->with(['user' => function($query){
$query->field('id,name,nickname,user_img,area_id,vip');
},'cate' => function($query){
2024-04-14 12:10:25 +08:00
$query->field('id,catename,ename');
2022-08-16 15:29:04 +08:00
}])
->where(['status'=>1])
2024-04-01 10:24:38 +08:00
->order('id desc')
->limit(20)
2022-08-16 15:29:04 +08:00
->append(['url'])
->select()
->toArray();
return $allTags;
2022-08-02 21:13:36 +08:00
}
/**
* 相关文章(标签)
* 相同标签文章,不包含自己
* @param [type] $tagId
* @param [type] $limit
* @return void
*/
public function getRelationTags($tagId,$id,$limit)
{
2024-04-01 10:24:38 +08:00
$arrId = Taglist::where([
['tag_id', '=', $tagId],
['article_id','<>',$id]
])->column('article_id');
$allTags = $this::field('id,cate_id,title')->where('id', 'in', $arrId)
->where(['status'=>1])
->order('pv desc')
->limit($limit)
->append(['url'])
->select()
->toArray();
return $allTags;
}
/**
* 上下文
*
* @param [type] $id 当前文章ID
* @param [type] $cid 当前分类ID
2023-03-16 22:54:11 +08:00
* @return void|array
*/
2024-04-14 12:08:53 +08:00
public function getPrevNextArticle($id, $cid)
{
//上一篇
2024-04-14 12:08:53 +08:00
$pIds = $this::where([
['id', '>=', $id], // >= <= 条件可以使用索引
['cate_id', '=', $cid],
['status', '=',1]
2024-04-14 12:08:53 +08:00
])->order('id asc')->limit(2)->column('id');
2024-04-14 12:08:53 +08:00
if(count($pIds) === 2) {
$previous = $this::field('id,title,cate_id')->append(['url'])->find($pIds[1])->toArray();
} else {
$previous = [];
}
//下一篇
2024-04-14 12:08:53 +08:00
$nids = $this::where([
['id', '<=', $id],
['cate_id', '=', $cid],
['status', '=',1]
2024-04-14 12:08:53 +08:00
])->order('id desc')->limit(2)->column('id');
if(count($nids) === 2) {
$next = $this::field('id,title,cate_id')->append(['url'])->find($nids[1])->toArray();
} else {
$next = [];
}
return ['previous' => $previous, 'next' => $next];
}
2023-05-05 11:58:42 +08:00
// 获取所有帖子内容
2023-05-05 12:01:49 +08:00
public function getList(array $where, int $limit, int $page)
2023-05-05 11:58:42 +08:00
{
2023-07-03 12:56:37 +08:00
return $this::field('id,user_id,cate_id,title,content,is_top,is_hot,is_reply,status,update_time,read_type,art_pass')
2023-07-03 12:49:15 +08:00
->with([
2023-05-05 11:58:42 +08:00
'user' => function($query){
$query->field('id,name,user_img');
},
'cate' => function($query){
2023-07-03 12:50:57 +08:00
$query->field('id,ename,catename');
2023-05-05 11:58:42 +08:00
}
2023-07-03 12:49:15 +08:00
])
2023-07-03 12:52:34 +08:00
->where(['status' => 1])
2023-07-03 12:49:15 +08:00
->where($where)
2024-04-14 12:08:53 +08:00
->order('id', 'desc')
2023-05-05 12:01:49 +08:00
->paginate([
2023-05-05 11:58:42 +08:00
'list_rows' => $limit,
'page' => $page
])->toArray();
}
2024-04-01 10:08:12 +08:00
// 获取admin应用所有帖子状态内容
public function getAllStatusList(array $where, int $limit, int $page)
{
return $this::field('id,user_id,cate_id,title,content,is_top,is_hot,is_reply,status,update_time,read_type,art_pass')
->with([
'user' => function($query){
$query->field('id,name,user_img');
},
'cate' => function($query){
$query->field('id,ename,catename');
}
])
->where($where)
2024-04-14 12:08:53 +08:00
->order('id', 'desc')
2024-04-01 10:08:12 +08:00
->paginate([
'list_rows' => $limit,
'page' => $page
])->toArray();
}
2022-08-02 21:13:36 +08:00
// 获取url
public function getUrlAttr($value,$data)
{
if(config('taoler.url_rewrite.article_as') == '<ename>/') {
2023-05-05 11:58:42 +08:00
//$cate = Cate::field('id,ename')->find($data['cate_id']);
return (string) url('article_detail',['id' => $data['id'],'ename' => $this->cate->ename]);
2022-08-02 21:13:36 +08:00
}
2023-05-05 11:58:42 +08:00
return (string) url('article_detail',['id' => $data['id']]);
2022-08-02 21:13:36 +08:00
}
2023-07-03 12:56:37 +08:00
// 内容是否加密
public function getContentAttr($value, $data)
{
//解密
if($data['read_type'] == 1 && (session('art_pass_'.$data['id']) !== $data['art_pass'])) {
return '内容已加密!请输入正确密码查看!';
}
return $value;
}
2020-01-01 13:17:19 +08:00
}