From 054b882729a84faf85dd5534602fe902c054fc29 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Fri, 21 Feb 2025 16:01:40 +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=9B=9E?= =?UTF-8?q?=E6=94=B6=E7=AB=99=E5=88=86=E9=A1=B5/=E6=81=A2=E5=A4=8D/?= =?UTF-8?q?=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 --- app/admin/controller/v1/Attachment.php | 12 ++- .../controller/v1/AttachmentCategory.php | 2 +- app/admin/controller/v1/AttachmentTrash.php | 90 +++++++++++++++++++ .../model/v1/AttachmentCategoryModel.php | 6 ++ app/admin/route/v1.php | 70 +++++++++------ app/common/model/AttachmentBaseModel.php | 4 +- .../model/AttachmentCategoryBaseModel.php | 4 +- .../20241220094223_create_attachment.php | 2 + 8 files changed, 150 insertions(+), 40 deletions(-) create mode 100644 app/admin/controller/v1/AttachmentTrash.php diff --git a/app/admin/controller/v1/Attachment.php b/app/admin/controller/v1/Attachment.php index a3cd0c1e..a4109b6c 100644 --- a/app/admin/controller/v1/Attachment.php +++ b/app/admin/controller/v1/Attachment.php @@ -83,11 +83,12 @@ class Attachment { $post = request()->post([ 'name', + 'desc', 'category_id', 'sort', 'recommend', 'image', - 'aplicable_to', + 'applicable_to', 'support_platform', 'attach', 'seo_title', @@ -96,7 +97,8 @@ class Attachment ]); $data = array_merge($post, ['language_id' => request()->lang_id]); - $validate = new AttachmentValidate; + $validate = new AttachmentValidate; + $data['attach'] = json_decode($data['attach'], true); if (!$validate->scene('create')->check($data)) { return error($validate->getError()); } @@ -116,11 +118,12 @@ class Attachment $id = request()->param('id'); $put = request()->put([ 'name', + 'desc', 'category_id', 'sort', 'recommend', 'image', - 'aplicable_to', + 'applicable_to', 'support_platform', 'attach', 'seo_title', @@ -128,7 +131,8 @@ class Attachment 'seo_desc' ]); - $validate = new AttachmentValidate; + $validate = new AttachmentValidate; + $put['attach'] = json_decode($put['attach'], true); if (!$validate->scene('update')->check(array_merge($put, ['id' => $id]))) { return error($validate->getError()); } diff --git a/app/admin/controller/v1/AttachmentCategory.php b/app/admin/controller/v1/AttachmentCategory.php index 5e02ad74..923f573c 100644 --- a/app/admin/controller/v1/AttachmentCategory.php +++ b/app/admin/controller/v1/AttachmentCategory.php @@ -76,7 +76,7 @@ class AttachmentCategory $data = array_merge($post, ['language_id' => request()->lang_id]); $validate = new AttachmentCategoryValidate; - if (!$validate->scene('create')->check($post)) { + if (!$validate->scene('create')->check($data)) { return error($validate->getError()); } diff --git a/app/admin/controller/v1/AttachmentTrash.php b/app/admin/controller/v1/AttachmentTrash.php new file mode 100644 index 00000000..b9067f90 --- /dev/null +++ b/app/admin/controller/v1/AttachmentTrash.php @@ -0,0 +1,90 @@ +param([ + 'name', + 'category_id', + 'page/d' => 1, + 'size/d' => 10 + ]); + + $attachments = AttachmentModel::field([ + 'id', + 'image', + 'name', + 'status', + 'category_id', + 'sort', + 'recommend', + 'created_at' + ]) + ->with(['category' => function ($query) { + $query->field(['id', 'name' => 'category_name']); + }]) + ->withSearch(['name'], [ + 'name' => $params['name']??null + ]) + ->language(request()->lang_id) + ->categoryId($params['category_id']??null) + ->order(['sort' => 'asc', 'id' => 'desc']) + ->onlyTrashed() + ->paginate([ + 'list_rows' => $params['size'], + 'page' => $params['page'] + ]) + ->bindAttr('category', ['category_name']) + ->hidden(['category_id', 'category']); + + return success('获取成功', $attachments); + } + + /** + * 恢复操作 + */ + public function restore() + { + $id = request()->param('id'); + + $attachment = AttachmentModel::onlyTrashed()->bypk($id)->find(); + if (empty($attachment)) { + return error("请确认操作对象是否正确"); + } + + if (!$attachment->restore()) { + return error("操作失败"); + } + return success("操作成功"); + } + + /** + * 删除操作 + */ + public function delete() + { + $id = request()->param('id'); + + $attachment = AttachmentModel::onlyTrashed()->bypk($id)->find(); + if (empty($attachment)) { + return error("请确认操作对象是否正确"); + } + + if (!$attachment->force()->delete()) { + return error("操作失败"); + } + return success("操作成功"); + } +} diff --git a/app/admin/model/v1/AttachmentCategoryModel.php b/app/admin/model/v1/AttachmentCategoryModel.php index bb4de4ac..242c6afe 100644 --- a/app/admin/model/v1/AttachmentCategoryModel.php +++ b/app/admin/model/v1/AttachmentCategoryModel.php @@ -24,4 +24,10 @@ class AttachmentCategoryModel extends AttachmentCategoryBaseModel { return $this->hasMany(AttachmentModel::class, 'category_id', 'id'); } + + // 语言查询 + public function scopeLanguage($query, $value) + { + $query->where('language_id', '=', $value); + } } diff --git a/app/admin/route/v1.php b/app/admin/route/v1.php index ad4e4afd..461c908d 100644 --- a/app/admin/route/v1.php +++ b/app/admin/route/v1.php @@ -273,47 +273,59 @@ Route::group('v1', function () { // 产品回收站删除 Route::delete('delete/:id', 'ProductTrash/delete'); }); + }); - // 附件(下载管理) - Route::group('attachment', function () { - // 附件(下载管理)列表 - Route::get('index', 'Attachment/index'); + // 附件(下载管理) + Route::group('attachment', function () { + // 附件(下载管理)列表 + Route::get('index', 'Attachment/index'); - // 附件(下载管理)详情 - Route::get('read/:id', 'Attachment/read'); + // 附件(下载管理)详情 + Route::get('read/:id', 'Attachment/read'); - // 附件(下载管理)新增 - Route::post('save', 'Attachment/save'); + // 附件(下载管理)新增 + Route::post('save', 'Attachment/save'); - // 附件(下载管理)更新 - Route::put('update/:id', 'Attachment/update'); + // 附件(下载管理)更新 + Route::put('update/:id', 'Attachment/update'); - // 附件(下载管理)设置排序值 - Route::get('sort/:id', 'Attachment/sort'); + // 附件(下载管理)设置排序值 + Route::post('sort/:id', 'Attachment/sort'); - // 附件(下载管理)删除 - Route::delete('delete/:id', 'Attachment/delete'); + // 附件(下载管理)删除 + Route::delete('delete/:id', 'Attachment/delete'); - // 附件(下载管理)分类 - Route::group('category', function () { - // 附件(下载管理)分类列表 - Route::get('index', 'AttachmentCategory/index'); + // 附件(下载管理)分类 + Route::group('category', function () { + // 附件(下载管理)分类列表 + Route::get('index', 'AttachmentCategory/index'); - // 附件(下载管理)分类详情 - Route::get('read/:id', 'AttachmentCategory/read'); + // 附件(下载管理)分类详情 + Route::get('read/:id', 'AttachmentCategory/read'); - // 附件(下载管理)分类新增 - Route::post('save', 'AttachmentCategory/save'); + // 附件(下载管理)分类新增 + Route::post('save', 'AttachmentCategory/save'); - // 附件(下载管理)分类更新 - Route::put('update/:id','AttachmentCategory/update'); + // 附件(下载管理)分类更新 + Route::put('update/:id','AttachmentCategory/update'); - // 附件(下载管理)分类设置排序值 - Route::post('sort/:id', 'AttachmentCategory/sort'); + // 附件(下载管理)分类设置排序值 + Route::post('sort/:id', 'AttachmentCategory/sort'); - // 附件(下载管理)分类删除 - Route::delete('delete/:id', 'AttachmentCategory/delete'); - }); + // 附件(下载管理)分类删除 + Route::delete('delete/:id', 'AttachmentCategory/delete'); + }); + + // 附件(下载管理)回收站 + Route::group('trash', function () { + // 附件(下载管理)回收站列表 + Route::get('index', 'AttachmentTrash/index'); + + // 附件(下载管理)回收站还原 + Route::get('restore/:id', 'AttachmentTrash/restore'); + + // 附件(下载管理)回收站删除 + Route::delete('delete/:id', 'AttachmentTrash/delete'); }); }); })->prefix('v1.'); diff --git a/app/common/model/AttachmentBaseModel.php b/app/common/model/AttachmentBaseModel.php index 0034c35c..a4cb562b 100644 --- a/app/common/model/AttachmentBaseModel.php +++ b/app/common/model/AttachmentBaseModel.php @@ -3,13 +3,11 @@ declare (strict_types = 1); namespace app\common\model; -use think\Model; - /** * 附件(下载管理)模型 * @mixin \think\Model */ -class AttachmentBaseModel extends Model +class AttachmentBaseModel extends BaseModel { // 表名 protected $name = 'attachment'; diff --git a/app/common/model/AttachmentCategoryBaseModel.php b/app/common/model/AttachmentCategoryBaseModel.php index 6be146a6..ac391064 100644 --- a/app/common/model/AttachmentCategoryBaseModel.php +++ b/app/common/model/AttachmentCategoryBaseModel.php @@ -3,13 +3,11 @@ declare (strict_types = 1); namespace app\common\model; -use think\Model; - /** * 附件(下载管理)分类模型 * @mixin \think\Model */ -class AttachmentCategoryBaseModel extends Model +class AttachmentCategoryBaseModel extends BaseModel { // 表名 protected $name = 'attachment_category'; diff --git a/database/migrations/20241220094223_create_attachment.php b/database/migrations/20241220094223_create_attachment.php index 73a0de2a..14aca3b2 100644 --- a/database/migrations/20241220094223_create_attachment.php +++ b/database/migrations/20241220094223_create_attachment.php @@ -1,5 +1,6 @@ addColumn('attach', 'json', ['null' => false, 'comment' => '附件地址: $[*].file_path为附件地址, $[*].file_ext为文件格式, $[*].btn_name为下载按钮名称']) ->addColumn('sort', 'integer', ['null' => false, 'default' => 0, 'comment' => '排序']) ->addColumn('recommend', 'boolean', ['null' => false, 'default' => 0, 'comment' => '是否推荐:1是,0否']) + ->addColumn('status', MysqlAdapter::PHINX_TYPE_TINY_INTEGER, ['null' => false, 'default' => 1, 'comment' => '状态:1为启用,-1为禁用']) ->addColumn('seo_title', 'string', ['limit' => 255, 'default' => null, 'comment' => 'SEO标题']) ->addColumn('seo_keywords', 'string', ['limit' => 255, 'default' => null, 'comment' => 'SEO关键字']) ->addColumn('seo_desc', 'string', ['limit' => 255, 'default' => null, 'comment' => 'SEO描述'])