diff --git a/plugs/think-plugs-cms/src/controller/Category.php b/plugs/think-plugs-cms/src/controller/Category.php index 66294ba..deb580b 100644 --- a/plugs/think-plugs-cms/src/controller/Category.php +++ b/plugs/think-plugs-cms/src/controller/Category.php @@ -24,7 +24,7 @@ class Category extends Controller $this->title = '栏目列表'; CmsCategory::mQuery()->layTable(function () { }, static function (QueryHelper $query) { - $query->equal('status')->like('name'); + $query->equal(['status', 'pid'])->like('name'); }); } @@ -38,6 +38,7 @@ class Category extends Controller public function add() { $this->title = '添加栏目'; + $this->pid = $this->request->param('pid', 1, 'intval'); CmsCategory::mForm('form'); } @@ -52,6 +53,7 @@ class Category extends Controller { $this->title = '编辑栏目'; $this->id = $this->request->param('id', 0, 'intval'); //当前栏目ID + $this->pid = $this->request->param('pid', 1, 'intval'); CmsCategory::mForm('form'); } @@ -78,6 +80,9 @@ class Category extends Controller if ($articles_count > 0) { $this->error("该栏目下有文章,不能删除!"); } + if ($id <= 2) { + $this->error("该栏目为根栏目,不能删除!"); + } return true; } diff --git a/plugs/think-plugs-cms/src/controller/Tutorial.php b/plugs/think-plugs-cms/src/controller/Tutorial.php index ae411b6..9f55601 100644 --- a/plugs/think-plugs-cms/src/controller/Tutorial.php +++ b/plugs/think-plugs-cms/src/controller/Tutorial.php @@ -2,6 +2,7 @@ namespace plugin\cms\controller; +use plugin\cms\model\CmsCategory; use plugin\cms\model\CmsTutorial; use think\admin\Controller; use think\admin\helper\QueryHelper; @@ -30,23 +31,25 @@ class Tutorial extends Controller /** * 添加办事指南 - * @return void */ public function add() { $this->title = '添加办事指南'; + $this->cates = CmsCategory::query()->where("pid", "=", 1)->select(); // 获取分类列表 + $this->cate_ids = []; CmsTutorial::mForm('form'); } /** * 编辑办事指南 - * @return void */ public function edit() { $this->title = '编辑办事指南'; $this->id = $this->request->param('id'); - CmsTutorial::mForm('form'); + $this->cates = CmsCategory::query()->where("pid", "=", 1)->select(); // 获取分类列表 + $this->cate_ids = CmsTutorial::find($this->id)->category_ids; // 获取当前分类ID + CmsTutorial::mForm('form', "id"); } /** @@ -84,4 +87,18 @@ class Tutorial extends Controller 'sort.between:0,9999' => '排序值必须为0~9999之间!', ]), 'id'); } + + /** + * 表单数据过滤 + */ + protected function _form_result(bool $state, array $post) + { + if ($this->request->isPost()) { + // 处理分类数据 + $tutorial = CmsTutorial::find($post['id']); + if ($state) { + $tutorial->category_ids = input('post.category_ids/a', []);; + } + } + } } \ No newline at end of file diff --git a/plugs/think-plugs-cms/src/model/CmsTutorial.php b/plugs/think-plugs-cms/src/model/CmsTutorial.php index 3ed08d5..6c63fa2 100644 --- a/plugs/think-plugs-cms/src/model/CmsTutorial.php +++ b/plugs/think-plugs-cms/src/model/CmsTutorial.php @@ -6,5 +6,31 @@ use think\admin\Model; class CmsTutorial extends Model { + /** + * 定义与分类的多对多关系 + */ + public function categories() + { + return $this->belongsToMany(CmsCategory::class, CmsTutorialCategory::class, 'cid', 'tid'); + } + /** + * 获取分类ID列表 + */ + public function getCategoryIdsAttr() + { + return $this->categories()->column('id'); + } + + public function setCategoryIdsAttr($value) + { + if ($this->isExists()) { + $this->categories()->detach(); + if (is_array($value)) { + foreach ($value as $cid) { + $this->categories()->attach($cid); + } + } + } + } } \ No newline at end of file diff --git a/plugs/think-plugs-cms/src/model/CmsTutorialCategory.php b/plugs/think-plugs-cms/src/model/CmsTutorialCategory.php new file mode 100644 index 0000000..925d31b --- /dev/null +++ b/plugs/think-plugs-cms/src/model/CmsTutorialCategory.php @@ -0,0 +1,24 @@ +belongsTo(CmsCategory::class, 'cid', 'id'); + } + + /** + * 关联办事指南模型 + */ + public function tutorial() + { + return $this->belongsTo(CmsTutorial::class, 'tid', 'id'); + } +} \ No newline at end of file diff --git a/plugs/think-plugs-cms/src/view/category/form.html b/plugs/think-plugs-cms/src/view/category/form.html index abe7dd6..3474558 100644 --- a/plugs/think-plugs-cms/src/view/category/form.html +++ b/plugs/think-plugs-cms/src/view/category/form.html @@ -9,6 +9,7 @@ --> +