From d9e11cd9f518f01c1238cf0dd820fa30344f51e5 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Fri, 21 Feb 2025 15:49:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=99=84=E4=BB=B6?= =?UTF-8?q?=EF=BC=88=E4=B8=8B=E8=BD=BD=E7=AE=A1=E7=90=86=EF=BC=89=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E5=88=86=E9=A1=B5/=E8=AF=A6=E6=83=85/=E6=96=B0?= =?UTF-8?q?=E5=A2=9E/=E6=9B=B4=E6=96=B0/=E8=AE=BE=E7=BD=AE=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E5=80=BC/=E5=88=A0=E9=99=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/v1/AttachmentCategory.php | 165 ++++++++++++++++++ .../model/v1/AttachmentCategoryModel.php | 27 +++ app/admin/route/v1.php | 21 +++ .../v1/AttachmentCategoryValidate.php | 64 +++++++ .../model/AttachmentCategoryBaseModel.php | 31 ++++ 5 files changed, 308 insertions(+) create mode 100644 app/admin/controller/v1/AttachmentCategory.php create mode 100644 app/admin/model/v1/AttachmentCategoryModel.php create mode 100644 app/admin/validate/v1/AttachmentCategoryValidate.php create mode 100644 app/common/model/AttachmentCategoryBaseModel.php diff --git a/app/admin/controller/v1/AttachmentCategory.php b/app/admin/controller/v1/AttachmentCategory.php new file mode 100644 index 00000000..5e02ad74 --- /dev/null +++ b/app/admin/controller/v1/AttachmentCategory.php @@ -0,0 +1,165 @@ +param([ + 'name', + 'page/d' => 1, + 'size/d' => 10 + ]); + + $categorys = AttachmentCategoryModel::field([ + 'id', + 'name', + 'sort', + 'is_show' + ]) + ->withSearch(['name'], [ + 'name' => $params['name']??null + ]) + ->language(request()->lang_id) + ->order(['sort' => 'asc', 'id' => 'desc']) + ->paginate([ + 'list_rows' => $params['size'], + 'page' => $params['page'] + ]); + + return success('获取成功', $categorys); + } + + /** + * 附件分类详情 + */ + public function read() + { + $id = request()->param('id'); + + $category = AttachmentCategoryModel::withoutField([ + 'language_id', + 'created_at', + 'updated_at', + 'deleted_at' + ]) + ->bypk($id) + ->find(); + if (empty($category)) { + return error('分类不存在'); + } + + return success('获取成功', $category); + } + + /** + * 附件分类添加 + */ + public function save() + { + $post = request()->post([ + 'name', + 'sort', + 'is_show' + ]); + $data = array_merge($post, ['language_id' => request()->lang_id]); + + $validate = new AttachmentCategoryValidate; + if (!$validate->scene('create')->check($post)) { + return error($validate->getError()); + } + + $category = AttachmentCategoryModel::create($data); + if ($category->isEmpty()) { + return error('操作失败'); + } + return success('操作成功'); + } + + /** + * 附件分类更新 + */ + public function update() + { + $id = request()->param('id'); + $put = request()->put([ + 'name', + 'sort', + 'is_show' + ]); + + $validate = new AttachmentCategoryValidate; + if (!$validate->scene('update')->check(array_merge($put, ['id' => $id]))) { + return error($validate->getError()); + } + + $category = AttachmentCategoryModel::bypk($id)->find(); + if (empty($category)) { + return error('请确认操作对象是否存在'); + } + + if (!$category->save($put)) { + return error('操作失败'); + } + return success('操作成功'); + } + + /** + * 设置排序值 + */ + public function sort() + { + $id = request()->param('id'); + $sort = request()->param('sort'); + + $validate = new AttachmentCategoryValidate; + if (!$validate->scene('sort')->check(['sort' => $sort])) { + return error($validate->getError()); + } + + $category = AttachmentCategoryModel::bypk($id)->find(); + if (empty($category)) { + return error('请确认操作对象是否存在'); + } + + $category->sort = $sort; + if (!$category->save()) { + return error('操作失败'); + } + return success('操作成功'); + } + + /** + * 附件分类删除 + */ + public function delete() + { + $id = request()->param('id'); + + $category = AttachmentCategoryModel::bypk($id)->find(); + if (empty($category)) { + return error('请确认操作对象是否存在'); + } + + // 检查分类下是否存在附件 + if (!$category->attachment()->count()) { + return error('分类下存在附件,请先删除附件'); + } + + if (!$category->delete()) { + return error('操作失败'); + } + return success('操作成功'); + } +} diff --git a/app/admin/model/v1/AttachmentCategoryModel.php b/app/admin/model/v1/AttachmentCategoryModel.php new file mode 100644 index 00000000..bb4de4ac --- /dev/null +++ b/app/admin/model/v1/AttachmentCategoryModel.php @@ -0,0 +1,27 @@ +hasMany(AttachmentModel::class, 'category_id', 'id'); + } +} diff --git a/app/admin/route/v1.php b/app/admin/route/v1.php index 31a6f0de..ad4e4afd 100644 --- a/app/admin/route/v1.php +++ b/app/admin/route/v1.php @@ -293,6 +293,27 @@ Route::group('v1', function () { // 附件(下载管理)删除 Route::delete('delete/:id', 'Attachment/delete'); + + // 附件(下载管理)分类 + Route::group('category', function () { + // 附件(下载管理)分类列表 + Route::get('index', 'AttachmentCategory/index'); + + // 附件(下载管理)分类详情 + Route::get('read/:id', 'AttachmentCategory/read'); + + // 附件(下载管理)分类新增 + Route::post('save', 'AttachmentCategory/save'); + + // 附件(下载管理)分类更新 + Route::put('update/:id','AttachmentCategory/update'); + + // 附件(下载管理)分类设置排序值 + Route::post('sort/:id', 'AttachmentCategory/sort'); + + // 附件(下载管理)分类删除 + Route::delete('delete/:id', 'AttachmentCategory/delete'); + }); }); }); })->prefix('v1.'); diff --git a/app/admin/validate/v1/AttachmentCategoryValidate.php b/app/admin/validate/v1/AttachmentCategoryValidate.php new file mode 100644 index 00000000..5f2af9ac --- /dev/null +++ b/app/admin/validate/v1/AttachmentCategoryValidate.php @@ -0,0 +1,64 @@ + ['规则1','规则2'...] + * + * @var array + */ + protected $rule = [ + 'id' => 'require|integer', + 'language_id' => 'require|integer', + 'name' => 'require|max:64', + 'sort' => 'integer', + 'is_show' => 'in:0,1', + ]; + + /** + * 定义错误信息 + * 格式:'字段名.规则名' => '错误信息' + * + * @var array + */ + protected $message = [ + 'id.require' => 'ID不能为空', + 'id.integer' => 'ID必须是整数', + 'language_id.require' => '语言ID不能为空', + 'language_id.integer' => '语言ID必须是整数', + 'name.require' => '名称不能为空', + 'name.max' => '名称不能超过64个字符', + 'sort.integer' => '排序必须是整数', + 'is_show.in' => '是否显示必须是0或1', + ]; + + /** + * 新增场景 + */ + public function sceneCreate() + { + return $this->remove('id', 'require|integer'); + } + + /** + * 更新场景 + */ + public function sceneUpdate() + { + return $this->remove('language_id', 'require|integer'); + } + + /** + * 排序场景 + */ + public function sceneSort() + { + return $this->only(['sort']); + } +} diff --git a/app/common/model/AttachmentCategoryBaseModel.php b/app/common/model/AttachmentCategoryBaseModel.php new file mode 100644 index 00000000..6be146a6 --- /dev/null +++ b/app/common/model/AttachmentCategoryBaseModel.php @@ -0,0 +1,31 @@ + 'int', + 'language_id' => 'int', + 'name' => 'string', + 'sort' => 'int', + 'is_show' => 'int', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + 'deleted_at' => 'datetime', + ]; +}