123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- declare(strict_types=1);
- /**
- * +----------------------------------------------------------------------
- * 文章控制制器
- * @author huwhis@163.com
- * @version 0.0.6
- * +----------------------------------------------------------------------
- */
- namespace app\index\controller;
- // 引入框架内置类
- use think\facade\View;
- use think\exception\HttpException;
- use app\common\model\Category as CategoryModel;
- use app\common\model\Article as ArticleModel;
- use app\common\model\ArticleBrowerHistory;
- use app\common\model\ArticleTags as ArticleTagsModel;
- use app\common\model\ArticleDolikeLog;
- use app\common\model\Tag;
- use app\common\model\TagArticle;
- use app\common\utils\ParsedownUtils;
- /**
- * 文章管理
- */
- class Article extends Base
- {
- protected $modelName = "Article";
- public function index()
- {
- $params = $this->app->request->param();
- $list = ArticleModel::queryPage($params);
- $category = CategoryModel::find($params['cid']);
- if ($category) {
- $baseUrl = $category->url;
- } else {
- $baseUrl = '/index/all';
- }
- View::assign([
- 'baseUrl' => $baseUrl,
- 'list' => $list->all(),
- 'total' => $list->total(),
- 'limit' => $list->listRows(),
- 'page' => $list->currentPage(),
- 'lastPage' => $list->lastPage(),
- 'pagelist' => $list->render(),
- 'cid' => $params['cid'],
- ]);
- $html = View::fetch();
- // if ($this->html) {
- // $this->makeHtmlFile($html);
- // }
- return $html;
- }
- /**
- * 阅读文章
- */
- public function read($id = null)
- {
- $data = ArticleModel::getOne($id);
- if (!$data) {
- throw new HttpException(404, '页面不存在');
- }
- $ip = $this->request->ip();
- $time = time();
- $abh = ArticleBrowerHistory::getByIpAid($ip, $id);
- if (!$abh) {
- $data->hits += 1;
-
- $data->isAutoWriteTimestamp(false)->save();
- ArticleBrowerHistory::create([
- 'ip' => $ip,
- 'aid'=> $id,
- 'create_time' => $time
- ]);
- } else {
- $createTime = $abh->create_time;
- if ($createTime + 24*3600 < $time) {
- $data->hits += 1;
-
- $data->isAutoWriteTimestamp(false)->save();
- }
- }
- $prev_next = ArticleModel::getNextPrev($id, $data->cid);
- if ($data->content_type == 1) {
- $parsedownExtension = new ParsedownUtils();
- // $parsedownExtension->setTocEnabled(true);
- $res = $parsedownExtension->text($data->content);
- // $data->toc = $res['toc'];
- $data->content = $res['content'];
- }
- View::assign('cid', $data->cid);
- View::assign('data', $data);
- View::assign('prev_next', $prev_next);
- $this->seo['title'] = $data->title;
- $this->seo['key'] = $data->keywords;
- $this->seo['des'] = $data->summary;
- View::assign('seo', $this->seo);
- $html = View::fetch();
- return $html;
- }
- /**
- * 文章点赞
- */
- public function dolike()
- {
- $id = $this->request->post('id');
- $ip = $this->request->ip();
- $log = ArticleDolikeLog::where('aid', $id)->where('ip', $ip)->find();
- if ($log) {
- return ['code' => 1, 'msg' => '多谢喜欢, 已经点过赞了'];
- }
- $res = ArticleModel::where('id', $id)->inc('likes')->update();
- // 记录 dolike 日志
- @ArticleDolikeLog::create([
- 'aid' => $id,
- 'ip' => $this->request->ip()
- ]);
- if ($res === false) {
- return ['code' => 2, 'msg' => '未知错误'];
- } else {
- return ['code' => 0];
- }
- }
- /**
- * 标签列表
- */
- public function tag($name = null)
- {
- if (!$name) {
- throw new HttpException(404, '标签不可为空');
- }
- $tag = Tag::where('tagname', $name)->find();
- if (!$tag) {
- throw new HttpException(404, '标签不存在');
- }
- $list = TagArticle::queryList($tag->tagid);
- View::assign([
- 'list' => $list,
- 'tag' => $name
- ]);
- return View::fetch();
- }
- /**
- * 归档页面(时间)
- */
- public function archive($year = 0, $month = 0)
- {
- $yearMonth = $year . '-' . $month;
- if ($year == 0 || $month == 0) {
- throw new HttpException(404, '日期格式不正确');
- }
- $list = ArticleModel::queryPage(['yearMonth' => $yearMonth]);
- View::assign([
- 'list' => $list->all(),
- 'total' => $list->total(),
- 'limit' => $list->listRows(),
- 'page' => $list->currentPage(),
- 'lastPage' => $list->lastPage(),
- 'pagelist' => $list->render(),
- 'yearMonth' => $yearMonth
- ]);
- $html = View::fetch();
- // if ($this->html) {
- // $this->makeHtmlFile($html);
- // }
- return $html;
- }
- /**
- * 时光轴
- */
- public function time()
- {
- $params = ['order' => 'update_time desc'];
- $list = ArticleModel::queryPage($params);
- View::assign([
- 'list' => $list->all(),
- 'total' => $list->total(),
- 'limit' => $list->listRows(),
- 'page' => $list->currentPage(),
- 'lastPage' => $list->lastPage(),
- 'pagelist' => $list->render(),
- ]);
- $html = View::fetch();
- return $html;
- }
- }
|