|
<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use app\admin\model\Category as CategoryModel;
use app\admin\model\Article as ArticleModel;
use app\index\model\Tags as TagsModel;
/**
* 文章管理
* @author huwhis@163.com
* @version 0.0.1
*/
class Article extends Base
{
protected $model;
protected $category_model;
public function __construct(Request $request = null)
{
parent::__construct($request);
$this->model = new ArticleModel();
$this->category_model = new CategoryModel();
$this->assign('timeList', $this->model->createTimeArchive());
}
public function index($cid = 0)
{
if (!$cid) {
throw new \think\exception\HttpException(404, '页面不存在');
}
$category_info = $this->category_model->field('id, pid, name, url, template')->find($cid);
$data = $this->model->where(['is_del'=>0, 'cid'=>$cid])->field('id, cid, title, username, title_pic, summary, clicks, create_time')->order('id', 'desc')->paginate();
$this->assign('category_info', $category_info);
$this->assign('data', $data);
$this->assign('cid', $cid);
return $this->fetch($category_info->template);
}
public function detail($cid = 0, $id = 0)
{
if (!$cid || !$id) {
throw new \think\exception\HttpException(404, '页面不存在');
}
$category_info = $this->category_model->field('id, pid, name, url, template')->where('is_del', 0)->find($cid);
$this->assign('category_info', $category_info);
$data = $this->model->field('id, cid, title, clicks, title_pic, keywords, summary, content, create_time')->find($id);
$data['clicks'] += 1;
@$data->save(['clicks'=>$data['clicks']], ['id'=>$id]);
$this->assign('data', $data);
// $next_prev = $this->getNextPrev($id, $cid);
// $this->assign('next_prev', $next_prev);
$hot = $this->hot($cid);
$this->assign('hot', $hot);
return $this->fetch();
}
/**
* 获取文章上 or 下一篇
*/
public function getNextPrev($id, $cid = null)
{
$data_p = null;
$data_N = null;
if ($cid) {
$id_list = $this->model->where(['is_del' => 0, 'cid' => $cid])
->order(['sort' => 'desc', 'id' => 'desc'])
->column('id');
if (count($id_list) > 1) {
$key = array_search($id, $id_list);
if ($key == 0) {
$data_p = null;
$N = $id_list[1];
$data_N = $this->model->field('id,title')->find($N);
} elseif ($key == count($id_list) - 1) {
$P = $id_list[count($id_list) - 2];
$data_p = $this->model->field('id,title')->find($P);
$data_N = null;
} else {
$P = $id_list[$key - 1];
$data_p = $this->model->field('id,title')->find($P);
$N = $id_list[$key + 1];
$data_N = $this->model->field('id,title')->find($N);
}
}
}
return ['prev' => $data_p, 'next' => $data_N];
}
/**
* 感兴趣文章
*/
public function hot($cid = 0)
{
$data = null;
if ($cid) {
$data = $this->model->where(['is_del' => 0, 'cid' => $cid])->field('id,cid,title,title_pic,create_time')->order('clicks','desc')->limit(3)->select();
}
return $data;
}
/**
* 归档页面(时间)
*/
public function archive($year=0,$month=0)
{
$time = mFristAndLast($year, $month);
$data = Db::table('sur_article')->alias('a')->join('sur_category c','a.cid=c.id')
->where('a.create_time','>',$time['firstday'])
->where('a.create_time','<',$time['lastday'])
->field('a.id,a.cid,a.title,a.username,a.title_pic,a.summary,a.clicks,a.create_time,c.url,c.name as cname')
->order('id','desc')
->paginate();
$this->assign('data', $data);
$this->assign('year', $year);
$this->assign('month', $month);
return $this->fetch();
}
/**
* 归档页面(发布者)
*/
public function author($name = null)
{
if (!$name) {
throw new \think\exception\HttpException(404, '页面不存在');
}
$data = Db::table('sur_article')->alias('a')->join('sur_category c','a.cid=c.id')
->where('a.username',$name)
->field('a.id,a.cid,a.title,a.username,a.title_pic,a.summary,a.clicks,a.create_time,c.url,c.name as cname')
->order('id','desc')
->paginate();
$this->assign('data', $data);
$this->assign('username', $name);
return $this->fetch();
}
public function tags($name=null)
{
if (!$name) {
throw new \think\exception\HttpException(404, '页面不存在');
}
$tagsModel = new TagsModel();
$data = $tagsModel->where('tag', $name)->field('id, cid, title, username, title_pic, summary, clicks, create_time, url, cname')->order('id', 'desc')->paginate();
$this->assign('data', $data);
$this->assign('tag', $name);
return $this->fetch();
}
}
|