2020-01-01 13:17:19 +08:00
|
|
|
<?php
|
|
|
|
namespace app\index\controller;
|
|
|
|
|
|
|
|
use app\common\controller\BaseController;
|
|
|
|
use think\facade\View;
|
|
|
|
use think\facade\Request;
|
|
|
|
use think\facade\Db;
|
2020-03-11 11:33:17 +08:00
|
|
|
use think\facade\Cache;
|
2020-04-29 18:03:15 +08:00
|
|
|
use think\facade\Config;
|
2020-01-01 13:17:19 +08:00
|
|
|
use app\common\model\Comment;
|
|
|
|
use app\common\model\Article as ArticleModel;
|
|
|
|
use think\exception\ValidateException;
|
2020-03-29 19:57:18 +08:00
|
|
|
use taoler\com\Message;
|
2020-11-25 15:29:48 +08:00
|
|
|
use app\common\lib\Msgres;
|
2020-03-28 21:41:07 +08:00
|
|
|
|
2020-01-01 13:17:19 +08:00
|
|
|
class Article extends BaseController
|
|
|
|
{
|
|
|
|
protected $middleware = [
|
|
|
|
'logincheck' => ['except' => ['cate','detail'] ],
|
|
|
|
];
|
|
|
|
|
|
|
|
//文章分类
|
|
|
|
public function cate(){
|
|
|
|
|
|
|
|
//获取分类ID
|
|
|
|
$ename = Request::param('ename');
|
2020-11-27 16:25:08 +08:00
|
|
|
$type = Request::param('type') ?? 'all';
|
|
|
|
|
2020-04-29 18:03:15 +08:00
|
|
|
//分页伪静态
|
|
|
|
$str = Request::baseUrl(); //不带参数在url
|
|
|
|
$patterns = "/\d+/"; //数字正则
|
|
|
|
preg_match($patterns,$str,$arr); //正则查询页码出现在位置
|
|
|
|
//检测route配置中是否设置了伪静态后缀
|
|
|
|
$suffix = Config::get('route.url_html_suffix') ? '.'.Config::get('route.url_html_suffix') : '/';
|
|
|
|
if(Config::get('route.url_html_suffix')){
|
|
|
|
//伪静态有后缀
|
|
|
|
if(isset($arr[0])){
|
|
|
|
$page = $arr[0];
|
|
|
|
$url = strstr($str,$arr[0],true);
|
|
|
|
} else {
|
|
|
|
$page = 1;
|
|
|
|
$url = strstr($str,'.html',true).'/';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//伪静态后缀false
|
|
|
|
if(isset($arr[0])){
|
|
|
|
$page = $arr[0];
|
|
|
|
$url = strstr($str,$arr[0],true);
|
|
|
|
} else {
|
|
|
|
$page = 1;
|
|
|
|
$url = $str.'/';
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 16:25:08 +08:00
|
|
|
//分类列表
|
|
|
|
$article = new ArticleModel();
|
|
|
|
$artList = $article->getCateList($ename,$type,$page,$url,$suffix);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
2020-03-13 22:59:38 +08:00
|
|
|
|
2020-01-01 13:17:19 +08:00
|
|
|
// 热议文章
|
2020-11-27 16:25:08 +08:00
|
|
|
$artHot = $article->getArtHot(10);
|
2020-01-01 13:17:19 +08:00
|
|
|
//分类右栏广告
|
2020-01-02 16:23:03 +08:00
|
|
|
$ad_cate = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',5)->whereTime('slid_over','>=',time())->select();
|
2020-01-01 13:17:19 +08:00
|
|
|
//通用右栏
|
2020-01-02 16:23:03 +08:00
|
|
|
$ad_comm = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',2)->whereTime('slid_over','>=',time())->select();
|
2020-01-01 13:17:19 +08:00
|
|
|
|
2020-03-13 22:59:38 +08:00
|
|
|
View::assign(['type'=>$type,'artList'=>$artList,'artHot'=>$artHot,'ad_cate'=>$ad_cate,'ad_comm'=>$ad_comm]);
|
2020-01-01 13:17:19 +08:00
|
|
|
return View::fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
//文章详情页
|
2020-02-12 15:08:52 +08:00
|
|
|
public function detail($id)
|
2020-01-01 13:17:19 +08:00
|
|
|
{
|
2020-11-25 15:29:48 +08:00
|
|
|
$article = new ArticleModel();
|
|
|
|
$artDetail = $article->getArtDetail($id);
|
|
|
|
if(!$artDetail){
|
2020-06-19 16:30:27 +08:00
|
|
|
// 抛出 HTTP 异常
|
|
|
|
throw new \think\exception\HttpException(404, '异常消息');
|
|
|
|
}
|
2020-11-25 15:29:48 +08:00
|
|
|
$comments = $artDetail->comments()->where('status',1)->order(['cai'=>'asc','create_time'=>'asc'])->paginate(10);
|
|
|
|
//$comment = new \app\common\model\Comment();
|
|
|
|
//$comments = $comment->getComment($id);
|
|
|
|
//dump($comments);
|
|
|
|
$artDetail->inc('pv')->update();
|
2020-03-13 22:59:38 +08:00
|
|
|
$pv = Db::name('article')->field('pv')->where('id',$id)->value('pv');
|
2020-11-25 15:29:48 +08:00
|
|
|
$download = $artDetail->upzip ? download($artDetail->upzip,'file') : '';
|
2020-01-01 13:17:19 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
$nt = time();
|
|
|
|
$ft = $article->comments;
|
|
|
|
$ct[] = [];
|
|
|
|
foreach($ft as $c){
|
|
|
|
$t = $c->create_time;
|
|
|
|
$ct[] = intval(($nt - strtotime($t))/86400);
|
|
|
|
}
|
|
|
|
dump($nt);
|
|
|
|
dump($ct);
|
|
|
|
$this->assign('ct',$ct);
|
|
|
|
$article->append(['comment.ct'])->toArray();
|
|
|
|
//halt($article);
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 热议文章
|
2020-11-25 15:29:48 +08:00
|
|
|
$artHot = $article->getArtHot(10);
|
2020-01-01 13:17:19 +08:00
|
|
|
//文章广告
|
2020-01-02 16:23:03 +08:00
|
|
|
$ad_article = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',4)->whereTime('slid_over','>=',time())->select();
|
2020-01-01 13:17:19 +08:00
|
|
|
//通用右栏
|
2020-01-02 16:23:03 +08:00
|
|
|
$ad_comm = Db::name('slider')->where('slid_status',1)->where('delete_time',0)->where('slid_type',2)->whereTime('slid_over','>=',time())->select();
|
2020-02-19 14:59:09 +08:00
|
|
|
|
2020-11-25 15:29:48 +08:00
|
|
|
View::assign(['article'=>$artDetail,'pv'=>$pv,'comments'=>$comments,'artHot'=>$artHot,'ad_art'=>$ad_article,'ad_comm'=>$ad_comm,$download]);
|
2020-01-01 13:17:19 +08:00
|
|
|
return View::fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
//文章评论
|
|
|
|
public function comment()
|
|
|
|
{
|
2020-11-30 17:13:55 +08:00
|
|
|
if (Request::isAjax()){
|
|
|
|
//获取评论
|
|
|
|
$data = Request::only(['content','article_id','user_id']);
|
|
|
|
$sendId = $data['user_id'];
|
|
|
|
if(empty($data['content'])){
|
|
|
|
return json(['code'=>0, 'msg'=>'评论不能为空!']);
|
|
|
|
}
|
|
|
|
//用户留言存入数据库
|
|
|
|
if (Comment::create($data)) {
|
|
|
|
//站内信
|
|
|
|
$article = Db::name('article')->field('id,title,user_id')->where('id',$data['article_id'])->find();
|
|
|
|
$title = $article['title'];
|
|
|
|
$link = (string) url('article/detail',['id'=>$data['article_id']]);
|
|
|
|
|
|
|
|
//评论中回复@user comment
|
|
|
|
$preg = "/@([^@\s]*)\s/";
|
|
|
|
preg_match($preg,$data['content'],$username);
|
|
|
|
if(isset($username[1])){
|
|
|
|
$receveId = Db::name('user')->whereOr('nickname', $username[1])->whereOr('name', $username[1])->value('id');
|
|
|
|
} else {
|
|
|
|
$receveId = $article['user_id'];
|
|
|
|
}
|
|
|
|
$data = ['title'=>$title,'content'=>'评论通知','link'=>$link,'user_id'=>$sendId,'type'=>2]; //type=2为评论留言
|
|
|
|
Message::sendMsg($sendId,$receveId,$data);
|
2020-04-30 18:35:46 +08:00
|
|
|
|
2020-11-30 17:13:55 +08:00
|
|
|
$res = ['code'=>0, 'msg'=>'留言成功'];
|
2020-03-28 21:41:07 +08:00
|
|
|
} else {
|
2020-11-30 17:13:55 +08:00
|
|
|
$res = ['code'=>-1, 'msg'=>'留言失败'];
|
2020-03-28 21:41:07 +08:00
|
|
|
}
|
2020-11-30 17:13:55 +08:00
|
|
|
return json($res);
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 17:03:25 +08:00
|
|
|
/**
|
|
|
|
* 添加帖子文章
|
|
|
|
* @return string|\think\Response|\think\response\Json|void
|
|
|
|
*/
|
2020-01-01 13:17:19 +08:00
|
|
|
public function add()
|
2020-11-23 17:03:25 +08:00
|
|
|
{
|
|
|
|
if (Request::isAjax()) {
|
|
|
|
$data = Request::only(['cate_id', 'title', 'title_color', 'user_id', 'content', 'upzip', 'tags', 'captcha']);
|
|
|
|
$validate = new \app\common\validate\Article; //调用验证器
|
|
|
|
$result = $validate->scene('Artadd')->check($data); //进行数据验证
|
|
|
|
if (true !== $result) {
|
2020-11-25 15:29:48 +08:00
|
|
|
return Msgres::error($validate->getError());
|
2020-11-23 17:03:25 +08:00
|
|
|
}
|
2020-11-25 15:29:48 +08:00
|
|
|
$article = new ArticleModel();
|
|
|
|
$result = $article->add($data);
|
2020-11-23 17:03:25 +08:00
|
|
|
if ($result == 1) {
|
|
|
|
$aid = Db::name('article')->max('id');
|
|
|
|
$link = (string)url('article/detail', ['id' => $aid]);
|
|
|
|
//清除文章tag缓存
|
|
|
|
Cache::tag('tagArtDetail')->clear();
|
2020-11-25 15:29:48 +08:00
|
|
|
$res = Msgres::success('add_success', $link);
|
2020-11-23 17:03:25 +08:00
|
|
|
} else {
|
2020-11-25 15:29:48 +08:00
|
|
|
$res = Msgres::error('add_error');
|
2020-11-23 17:03:25 +08:00
|
|
|
}
|
|
|
|
return $res;
|
2020-06-19 16:30:27 +08:00
|
|
|
}
|
2020-11-23 17:03:25 +08:00
|
|
|
return View::fetch();
|
2020-06-19 16:30:27 +08:00
|
|
|
}
|
2020-01-01 13:17:19 +08:00
|
|
|
|
2020-11-23 17:03:25 +08:00
|
|
|
/**
|
|
|
|
* 编辑文章
|
|
|
|
* @param $id
|
|
|
|
* @return string|\think\Response|\think\response\Json|void
|
|
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
|
|
* @throws \think\db\exception\DbException
|
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
|
*/
|
2020-02-19 14:59:09 +08:00
|
|
|
public function edit($id)
|
2020-01-01 13:17:19 +08:00
|
|
|
{
|
2020-11-23 17:03:25 +08:00
|
|
|
$article = ArticleModel::find($id);
|
|
|
|
//编辑
|
2020-01-01 13:17:19 +08:00
|
|
|
if(Request::isAjax()){
|
2020-04-13 15:51:24 +08:00
|
|
|
$data = Request::only(['id','cate_id','title','title_color','user_id','content','upzip','tags','captcha']);
|
2020-01-01 13:17:19 +08:00
|
|
|
$validate = new \app\common\validate\Article(); //调用验证器
|
|
|
|
$res = $validate->scene('Artadd')->check($data); //进行数据验证
|
|
|
|
|
2020-02-25 15:40:06 +08:00
|
|
|
if(true !==$res){
|
2020-11-25 15:29:48 +08:00
|
|
|
return Msgres::error($validate->getError());
|
2020-03-15 22:19:10 +08:00
|
|
|
} else {
|
2020-01-01 13:17:19 +08:00
|
|
|
$result = $article->edit($data);
|
|
|
|
if($result == 1) {
|
2020-11-23 17:03:25 +08:00
|
|
|
//删除原有缓存显示编辑后内容
|
2020-04-24 15:07:26 +08:00
|
|
|
Cache::delete('article_'.$id);
|
|
|
|
$link = (string) url('article/detail',['id'=> $id]);
|
2020-11-25 15:29:48 +08:00
|
|
|
$editRes = Msgres::success('edit_success',$link);
|
2020-01-01 13:17:19 +08:00
|
|
|
} else {
|
2020-11-25 15:29:48 +08:00
|
|
|
$editRes = Msgres::error($result);
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
2020-11-23 17:03:25 +08:00
|
|
|
return $editRes;
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-23 17:03:25 +08:00
|
|
|
//查询标签
|
|
|
|
$tag = $article->tags;
|
2020-02-19 14:59:09 +08:00
|
|
|
$attr = explode(',',$tag);
|
|
|
|
$tags = [];
|
|
|
|
foreach($attr as $key=>$v){
|
|
|
|
if ($v !='') {
|
|
|
|
$tags[] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
View::assign(['article'=>$article,'tags'=>$tags]);
|
2020-01-01 13:17:19 +08:00
|
|
|
return View::fetch();
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:07:26 +08:00
|
|
|
//删除帖子
|
2020-01-01 13:17:19 +08:00
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
$article = ArticleModel::find(input('id'));
|
|
|
|
$result = $article->together(['comments'])->delete();
|
|
|
|
if($result) {
|
2020-11-27 16:25:08 +08:00
|
|
|
$res = Msgres::success('delete_success');
|
2020-01-01 13:17:19 +08:00
|
|
|
} else {
|
2020-11-27 16:25:08 +08:00
|
|
|
$res = Msgres::error('delete_error');
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
2020-11-27 16:25:08 +08:00
|
|
|
return $res;
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//文本编辑器图片上传
|
2020-04-24 15:07:26 +08:00
|
|
|
public function textImgUpload()
|
2020-01-01 13:17:19 +08:00
|
|
|
{
|
|
|
|
$file = request()->file('file');
|
|
|
|
try {
|
2020-03-18 21:30:30 +08:00
|
|
|
validate(['file'=>'fileSize:1024000|fileExt:jpg,png,gif'])
|
2020-02-25 15:40:06 +08:00
|
|
|
->check(['file'=>$file]);
|
2020-01-01 13:17:19 +08:00
|
|
|
$savename = \think\facade\Filesystem::disk('public')->putFile('article_pic',$file);
|
2020-01-11 17:27:15 +08:00
|
|
|
} catch (ValidateException $e) {
|
2020-03-18 21:30:30 +08:00
|
|
|
return json(['status'=>-1,'msg'=>$e->getMessage()]);
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
|
|
|
$upload = Config::get('filesystem.disks.public.url');
|
|
|
|
|
|
|
|
if($savename){
|
|
|
|
//$name = $file->hashName();
|
|
|
|
$name_path =str_replace('\\',"/",$upload.'/'.$savename);
|
|
|
|
//halt($name_path);
|
|
|
|
//$image = \think\Image::open("uploads/$name_path");
|
|
|
|
//$image->thumb(168, 168)->save("uploads/$name_path");
|
|
|
|
|
|
|
|
$res = ['status'=>0,'msg'=>'上传成功','url'=> $name_path];
|
|
|
|
}else{
|
2020-02-25 12:22:00 +08:00
|
|
|
$res = ['status'=>-1,'msg'=>'上传错误'];
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
|
|
|
return json($res);
|
|
|
|
}
|
|
|
|
|
2020-11-23 17:03:25 +08:00
|
|
|
//上传附件
|
|
|
|
public function upzip()
|
|
|
|
{
|
|
|
|
$file = request()->file('file');
|
|
|
|
try {
|
|
|
|
validate(['file'=>'fileSize:1024000|fileExt:jpg,zip'])
|
|
|
|
->check(['file'=>$file]);
|
|
|
|
$savename = \think\facade\Filesystem::disk('public')->putFile('article_zip',$file);
|
|
|
|
} catch (ValidateException $e) {
|
|
|
|
return json(['status'=>-1,'msg'=>$e->getMessage()]);
|
|
|
|
}
|
|
|
|
$upload = Config::get('filesystem.disks.public.url');
|
|
|
|
|
|
|
|
if($savename){
|
|
|
|
$name_path =str_replace('\\',"/",$upload.'/'.$savename);
|
|
|
|
$res = ['status'=>0,'msg'=>'上传成功','url'=> $name_path];
|
|
|
|
}else{
|
|
|
|
$res = ['status'=>-1,'msg'=>'上传错误'];
|
|
|
|
}
|
|
|
|
return json($res);
|
|
|
|
}
|
|
|
|
|
|
|
|
//附件下载
|
|
|
|
public function download($id)
|
|
|
|
{
|
|
|
|
$zipdir = Db::name('article')->where('id',$id)->value('upzip');
|
|
|
|
$zip = substr($zipdir,1);
|
|
|
|
return download($zip,'my');
|
|
|
|
}
|
|
|
|
|
|
|
|
//添加tag
|
|
|
|
public function tags()
|
|
|
|
{
|
|
|
|
$data = Request::only(['tags']);
|
|
|
|
$att = explode(',',$data['tags']);
|
|
|
|
$tags = [];
|
|
|
|
foreach($att as $v){
|
|
|
|
if ($v !='') {
|
|
|
|
$tags[] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return json(['code'=>0,'data'=>$tags]);
|
|
|
|
}
|
2020-01-01 13:17:19 +08:00
|
|
|
|
|
|
|
//文章置顶,状态
|
|
|
|
public function jieset(){
|
|
|
|
$data = Request::param();
|
2020-02-25 15:40:06 +08:00
|
|
|
$article = ArticleModel::field('id,is_top,is_hot')->find($data['id']);
|
2020-01-01 13:17:19 +08:00
|
|
|
if($data['field'] === 'top') {
|
|
|
|
if($data['rank']==1){
|
|
|
|
$article->save(['is_top' => 1]);
|
2020-02-25 15:40:06 +08:00
|
|
|
$res = ['status'=>0,'msg'=>'置顶成功'];
|
2020-01-01 13:17:19 +08:00
|
|
|
} else {
|
|
|
|
$article->save(['is_top' => 0]);
|
2020-02-25 15:40:06 +08:00
|
|
|
$res = ['status'=>0,'msg'=>'已取消置顶'];
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if($data['rank']==1){
|
|
|
|
$article->save(['is_hot' => 1]);
|
2020-02-25 15:40:06 +08:00
|
|
|
$res = ['status'=>0,'msg'=>'已设精贴'];
|
2020-01-01 13:17:19 +08:00
|
|
|
} else {
|
|
|
|
$article->save(['is_hot' => 0]);
|
2020-02-25 15:40:06 +08:00
|
|
|
$res = ['status'=>0,'msg'=>'精贴已取消'];
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-24 15:07:26 +08:00
|
|
|
|
|
|
|
//删除本贴设置缓存显示编辑后内容
|
|
|
|
Cache::delete('article_'.$data['id']);
|
|
|
|
//清除文章tag缓存
|
2020-05-13 17:55:29 +08:00
|
|
|
Cache::tag('tagArtDetail')->clear();
|
2020-01-01 13:17:19 +08:00
|
|
|
return json($res);
|
|
|
|
}
|
|
|
|
|
2020-04-19 14:29:34 +08:00
|
|
|
//改变标题颜色
|
|
|
|
public function titleColor()
|
|
|
|
{
|
|
|
|
$data = Request::param();
|
|
|
|
$result = ArticleModel::update($data);
|
|
|
|
if($result){
|
|
|
|
//清除文章缓存
|
|
|
|
Cache::tag(['tagArt','tagArtDetail'])->clear();
|
|
|
|
$res = ['code'=> 0, 'msg'=>'标题颜色设置成功'];
|
|
|
|
}else{
|
|
|
|
$res = ['code'=> -1, 'msg'=>'标题颜色设置失败'];
|
|
|
|
}
|
|
|
|
return json($res);
|
|
|
|
}
|
|
|
|
|
2020-01-01 13:17:19 +08:00
|
|
|
}
|