147 lines
3.9 KiB
PHP
147 lines
3.9 KiB
PHP
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\index\controller;
|
|
|
|
use app\index\model\ArticleCategoryModel;
|
|
use app\index\model\ArticleLeaveMessageModel;
|
|
use app\index\model\ArticleModel;
|
|
use app\index\model\SysBannerItemModel;
|
|
use think\facade\Validate;
|
|
use think\facade\View;
|
|
|
|
/**
|
|
* 文章控制器
|
|
*/
|
|
class Article extends Common
|
|
{
|
|
/**
|
|
* 文件列表
|
|
*/
|
|
public function index()
|
|
{
|
|
// 获取上级分类
|
|
$param = request()->param([
|
|
'pid',
|
|
'cid',
|
|
'keywords' => null,
|
|
'page/d' => 1,
|
|
'size/d' => 12,
|
|
]);
|
|
|
|
// 获取banner焦点图
|
|
$banner = SysBannerItemModel::hasWhere('banner', [
|
|
'language_id' => $this->lang_id,
|
|
'unique_label' => 'BANNER_67f9fc71e27db'
|
|
])
|
|
->type('image')
|
|
->visible(['id', 'title', 'image', 'link'])
|
|
->find();
|
|
View::assign('banner', $banner);
|
|
|
|
// 获取分类列表
|
|
$categorys = ArticleCategoryModel::field([
|
|
'id',
|
|
'pid',
|
|
'name'
|
|
])
|
|
->language($this->lang_id)
|
|
->parent($param['pid']??null)
|
|
->isShow(true)
|
|
->order(['sort' => 'asc', 'id' => 'desc'])
|
|
->select();
|
|
View::assign('categorys', $categorys);
|
|
|
|
// 获取文章列表
|
|
if (!$categorys->isEmpty()) {
|
|
$articles = ArticleModel::field([
|
|
'id',
|
|
'title',
|
|
'desc',
|
|
'image'
|
|
])
|
|
->withSearch(['title'], ['title' => $param['keywords']??null])
|
|
->category($param['cid']??$categorys[0]['id'])
|
|
->where('release_time', '<=', date('Y-m-d H:i:s'))
|
|
->order(['sort' => 'asc', 'release_time' => 'desc', 'id' => 'desc'])
|
|
->paginate([
|
|
'list_rows' => $param['size'],
|
|
'page' => $param['page'],
|
|
]);
|
|
}
|
|
View::assign('articles', $articles??[]);
|
|
|
|
return View::fetch('index');
|
|
}
|
|
|
|
/**
|
|
* 文章详情
|
|
*/
|
|
public function detail()
|
|
{
|
|
$id = request()->param('id');
|
|
$detail = ArticleModel::withoutField([
|
|
'created_at',
|
|
'updated_at',
|
|
'deleted_at'
|
|
])
|
|
->bypk($id)
|
|
->find();
|
|
View::assign('detail', $detail);
|
|
|
|
// 获取倒序或发布时间倒序3篇文章做为推荐文章
|
|
$recommends = ArticleModel::field([
|
|
'id',
|
|
'title',
|
|
'desc',
|
|
'image'
|
|
])
|
|
->where('id', '<>', $id)
|
|
->order(['release_time' => 'desc', 'id' => 'desc'])
|
|
->limit(3)
|
|
->select();
|
|
View::assign('recommends', $recommends);
|
|
|
|
return View::fetch('detail');
|
|
}
|
|
|
|
/**
|
|
* 留言
|
|
*/
|
|
public function comment()
|
|
{
|
|
$id = request()->param('id');
|
|
$post = request()->post([
|
|
'name',
|
|
'email',
|
|
'content'
|
|
]);
|
|
|
|
// 验证字段
|
|
$validate = Validate::rule([
|
|
'name' => 'max:64',
|
|
'email' => 'email'
|
|
])
|
|
->message([
|
|
'name.max' => '姓名不能超过64个字符',
|
|
'email' => '请输入正确的邮箱'
|
|
]);
|
|
if (!$validate->check($post)) {
|
|
return error($validate->getError());
|
|
}
|
|
|
|
// 保存留言
|
|
$data = array_merge($post, [
|
|
'article_id' => $id,
|
|
'ip' => request()->ip(),
|
|
'user_agent' => request()->header('user-agent')
|
|
]);
|
|
$ret = ArticleLeaveMessageModel::create($data);
|
|
if ($ret->isEmpty()) {
|
|
return error('留言提交失败');
|
|
}
|
|
|
|
return success('留言提交成功');
|
|
}
|
|
}
|