feat: 添加横幅(分类)分页/详情/新增/更新/删除接口
This commit is contained in:
136
app/admin/controller/v1/Banner.php
Normal file
136
app/admin/controller/v1/Banner.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller\v1;
|
||||
|
||||
use app\admin\model\v1\SysBannerModel;
|
||||
use app\admin\validate\v1\SysBannerValidate;
|
||||
|
||||
/**
|
||||
* 横幅(分类)模型
|
||||
*/
|
||||
class Banner
|
||||
{
|
||||
// 分页
|
||||
public function index()
|
||||
{
|
||||
$param = request()->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('操作成功');
|
||||
}
|
||||
}
|
||||
@@ -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}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,28 +184,41 @@ Route::group('v1', function () {
|
||||
|
||||
// 横幅管理
|
||||
Route::group('banner', function() {
|
||||
|
||||
// 横幅数据项列表
|
||||
Route::group('items', function() {
|
||||
// 横幅分页
|
||||
Route::get('index', 'BannerItem/index');
|
||||
// 横幅(分类)分页
|
||||
Route::get('index', 'Banner/index');
|
||||
|
||||
// 横幅详情
|
||||
Route::get('read/:id', 'BannerItem/read');
|
||||
Route::get('read/:id', 'Banner/read');
|
||||
|
||||
// 横幅新增
|
||||
Route::post('save', 'BannerItem/save');
|
||||
Route::post('save', 'Banner/save');
|
||||
|
||||
// 横幅更新
|
||||
Route::put('update/:id', 'BannerItem/update');
|
||||
|
||||
// 设置排序值
|
||||
Route::post('sort/:id', 'BannerItem/sort');
|
||||
|
||||
// 导出
|
||||
Route::get('export', 'BannerItem/export');
|
||||
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');
|
||||
});
|
||||
});
|
||||
|
||||
76
app/admin/validate/v1/SysBannerValidate.php
Normal file
76
app/admin/validate/v1/SysBannerValidate.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class SysBannerValidate extends Validate
|
||||
{
|
||||
/**
|
||||
* 定义验证规则
|
||||
* 格式:'字段名' => ['规则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必须为空";
|
||||
}
|
||||
}
|
||||
@@ -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',
|
||||
|
||||
@@ -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' => '更新时间'])
|
||||
|
||||
Reference in New Issue
Block a user