diff --git a/app/admin/controller/Category.php b/app/admin/controller/Category.php index 6ee270a..e1485df 100644 --- a/app/admin/controller/Category.php +++ b/app/admin/controller/Category.php @@ -126,13 +126,10 @@ class Category extends Common public function update(Request $request, $id) { // - - $post_data = $request->post(); $model_category = ModelCategory::where('title',$post_data['title']) ->where('pid',$post_data['pid']) - ->where('type',$post_data['type']) ->where('id','<>',$id) ->find(); diff --git a/app/admin/controller/Post.php b/app/admin/controller/Post.php index 872fa02..95aa3b8 100644 --- a/app/admin/controller/Post.php +++ b/app/admin/controller/Post.php @@ -19,7 +19,8 @@ class Post extends Common { parent::initialize(); - ModelPost::quickSelect(true); + $list_post = ModelPost::quickSelect(true); + } /** @@ -118,7 +119,9 @@ class Post extends Common // $model_post = ModelPost::find($id); + $list_category = Category::where('status', 1)->order('sort asc')->select(); + View::assign('list_category', $list_category); View::assign('post', $model_post); diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index 5f1b87e..1154268 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -16,14 +16,14 @@ class Index extends Common * * @return \think\Response */ - public function index($post_id = 0) + public function index($post_id = 0,$category_id = 0) { - $list_post = Post::quickSelect(); + $list_post = Post::quickSelect(); if (empty($post_id)) { - $model_post = $list_post->first(); + $model_post = $list_post->first()->post->first(); } else { - $model_post = $list_post->where('id', $post_id)->first(); + $model_post = $list_post->where('id',$category_id)->column('post')[0]->where('id', $post_id)->first(); } View::assign('model_post', $model_post); diff --git a/app/index/route/app.php b/app/index/route/app.php index fe110e1..0a858b3 100644 --- a/app/index/route/app.php +++ b/app/index/route/app.php @@ -2,4 +2,4 @@ use think\facade\Route; -Route::rule('doc/[:post_id]','Index/index'); \ No newline at end of file +Route::rule('doc/[:category_id]/[:post_id]','Index/index'); \ No newline at end of file diff --git a/app/model/Category.php b/app/model/Category.php index 8ef5cb3..5cc26bc 100644 --- a/app/model/Category.php +++ b/app/model/Category.php @@ -17,26 +17,32 @@ class Category extends Model public static $allCategory = []; + + public function post() + { + return $this->hasMany(Post::class, 'category_id'); + } + /** * 获取指定id下的所有分类 * * @param string $id * @return void */ - public static function getListLevel($id = '',$type = 1) + public static function getListLevel($id = '', $type = 1) { - if(empty(self::$allCategory)){ + if (empty(self::$allCategory)) { - $model_list = Category::where('type',$type)->order('sort asc')->select(); - self::$allCategory = array2level($model_list,0,0); + $model_list = Category::where('type', $type)->order('sort asc')->select(); + self::$allCategory = array2level($model_list, 0, 0); } - if(!empty($id)){ + if (!empty($id)) { $list = []; $in_category = [$id]; foreach (self::$allCategory as $category) { - if(in_array($category->pid,$in_category)){ + if (in_array($category->pid, $in_category)) { $list[] = $category; $in_category[] = $category->id; } @@ -51,13 +57,13 @@ class Category extends Model public function getTitleImgAttr($value) { - + return get_source_link($value); } public function posts() { - return $this->hasMany(PostCategory::class,'category_id'); + return $this->hasMany(PostCategory::class, 'category_id'); } /** @@ -72,7 +78,7 @@ class Category extends Model $list_post = []; foreach ($list_post_category as $list_post_category) { - array_push($list_post,$list_post_category->post); + array_push($list_post, $list_post_category->post); } return $list_post; @@ -86,34 +92,34 @@ class Category extends Model public function getPostsListAttr() { $list_post_category = $this->getAttr('posts'); - - $list_post = array_column($list_post_category->append(['post'])->toArray(),'post'); + + $list_post = array_column($list_post_category->append(['post'])->toArray(), 'post'); return $list_post; } public function getTplNameAttr($value) { - return Config::get('view_type.category.'.$value); + return Config::get('view_type.category.' . $value); } public function getModelParentAttr() { $pid = $this->getData('pid'); - if($pid == 0){ + if ($pid == 0) { return $this; } - return Category::where('id',$pid)->find(); + return Category::where('id', $pid)->find(); } // 返回除自身以外的其他的同级同类的分类 public function getModelSiblingsAttr() { - return Category::where('pid',$this->getData('pid')) - ->where('level',$this->getData('level')) - ->where('id','<>',$this->getData('id')) - ->select(); + return Category::where('pid', $this->getData('pid')) + ->where('level', $this->getData('level')) + ->where('id', '<>', $this->getData('id')) + ->select(); } /** @@ -123,9 +129,8 @@ class Category extends Model */ public function getModelSameParentAttr() { - return Category::where('pid',$this->getData('pid')) - ->where('level',$this->getData('level')) - ->select(); + return Category::where('pid', $this->getData('pid')) + ->where('level', $this->getData('level')) + ->select(); } - } diff --git a/app/model/Post.php b/app/model/Post.php index 7348d08..2ffa7ed 100644 --- a/app/model/Post.php +++ b/app/model/Post.php @@ -25,6 +25,11 @@ class Post extends Model protected $defaultSoftDelete = 0; + public function category() + { + return $this->belongsTo(Category::class, 'category_id'); + } + public function categorys() { return $this->hasMany(PostCategory::class, 'post_id'); @@ -162,11 +167,15 @@ class Post extends Model $list_post = Cache::get($cacke_key); if (empty($list_post) || $clear) { - $list_post = Post::where('status', 1)->order('sort desc')->select(); - Cache::set($cacke_key, $list_post,600); + + $list_post = Category::with(['post'])->where('type', 'default') + ->where('status', 1) + ->order('sort asc') + ->select(); + + Cache::set($cacke_key, $list_post, 600); } return $list_post; } - } diff --git a/public/cdn/layui-ul.css b/public/cdn/layui-ul.css index 726959e..9f03d0b 100644 --- a/public/cdn/layui-ul.css +++ b/public/cdn/layui-ul.css @@ -3,6 +3,12 @@ border-right: 1px solid #bbb; } +.ul-nav-tree-group-title{ + font-size: 16px; + font-weight: 600; + line-height: 2; +} + .ul-nav-tree-item { display : flex; align-items: baseline; diff --git a/view/admin/post/create.html b/view/admin/post/create.html index 59babb2..5875e7e 100644 --- a/view/admin/post/create.html +++ b/view/admin/post/create.html @@ -143,9 +143,11 @@
分类
+ + {volist name='list_category' id='vo'} + + {/volist} +
@@ -154,9 +156,9 @@
- - - + + +
跳转链接状态
diff --git a/view/admin/post/edit.html b/view/admin/post/edit.html index 67e37b1..7514da4 100644 --- a/view/admin/post/edit.html +++ b/view/admin/post/edit.html @@ -140,7 +140,17 @@
- +
+
分类
+
+ +
+
跳转链接
@@ -315,6 +325,7 @@ is_top: '{$post->getData("is_top")}', jump_to_url_status: '{$post->getData("jump_to_url_status")}', tpl_name: '{$post->getData("tpl_name")}', + category_id: '{$post->getData("category_id")}', }) form.on('submit(save)', function (data) { diff --git a/view/admin/post/index.html b/view/admin/post/index.html index bc40b10..c4f0147 100644 --- a/view/admin/post/index.html +++ b/view/admin/post/index.html @@ -40,9 +40,9 @@ ID - 名称 + 名称 封面 - 简介 + 简介 排序 状态 分类 @@ -62,9 +62,7 @@ {$vo.sort} {$vo.status_name} - {volist name='vo.categorys_list' id='category'} -

{:str_repeat('|--',$category.level)}{$category.title}

- {/volist} + {$vo.category.title|default="未分类"} {volist name='vo.tags_list' id='tag'} diff --git a/view/index/index/index.html b/view/index/index/index.html index db40e28..8ff586e 100644 --- a/view/index/index/index.html +++ b/view/index/index/index.html @@ -30,15 +30,25 @@