mirror of
https://gitee.com/ulthon/layui-ul.git
synced 2026-07-06 20:32:49 +08:00
完成文档分类
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
|
||||
use think\facade\Route;
|
||||
|
||||
Route::rule('doc/[:post_id]','Index/index');
|
||||
Route::rule('doc/[:category_id]/[:post_id]','Index/index');
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user