更新后端内容
This commit is contained in:
@@ -13,37 +13,90 @@ class Article extends Base
|
||||
// 文章列表
|
||||
public function articlelist()
|
||||
{
|
||||
$lists = Db::table('yz_article')
|
||||
->where('delete_time', null)
|
||||
->where('status', '<>', 3)
|
||||
->order('id DESC')
|
||||
->select()
|
||||
->each(function ($item) {
|
||||
// 获取分类信息
|
||||
if (Request::isPost()) {
|
||||
$category = input('post.category');
|
||||
$page = input('post.page', 1);
|
||||
$limit = input('post.limit', 20);
|
||||
|
||||
$query = Db::table('yz_article')
|
||||
->where('delete_time', null)
|
||||
->where('status', '<>', 3);
|
||||
|
||||
if (!empty($category)) {
|
||||
// 先获取分类ID
|
||||
$cateInfo = Db::table('yz_article_category')
|
||||
->where('id', $item['cate'])
|
||||
->field('name, image')
|
||||
->where('name', $category)
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->find();
|
||||
|
||||
// 设置分类名称
|
||||
$item['cate'] = $cateInfo['name'];
|
||||
|
||||
// 如果文章没有图片,使用分类的图片
|
||||
if (empty($item['image']) && !empty($cateInfo['image'])) {
|
||||
$item['image'] = $cateInfo['image'];
|
||||
|
||||
if ($cateInfo) {
|
||||
$query = $query->where('cate', $cateInfo['id']);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取总记录数
|
||||
$count = $query->count();
|
||||
|
||||
// 获取分页数据
|
||||
$lists = $query->order('id DESC')
|
||||
->page($page, $limit)
|
||||
->select()
|
||||
->each(function ($item) {
|
||||
// 获取分类信息
|
||||
$cateInfo = Db::table('yz_article_category')
|
||||
->where('id', $item['cate'])
|
||||
->field('name, image')
|
||||
->find();
|
||||
|
||||
// 格式化时间
|
||||
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
|
||||
$item['publishdate'] = $item['publishdate'] ? date('Y-m-d H:i:s', $item['publishdate']) : '';
|
||||
// 设置分类名称
|
||||
$item['cate'] = $cateInfo['name'];
|
||||
|
||||
return $item;
|
||||
});
|
||||
// 如果文章没有图片,使用分类的图片
|
||||
if (empty($item['image']) && !empty($cateInfo['image'])) {
|
||||
$item['image'] = $cateInfo['image'];
|
||||
}
|
||||
|
||||
View::assign([
|
||||
'lists' => $lists
|
||||
]);
|
||||
return View::fetch();
|
||||
// 格式化时间
|
||||
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
|
||||
$item['publishdate'] = $item['publishdate'] ? date('Y-m-d H:i:s', $item['publishdate']) : '';
|
||||
|
||||
return $item;
|
||||
});
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取成功',
|
||||
'count' => $count,
|
||||
'data' => $lists
|
||||
]);
|
||||
} else {
|
||||
// 获取所有分类并构建父子结构
|
||||
$allCategories = Db::table('yz_article_category')
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->order('sort asc, id asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$categories = [];
|
||||
foreach ($allCategories as $category) {
|
||||
if ($category['cid'] == 0) {
|
||||
$category['children'] = [];
|
||||
foreach ($allCategories as $subCategory) {
|
||||
if ($subCategory['cid'] == $category['id']) {
|
||||
$category['children'][] = $subCategory;
|
||||
}
|
||||
}
|
||||
$categories[] = $category;
|
||||
}
|
||||
}
|
||||
|
||||
View::assign([
|
||||
'categories' => $categories
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
// 添加文章
|
||||
@@ -149,41 +202,43 @@ class Article extends Base
|
||||
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)
|
||||
->order('sort asc, id asc')
|
||||
->select()
|
||||
->each(function ($item) {
|
||||
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
|
||||
return $item;
|
||||
});
|
||||
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()
|
||||
->each(function ($item) {
|
||||
$item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
|
||||
return $item;
|
||||
});
|
||||
->toArray();
|
||||
|
||||
View::assign([
|
||||
'lists' => $lists
|
||||
]);
|
||||
return View::fetch();
|
||||
// return json(['code' => 0, 'msg' => '获取成功', 'data' => $lists]);
|
||||
// 构建树形结构
|
||||
$tree = [];
|
||||
foreach ($lists as $item) {
|
||||
if ($item['cid'] == 0) {
|
||||
$node = [
|
||||
'id' => $item['id'],
|
||||
'title' => $item['name'],
|
||||
'children' => []
|
||||
];
|
||||
|
||||
// 查找子分类
|
||||
foreach ($lists as $subItem) {
|
||||
if ($subItem['cid'] == $item['id']) {
|
||||
$node['children'][] = [
|
||||
'id' => $subItem['id'],
|
||||
'title' => $subItem['name'],
|
||||
'children' => []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$tree[] = $node;
|
||||
}
|
||||
}
|
||||
|
||||
return json(['code' => 0, 'msg' => '获取成功', 'data' => $tree]);
|
||||
}
|
||||
|
||||
// 非 POST 请求返回视图
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
//获取分类结构
|
||||
@@ -224,15 +279,13 @@ class Article extends Base
|
||||
public function cateadd()
|
||||
{
|
||||
if (Request::isPost()) {
|
||||
// 将时间戳转换为 'Y-m-d H:i:s' 格式
|
||||
$currentDateTime = time();
|
||||
$data = [
|
||||
'name' => input('post.name'),
|
||||
'image' => input('post.image'),
|
||||
'cid' => input('post.cid'),
|
||||
'sort' => input('post.sort', 0),
|
||||
'status' => input('post.status', 1),
|
||||
'create_time' => $currentDateTime
|
||||
'create_time' => time()
|
||||
];
|
||||
|
||||
$insert = Db::table('yz_article_category')->insert($data);
|
||||
@@ -241,17 +294,22 @@ class Article extends Base
|
||||
}
|
||||
return json(['code' => 0, 'msg' => '添加成功', 'data' => []]);
|
||||
} else {
|
||||
$lists = Db::table('yz_article_category')
|
||||
->order('id DESC')
|
||||
// 获取所有可选的父级分类
|
||||
$parentCategories = Db::table('yz_article_category')
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->where('cid', 0)
|
||||
->field('id, name')
|
||||
->select()
|
||||
->each(function ($item, $key) {
|
||||
$item['create_time'] = time();
|
||||
return $item;
|
||||
});
|
||||
View::assign([
|
||||
'lists' => $lists
|
||||
->toArray();
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'parentOptions' => $parentCategories
|
||||
]
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,10 +338,39 @@ class Article extends Base
|
||||
} else {
|
||||
$id = input('get.id');
|
||||
$info = Db::table('yz_article_category')->where('id', $id)->find();
|
||||
View::assign([
|
||||
'info' => $info
|
||||
|
||||
// 获取所有可选的父级分类
|
||||
$parentCategories = Db::table('yz_article_category')
|
||||
->where('delete_time', null)
|
||||
->where('status', 1)
|
||||
->where('id', '<>', $id) // 排除自己
|
||||
->where(function ($query) use ($id) {
|
||||
// 排除自己的所有子分类
|
||||
$query->where('cid', '<>', $id);
|
||||
})
|
||||
->field('id, name, cid')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 构建父级分类选项
|
||||
$parentOptions = [];
|
||||
foreach ($parentCategories as $category) {
|
||||
if ($category['cid'] == 0) {
|
||||
$parentOptions[] = [
|
||||
'id' => $category['id'],
|
||||
'name' => $category['name']
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'info' => $info,
|
||||
'parentOptions' => $parentOptions
|
||||
]
|
||||
]);
|
||||
return View::fetch();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user