From 89ae83f8f838742cad68986fa2d97d820f821bd9 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Wed, 12 Mar 2025 16:01:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=A8=AA=E5=B9=85?= =?UTF-8?q?=EF=BC=88=E5=88=86=E7=B1=BB=EF=BC=89=E5=88=86=E9=A1=B5/?= =?UTF-8?q?=E8=AF=A6=E6=83=85/=E6=96=B0=E5=A2=9E/=E6=9B=B4=E6=96=B0/?= =?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/Banner.php | 136 ++++++++++++++++++ app/admin/model/v1/SysBannerModel.php | 9 ++ app/admin/route/v1.php | 29 ++-- app/admin/validate/v1/SysBannerValidate.php | 76 ++++++++++ app/common/model/SysBannerBaseModel.php | 2 +- .../20241230095616_create_sys_banner.php | 7 +- 6 files changed, 247 insertions(+), 12 deletions(-) create mode 100644 app/admin/controller/v1/Banner.php create mode 100644 app/admin/validate/v1/SysBannerValidate.php diff --git a/app/admin/controller/v1/Banner.php b/app/admin/controller/v1/Banner.php new file mode 100644 index 00000000..fbe23fd9 --- /dev/null +++ b/app/admin/controller/v1/Banner.php @@ -0,0 +1,136 @@ +param([ + 'name', + 'page/d' => 1, + 'size/d' => 10 + ]); + + $banners = SysBannerModel::field([ + 'id', + 'name', + 'recommend', + 'at_platform', + 'desc', + 'created_at' + ]) + ->withSearch(['name'], [ + 'name' => $param['name'] ?? null + ]) + ->order(['sort' => 'asc', 'id' => 'desc']) + ->paginate([ + 'list_rows' => $param['size'], + 'page' => $param['page'] + ]); + + return success('获取成功', $banners); + } + + // 详情 + public function read() + { + $id = request()->param('id'); + + $banner = SysBannerModel::withoutField([ + 'at_page', + 'unique_label', + 'language_id', + 'created_at', + 'updated_at', + 'deleted_at' + ]) + ->bypk($id) + ->find(); + if (empty($banner)) { + return error('横幅(分类)不存在'); + } + + return success('获取成功', $banner); + } + + // 新增 + public function save() + { + $post = request()->post([ + 'name', + 'desc', + 'recommend', + 'unique_label' => '', + 'at_platform' => 'pc', + 'status' => 1 + ]); + $data = array_merge($post, [ + 'language_id' => request()->lang_id, + 'unique_label' => $post['unique_label'] ?? uniqid('BANNER_') + ]); + + $validate = new SysBannerValidate; + if (!$validate->scene('add')->check($data)) { + return error($validate->getError()); + } + + $banner = SysBannerModel::create($data); + if ($banner->isEmpty()) { + return error('操作失败'); + } + return success('操作成功'); + } + + // 修改 + public function update() + { + $id = request()->param('id'); + $put = request()->put([ + 'name', + 'desc', + 'recommend', + 'at_platform' => 'pc', + 'status' => 1 + ]); + + $validate = new SysBannerValidate; + if (!$validate->scene('edit')->check(array_merge($put, ['id' => $id]))) { + return error($validate->getError()); + } + + $banner = SysBannerModel::bypk($id)->find(); + if (empty($banner)) { + return error('请确认操作对象是否存在'); + } + + if (!$banner->save($put)) { + return error('操作失败'); + } + return success('操作成功'); + } + + // 删除 + public function delete() + { + $id = request()->param('id'); + + $banner = SysBannerModel::bypk($id)->find(); + if (empty($banner)) { + return error('请确认操作对象是否存在'); + } + + if (!$banner->delete()) { + return error('操作失败'); + } + return success('操作成功'); + } +} diff --git a/app/admin/model/v1/SysBannerModel.php b/app/admin/model/v1/SysBannerModel.php index db1b8e6a..1d0f6fee 100644 --- a/app/admin/model/v1/SysBannerModel.php +++ b/app/admin/model/v1/SysBannerModel.php @@ -16,4 +16,13 @@ class SysBannerModel extends SysBannerBaseModel use SoftDelete; // 软删除字段 protected $deleteTime = 'deleted_at'; + + // 按名称搜索 + public function searchNameAttr($query, $value, $data) + { + if (is_null($value)) { + return; + } + $query->where('name', 'like', "%{$value}"); + } } diff --git a/app/admin/route/v1.php b/app/admin/route/v1.php index 1f3bd207..56c78ff1 100644 --- a/app/admin/route/v1.php +++ b/app/admin/route/v1.php @@ -184,28 +184,41 @@ Route::group('v1', function () { // 横幅管理 Route::group('banner', function() { + // 横幅(分类)分页 + Route::get('index', 'Banner/index'); + + // 横幅详情 + Route::get('read/:id', 'Banner/read'); + + // 横幅新增 + Route::post('save', 'Banner/save'); + + // 横幅更新 + Route::put('update/:id', 'Banner/update'); + + // 横幅删除 + Route::delete('delete/:id', 'Banner/delete'); - // 横幅数据项列表 Route::group('items', function() { - // 横幅分页 + // 横幅数据项分页 Route::get('index', 'BannerItem/index'); - // 横幅详情 + // 横幅数据项详情 Route::get('read/:id', 'BannerItem/read'); - // 横幅新增 + // 横幅数据项新增 Route::post('save', 'BannerItem/save'); - // 横幅更新 + // 横幅数据项更新 Route::put('update/:id', 'BannerItem/update'); - // 设置排序值 + // 设置数据项排序值 Route::post('sort/:id', 'BannerItem/sort'); - // 导出 + // 横幅数据项导出 Route::get('export', 'BannerItem/export'); - // 横幅删除 + // 横幅数据项删除 Route::delete('delete/:id', 'BannerItem/delete'); }); }); diff --git a/app/admin/validate/v1/SysBannerValidate.php b/app/admin/validate/v1/SysBannerValidate.php new file mode 100644 index 00000000..b8539327 --- /dev/null +++ b/app/admin/validate/v1/SysBannerValidate.php @@ -0,0 +1,76 @@ + ['规则1','规则2'...] + * + * @var array + */ + protected $rule = [ + 'id' => 'require|integer', + 'language_id' => 'require|integer', + 'at_platform' => 'in:pc,mobile', + 'at_page' => 'max:255', + 'unique_label' => 'max:64', + 'name' => 'require|max:64', + 'desc' => 'max:255', + 'recommend' => 'in:0,1', + 'sort' => 'integer', + 'status' => 'in:-1,1' + ]; + + /** + * 定义错误信息 + * 格式:'字段名.规则名' => '错误信息' + * + * @var array + */ + protected $message = [ + 'id.require' => 'ID不能为空', + 'id.integer' => 'ID必须是整数', + 'language_id.require' => '语言ID不能为空', + 'language_id.integer' => '语言ID必须是整数', + 'at_platform.in' => '显示端口只能是pc或mobile', + 'at_page.max' => '页面位置最多255个字符', + 'unique_label.max' => '唯一标识最多64个字符', + 'name.require' => '名称不能为空', + 'name.max' => '名称最多64个字符', + 'desc.max' => '描述最多255个字符', + 'recommend.in' => '推荐只能是0或1', + 'sort.integer' => '排序必须是整数', + 'status.in' => '状态只能是-1或1' + ]; + + // 新增场景 + public function sceneAdd() + { + return $this->remove('id', 'require|integer'); + } + + // 编辑场景 + public function sceneEdit() + { + return $this->remove('language_id', 'require|integer')->append('unique_label', 'empty'); + } + + // 验证是否为空 + private function empty($value, $rule, $data) + { + if (!isset($data['unique_label'])) { + return true; + } + + if (is_null($value) || $value == '') { + return true; + } + + return "unique_label必须为空"; + } +} diff --git a/app/common/model/SysBannerBaseModel.php b/app/common/model/SysBannerBaseModel.php index 157c59a9..50822607 100644 --- a/app/common/model/SysBannerBaseModel.php +++ b/app/common/model/SysBannerBaseModel.php @@ -21,11 +21,11 @@ class SysBannerBaseModel extends BaseModel 'language_id' => 'int', 'at_platform' => 'string', 'at_page' => 'string', - 'at_position' => 'int', 'unique_label' => 'string', 'name' => 'string', 'desc' => 'string', 'sort' => 'sort', + 'recommend' => 'int', 'status' => 'int', 'created_at' => 'datetime', 'updated_at' => 'datetime', diff --git a/database/migrations/20241230095616_create_sys_banner.php b/database/migrations/20241230095616_create_sys_banner.php index 76cae558..64bdfad9 100644 --- a/database/migrations/20241230095616_create_sys_banner.php +++ b/database/migrations/20241230095616_create_sys_banner.php @@ -30,12 +30,13 @@ class CreateSysBanner extends Migrator $table = $this->table('sys_banner', ['engine' => 'InnoDB', 'comment' => '系统Banner表']); $table->addColumn('language_id', 'integer', ['null' => false, 'comment' => '语言ID']) ->addColumn('at_platform', 'string', ['limit' => 8, 'null' => false, 'comment' => '所在平台: pc为电脑, mobile为手机']) - ->addColumn('at_page', 'string', ['limit' => 255, 'null' => false, 'comment' => '所在页面']) - ->addColumn('at_position', 'string', ['limit' => 255, 'null' => false, 'comment' => '所在位置: top为顶部, footer为底部, left为左侧, right为右侧, center为中间']) + ->addColumn('at_page', 'string', ['limit' => 255, 'null' => true, 'comment' => '所在页面']) + ->addColumn('at_position', 'string', ['limit' => 255, 'null' => true, 'comment' => '所在位置: top为顶部, footer为底部, left为左侧, right为右侧, center为中间']) ->addColumn('unique_label', 'string', ['limit' => 64, 'null' => false, 'comment' => 'banner唯一标识']) ->addColumn('name', 'string', ['limit' => 64, 'null' => false, 'comment' => 'banner名称']) - ->addColumn('desc', 'string', ['limit' => 255, 'null' => false, 'comment' => 'banner描述']) + ->addColumn('desc', 'string', ['limit' => 255, 'null' => false, 'default' => '', 'comment' => 'banner描述']) ->addColumn('sort', 'integer', ['limit' => 11, 'null' => false, 'default' => 0, 'comment' => '排序']) + ->addColumn('recommend', 'boolean', ['limit' => 1, 'null' => false, 'default' => 0, 'comment' => '是否推荐: 1为是, 0为否']) ->addColumn('status', 'boolean', ['limit' => 1, 'null' => false, 'default' => 1, 'comment' => '-1为禁用, 1为启用']) ->addColumn('created_at', 'timestamp', ['null' => false, 'default' =>'CURRENT_TIMESTAMP', 'comment' => '创建时间']) ->addColumn('updated_at', 'timestamp', ['null' => false, 'default' =>'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', 'comment' => '更新时间'])