Files
guangan/plugs/think-plugs-cms/src/controller/Article.php
2024-11-27 15:16:05 +08:00

143 lines
3.5 KiB
PHP

<?php
namespace plugin\cms\controller;
use plugin\cms\model\CmsArticle;
use plugin\cms\model\CmsCateArticle;
use plugin\cms\model\CmsCategory;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
/**
* 文章管理
*/
class Article extends Controller
{
/**
* 文章管理
* @auth true
* @menu true
* @login true
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
$this->title = '文章管理';
CmsArticle::mQuery()->layTable(function () {
}, static function (QueryHelper $query) {
$query->where('is_deleted', 0);
$query->with(['category']);
$query->equal('status')->like('title');
$query->timeBetween('create_at');
});
}
public function _index_page_filter(&$list)
{
foreach ($list as &$item) {
$cate_list = $item['category'];
$item['cate_name'] = join(',', array_column($cate_list, 'name'));
}
}
/**
* 添加文章
* @auth true
* @menu true
* @login true
* @return void
*/
public function add()
{
$this->title = '添加文章';
$this->cates = CmsCategory::getList();
$this->cate_ids = [];
CmsArticle::mForm('form');
}
/**
* 编辑文章
* @auth true
* @menu true
* @login true
* @return void
*/
public function edit()
{
$this->title = '编辑文章';
$this->cates = CmsCategory::getList();
$this->id = $this->request->param('id');
$this->cate_ids = CmsCateArticle::where(['aid' => $this->id])->column('cid');
CmsArticle::mForm('form', "id");
}
public function _form_filter(&$data)
{
unset($data['cate_ids']);
}
public function _form_result($state, $data)
{
if (isset($data['id'])) {
CmsCateArticle::where(['aid' => $data['id']])->delete();
$cate_ids = $this->request->param('cate_ids', []);
foreach ($cate_ids as $cid) {
CmsCateArticle::create(['aid' => $data['id'], 'cid' => $cid]);
}
}
}
/**
* 删除文章
* @auth true
* @menu true
* @login true
* @return void
*/
public function remove()
{
CmsArticle::mDelete('id');
}
/**
* 修改文章状态
* @auth true
* @menu true
* @login true
* @return void
*/
public function status()
{
CmsArticle::mSave($this->_vali([
'id.require' => '文章ID不能为空',
'status.in:0,1' => '状态值范围异常!',
'status.require' => '状态值不能为空!',
]), 'id');
}
public function _status_save_result($state, $data)
{
if ($state) {
$status = $this->request->param('status');
$id = $this->request->param('id');
if ($status == 1) {
CmsArticle::mk()->where('id', $id)->save([
'publish_at' => date('Y-m-d H:i:s')
]);
}
}
}
public function sort()
{
CmsArticle::mSave($this->_vali([
'id.require' => '文章ID不能为空',
'sort.require' => '排序值不能为空!',
'sort.number' => '排序必须为数字!',
'sort.between:0,9999' => '排序值必须为0~9999之间!',
]), 'id');
}
}