diff --git a/app/admin/controller/Article.php b/app/admin/controller/Article.php index bdf6329..6a5d424 100644 --- a/app/admin/controller/Article.php +++ b/app/admin/controller/Article.php @@ -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(); } } diff --git a/app/admin/view/article/articlecate.php b/app/admin/view/article/articlecate.php index 8e8c0ca..f79e4ce 100644 --- a/app/admin/view/article/articlecate.php +++ b/app/admin/view/article/articlecate.php @@ -1,8 +1,8 @@ {include file="public/header" /}
-
- 文章分类列表 +
+ 文章分类
- - - - - - - - - - - - - - - {volist name="lists" id='vo'} - {if condition="$vo.cid eq 0"} - - - - - {$vo.desc} - - - - - - {volist name="lists" id='sub'} - {if condition="$sub.cid eq $vo.id"} - - - - - - - - - - - {/if} - {/volist} - {/if} - {/volist} - -
ID分类名称分类图片描述排序状态创建时间操作
{$vo.id}{$vo.name}{$vo.sort}{$vo.status==1?'开启':'禁用'}{$vo.create_time} - - - -
{$sub.id}├─ {$sub.name}{$sub.desc}{$sub.sort}{$sub.status==1?'开启':'禁用'}{$sub.create_time} - - -
+
+
+
+
分类列表
+
+
+
+
+
+
+
+
分类信息
+
+
+ +

请选择左侧分类

+
+ +
+
+
+
\ No newline at end of file + + + \ No newline at end of file diff --git a/app/admin/view/article/articlelist.php b/app/admin/view/article/articlelist.php index f921116..23c2524 100644 --- a/app/admin/view/article/articlelist.php +++ b/app/admin/view/article/articlelist.php @@ -1,8 +1,25 @@ {include file="public/header" /}
-
+
文章列表 +
+ +
+
+ +
+
+
- - - - - - - - - - - - - - - {volist name="lists" id='vo'} - - - - - - - - - - - {/volist} - -
ID标题分类封面作者状态发布时间操作
{$vo.id}{$vo.title}{$vo.cate} - {if condition="$vo.image"} - - {/if} - {$vo.author} - {switch name="vo.status"} - {case value="0"}草稿{/case} - {case value="1"}待审核{/case} - {case value="2"}已发布{/case} - {case value="3"}隐藏{/case} - {/switch} - {$vo.publishdate} - - -
+
+ + + + + + \ No newline at end of file diff --git a/public/static/css/moban.css b/public/static/css/moban.css index 659bee3..8115a88 100644 --- a/public/static/css/moban.css +++ b/public/static/css/moban.css @@ -33,3 +33,9 @@ background: #e9ecef; color: #409eff; } +.shaixuan { + margin-left: 20px; + display: flex; + align-items: center; +} + diff --git a/runtime/admin/temp/705995d2a8e3fb66a54834e54543f090.php b/runtime/admin/temp/705995d2a8e3fb66a54834e54543f090.php index a7721fb..9e2a71d 100644 --- a/runtime/admin/temp/705995d2a8e3fb66a54834e54543f090.php +++ b/runtime/admin/temp/705995d2a8e3fb66a54834e54543f090.php @@ -1,4 +1,4 @@ - + @@ -95,8 +95,25 @@
-
+
文章列表 +
+ +
+
+ +
+
+
- - - - - - - - - - - - - - - $vo): $mod = ($i % 2 );++$i;?> - - - - - - - - - - - - -
ID标题分类封面作者状态发布时间操作
- - - - - 草稿待审核已发布隐藏 - - - - -
+
+ + + + + + \ No newline at end of file diff --git a/runtime/admin/temp/a7bfd508279dfa35c2e6ff0e3f27da40.php b/runtime/admin/temp/a7bfd508279dfa35c2e6ff0e3f27da40.php index 4c8406a..69c1715 100644 --- a/runtime/admin/temp/a7bfd508279dfa35c2e6ff0e3f27da40.php +++ b/runtime/admin/temp/a7bfd508279dfa35c2e6ff0e3f27da40.php @@ -1,4 +1,4 @@ - + @@ -95,8 +95,8 @@
-
- 文章分类列表 +
+ 文章分类
- - - - - - - - - - - - - - - $vo): $mod = ($i % 2 );++$i;if($vo['cid'] == 0): ?> - - - - - - - - - - - $sub): $mod = ($i % 2 );++$i;if($sub['cid'] == $vo['id']): ?> - - - - - - - - - - - - - - - -
ID分类名称分类图片描述排序状态创建时间操作
禁用'; ?> - - - -
├─ 禁用'; ?> - - -
+
+
+
+
分类列表
+
+
+
+
+
+
+
+
分类信息
+
+
+ +

请选择左侧分类

+
+ +
+
+
+
\ No newline at end of file + + + \ No newline at end of file diff --git a/runtime/index/temp/69170ce622adbb0032543cdbee52d3fd.php b/runtime/index/temp/69170ce622adbb0032543cdbee52d3fd.php index 860a4e8..266b5cf 100644 --- a/runtime/index/temp/69170ce622adbb0032543cdbee52d3fd.php +++ b/runtime/index/temp/69170ce622adbb0032543cdbee52d3fd.php @@ -1,4 +1,4 @@ - + @@ -203,7 +203,7 @@ layui.use(['carousel', 'form'], function(){
-

技术文章

+

技术文章11

全部