129 lines
3.7 KiB
PHP
129 lines
3.7 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | 文章CMS后台分类管理
|
|
// +----------------------------------------------------------------------
|
|
declare(strict_types=1);
|
|
|
|
namespace addon\articles\controller\backend;
|
|
|
|
use think\facade\Request;
|
|
use think\facade\View;
|
|
use think\Validate;
|
|
use ywxapp\controller\BackendBase;
|
|
use addon\articles\model\ArticleCategory;
|
|
use addon\articles\library\ArticleSchema;
|
|
|
|
class Category extends BackendBase
|
|
{
|
|
|
|
/**
|
|
* Summary of needLogin
|
|
* @var array
|
|
*/
|
|
protected $noNeedLogin = [];
|
|
|
|
/**
|
|
* Summary of needRight
|
|
* @var array
|
|
*/
|
|
protected $noNeedVerify = [];
|
|
|
|
protected function initialize()
|
|
{
|
|
parent::initialize();
|
|
ArticleSchema::ensure();
|
|
}
|
|
|
|
/**
|
|
* 显示资源列表
|
|
*
|
|
* @param \think\Request $request
|
|
* @return \think\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$page = $this->request->param('page', 1);
|
|
$size = $this->request->param('size', 15);
|
|
if ($this->request->isAjax()) {
|
|
$data = ArticleCategory::paginate([
|
|
'page' => $page,
|
|
'list_rows' => $size,
|
|
]);
|
|
$this->result->success($data->items());
|
|
}
|
|
$this->view->assign('title', '登录');
|
|
return $this->view->fetch('category/index');
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
if (Request::isPost()) {
|
|
$data = Request::post();
|
|
$data['status'] = isset($data['status']) ? 1 : 0;
|
|
|
|
$validate = new Validate([
|
|
'name|名称' => 'require|max:50',
|
|
'alias|别名' => 'require|max:50',
|
|
]);
|
|
if (!$validate->check($data)) {
|
|
$this->result->error($validate->getError());
|
|
}
|
|
ArticleCategory::create($data);
|
|
$this->result->success();
|
|
}
|
|
}
|
|
|
|
public function edit($id = null)
|
|
{
|
|
$category = ArticleCategory::find($id);
|
|
if (!$category) {
|
|
return redirect('/articles/backend/category');
|
|
}
|
|
return View::fetch('category/edit', ['category' => $category]);
|
|
}
|
|
|
|
public function update($id = 0)
|
|
{
|
|
$data = Request::post();
|
|
$id = $id ?: (int) ($data['id'] ?? 0);
|
|
$data['update_at'] = time();
|
|
$data['status'] = isset($data['status']) ? 1 : 0;
|
|
|
|
$validate = new Validate([
|
|
'name|名称' => 'require|max:50',
|
|
'alias|别名' => 'require|max:50',
|
|
]);
|
|
if (!$validate->check($data)) {
|
|
return json(['code' => 0, 'msg' => $validate->getError()]);
|
|
}
|
|
|
|
if ($id) {
|
|
$category = ArticleCategory::find($id);
|
|
if (!$category) {
|
|
return json(['code' => 0, 'msg' => '分类不存在']);
|
|
}
|
|
$category->save($data);
|
|
} else {
|
|
$data['create_at'] = time();
|
|
ArticleCategory::create($data);
|
|
}
|
|
return json(['code' => 0, 'msg' => '保存成功', 'url' => '/articles/backend/category']);
|
|
}
|
|
|
|
public function delete($id = 0)
|
|
{
|
|
$id = $id ?: (int) Request::post('id', 0);
|
|
$category = ArticleCategory::find($id);
|
|
if ($category) {
|
|
if (ArticleCategory::hasArticles($id)) {
|
|
return json(['code' => 0, 'msg' => '该分类下还有文章,无法删除']);
|
|
}
|
|
$category->delete();
|
|
return json(['code' => 0, 'msg' => '已删除']);
|
|
}
|
|
return json(['code' => 0, 'msg' => '分类不存在']);
|
|
}
|
|
}
|