增加文章发布功能
This commit is contained in:
@@ -0,0 +1,279 @@
|
||||
<?php
|
||||
/**
|
||||
* 后台管理系统-文章管理
|
||||
*/
|
||||
namespace app\admin\controller;
|
||||
use app\admin\controller\Base;
|
||||
use think\facade\Db;
|
||||
use think\facade\View;
|
||||
use think\facade\Request;
|
||||
|
||||
class Article extends Base
|
||||
{
|
||||
// 文章列表
|
||||
public function articlelist()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$lists = Db::table('yz_article')
|
||||
->where('delete_time', null)
|
||||
->where('status', '<>', 3)
|
||||
->order('id DESC')
|
||||
->select()
|
||||
->each(function ($item) {
|
||||
$item['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
|
||||
$item['publishdate'] = $item['publishdate'] ? date('Y-m-d H:i:s', strtotime($item['publishdate'])) : '';
|
||||
return $item;
|
||||
});
|
||||
return json(['code' => 0, 'msg' => '获取成功', 'data' => $lists]);
|
||||
} else {
|
||||
$lists = Db::table('yz_article')
|
||||
->where('delete_time', null)
|
||||
->where('status', '<>', 3)
|
||||
->order('id DESC')
|
||||
->select()
|
||||
->each(function ($item) {
|
||||
$item['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
|
||||
return $item;
|
||||
});
|
||||
View::assign(['lists' => $lists]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
// 添加文章
|
||||
public function add()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$data = [
|
||||
'title' => input('post.title'),
|
||||
'cate' => input('post.cate'),
|
||||
'image' => input('post.image'),
|
||||
'content' => input('post.content'),
|
||||
'author' => input('post.author'),
|
||||
'desc' => input('post.desc'),
|
||||
'status' => input('post.status', 2),
|
||||
'publishdate' => date('Y-m-d H:i:s', time()),
|
||||
'create_time' => date('Y-m-d H:i:s', time())
|
||||
];
|
||||
|
||||
$insert = Db::table('yz_article')->insert($data);
|
||||
if (empty($insert)) {
|
||||
return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
|
||||
} else {
|
||||
$lists = Db::table('yz_article')
|
||||
->order('id DESC')
|
||||
->select()
|
||||
->each(function ($item, $key) {
|
||||
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
|
||||
return $item;
|
||||
});
|
||||
View::assign([
|
||||
'lists' => $lists
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
// 编辑文章
|
||||
public function edit()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$article_id = input('post.article_id');
|
||||
$data = [
|
||||
'title' => input('post.title'),
|
||||
'content' => input('post.content'),
|
||||
'author' => input('post.author'),
|
||||
'status' => input('post.status', 1),
|
||||
'update_time' => time()
|
||||
];
|
||||
|
||||
$update = Db::table('yz_article')
|
||||
->where('article_id', $article_id)
|
||||
->update($data);
|
||||
|
||||
if ($update === false) {
|
||||
return json(['code' => 1, 'msg' => '更新失败', 'data' => []]);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '更新成功', 'data' => []]);
|
||||
} else {
|
||||
$article_id = input('get.article_id');
|
||||
$info = Db::table('yz_article')->where('article_id', $article_id)->find();
|
||||
View::assign([
|
||||
'info' => $info
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
// 删除文章
|
||||
public function delete()
|
||||
{
|
||||
$id = input('post.id');
|
||||
$data = [
|
||||
'delete_time' => date('Y-m-d H:i:s', time()),
|
||||
];
|
||||
$delete = Db::table('yz_article')->where('id', $id)->update($data);
|
||||
if ($delete === false) {
|
||||
return json(['code' => 1, 'msg' => '删除失败', 'data' => []]);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
|
||||
}
|
||||
|
||||
// 文章分类
|
||||
public function articlecate()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$count = Db::table('yz_article_category')
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->count();
|
||||
$page = (int) input('post.page', 1);
|
||||
$limit = (int) input('post.limit', 10);
|
||||
$lists = Db::table('yz_article_category')
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->order('id asc')
|
||||
->page($page, $limit)
|
||||
->select();
|
||||
return json(['code' => 0, 'msg' => '获取成功', 'data' => $lists]);
|
||||
} else {
|
||||
// 获取分类列表
|
||||
$lists = Db::table('yz_article_category')
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->order('id asc')
|
||||
->select();
|
||||
|
||||
View::assign([
|
||||
'lists' => $lists
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
//获取分类结构
|
||||
public function getcate()
|
||||
{
|
||||
// 获取所有分类
|
||||
$lists = Db::table('yz_article_category')
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->order('sort asc, id asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 构建父子结构
|
||||
$tree = [];
|
||||
foreach ($lists as $item) {
|
||||
if ($item['cid'] == 0) {
|
||||
// 顶级分类
|
||||
$tree[] = $item;
|
||||
} else {
|
||||
// 子分类
|
||||
foreach ($tree as &$parent) {
|
||||
if ($parent['id'] == $item['cid']) {
|
||||
if (!isset($parent['children'])) {
|
||||
$parent['children'] = [];
|
||||
}
|
||||
$parent['children'][] = $item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return json(['code' => 0, 'msg' => '获取成功', 'data' => $tree]);
|
||||
}
|
||||
|
||||
// 添加文章分类
|
||||
public function cateadd()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
// 将时间戳转换为 'Y-m-d H:i:s' 格式
|
||||
$currentDateTime = date('Y-m-d H:i:s');
|
||||
$data = [
|
||||
'name' => input('post.name'),
|
||||
'cid' => input('post.cid'),
|
||||
'sort' => input('post.sort', 0),
|
||||
'status' => input('post.status', 1),
|
||||
'create_time' => $currentDateTime
|
||||
];
|
||||
|
||||
$insert = Db::table('yz_article_category')->insert($data);
|
||||
if (empty($insert)) {
|
||||
return json(['code' => 1, 'msg' => '添加失败', 'data' => []]);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
|
||||
} else {
|
||||
$lists = Db::table('yz_article_category')
|
||||
->order('id DESC')
|
||||
->select()
|
||||
->each(function ($item, $key) {
|
||||
$item['create_time'] = date('Y-m-d H:i:s', strtotime($item['create_time']));
|
||||
return $item;
|
||||
});
|
||||
View::assign([
|
||||
'lists' => $lists
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
//编辑文章分类
|
||||
public function cateedit()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
$data = [
|
||||
'id' => input('post.id'),
|
||||
'name' => input('post.name'),
|
||||
'cid' => input('post.cid'),
|
||||
'sort' => input('post.sort', 0),
|
||||
'status' => input('post.status', 1),
|
||||
'update_time' => date('Y-m-d H:i:s')
|
||||
];
|
||||
|
||||
$update = Db::table('yz_article_category')
|
||||
->where('id', $data['id'])
|
||||
->update($data);
|
||||
|
||||
if ($update === false) {
|
||||
return json(['code' => 1, 'msg' => '更新失败', 'data' => []]);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '更新成功', 'data' => []]);
|
||||
} else {
|
||||
$id = input('get.id');
|
||||
$info = Db::table('yz_article_category')->where('id', $id)->find();
|
||||
View::assign([
|
||||
'info' => $info
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
//删除文章分类
|
||||
public function catedel()
|
||||
{
|
||||
$id = input('post.id');
|
||||
|
||||
// 检查是否有子分类
|
||||
$hasChildren = Db::table('yz_article_category')
|
||||
->where('cid', $id)
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if ($hasChildren) {
|
||||
return json(['code' => 1, 'msg' => '该分类下有子分类,无法删除', 'data' => []]);
|
||||
}
|
||||
|
||||
$delete = Db::table('yz_article_category')
|
||||
->where('id', $id)
|
||||
->update(['delete_time' => date('Y-m-d H:i:s')]);
|
||||
|
||||
if ($delete === false) {
|
||||
return json(['code' => 1, 'msg' => '删除失败', 'data' => []]);
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '删除成功', 'data' => []]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user