feat: 添加横幅相关接口
This commit is contained in:
221
app/admin/controller/v1/Banner.php
Normal file
221
app/admin/controller/v1/Banner.php
Normal file
@@ -0,0 +1,221 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\admin\controller\v1;
|
||||||
|
|
||||||
|
use app\admin\model\v1\SysBannerItemModel;
|
||||||
|
use app\admin\validate\v1\SysBannerItemValidate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 横幅控制器
|
||||||
|
*/
|
||||||
|
class Banner
|
||||||
|
{
|
||||||
|
// 分页
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$param = request()->param([
|
||||||
|
'title',
|
||||||
|
'banner_id',
|
||||||
|
'created_at',
|
||||||
|
'page/d' => 1,
|
||||||
|
'size/d' => 10
|
||||||
|
]);
|
||||||
|
|
||||||
|
$banners = SysBannerItemModel::field([
|
||||||
|
'id',
|
||||||
|
'banner_id',
|
||||||
|
'title',
|
||||||
|
'sort',
|
||||||
|
'created_at'
|
||||||
|
])
|
||||||
|
->with(['banner' => function ($query) {
|
||||||
|
$query->field(['id', 'name' => 'banner_name']);
|
||||||
|
}])
|
||||||
|
->withSearch(['title', 'created_at'], [
|
||||||
|
'title' => $param['title'] ?? null,
|
||||||
|
'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null
|
||||||
|
])
|
||||||
|
->bannerId($param['banner_id'] ?? null)
|
||||||
|
->paginate([
|
||||||
|
'list_rows' => $param['size'] ?? 10,
|
||||||
|
'page' => $param['page'] ?? 1
|
||||||
|
])
|
||||||
|
->bindAttr('banner', ['banner_name'])
|
||||||
|
->hidden(['banner_id', 'banner']);
|
||||||
|
|
||||||
|
return success('获取成功', $banners);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 详情
|
||||||
|
public function read()
|
||||||
|
{
|
||||||
|
$id = request()->param('id');
|
||||||
|
|
||||||
|
$banner = SysBannerItemModel::withoutField([
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
'deleted_at'
|
||||||
|
])
|
||||||
|
->find($id);
|
||||||
|
if (empty($banner)) {
|
||||||
|
return error('横幅不存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
return success('获取成功', $banner);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 新增
|
||||||
|
public function save()
|
||||||
|
{
|
||||||
|
$post = request()->post([
|
||||||
|
'banner_id',
|
||||||
|
'title',
|
||||||
|
'title_txt_color',
|
||||||
|
'desc',
|
||||||
|
'desc_txt_color',
|
||||||
|
'type',
|
||||||
|
'image',
|
||||||
|
'video',
|
||||||
|
'link_to',
|
||||||
|
'link',
|
||||||
|
'sort',
|
||||||
|
'status'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$validate = new SysBannerItemValidate;
|
||||||
|
if (!$validate->scene('add')->check($post)) {
|
||||||
|
return error($validate->getError());
|
||||||
|
}
|
||||||
|
|
||||||
|
$banner = SysBannerItemModel::create($post);
|
||||||
|
if ($banner->isEmpty()) {
|
||||||
|
return error('操作失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
return success('操作成功');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改
|
||||||
|
public function update()
|
||||||
|
{
|
||||||
|
$id = request()->param('id');
|
||||||
|
$put = request()->put([
|
||||||
|
'banner_id',
|
||||||
|
'title',
|
||||||
|
'title_txt_color',
|
||||||
|
'desc',
|
||||||
|
'desc_txt_color',
|
||||||
|
'type',
|
||||||
|
'image',
|
||||||
|
'video',
|
||||||
|
'link_to',
|
||||||
|
'link',
|
||||||
|
'sort',
|
||||||
|
'status'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$validate = new SysBannerItemValidate;
|
||||||
|
if (!$validate->check(array_merge($put, ['id' => $id]))) {
|
||||||
|
return error($validate->getError());
|
||||||
|
}
|
||||||
|
|
||||||
|
$banner = SysBannerItemModel::bypk($id)->find();
|
||||||
|
if (empty($banner)) {
|
||||||
|
return error('请确认操作对象是否存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$banner->save($put)) {
|
||||||
|
return error('操作失败');
|
||||||
|
}
|
||||||
|
return success('操作成功');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置排序值
|
||||||
|
public function sort()
|
||||||
|
{
|
||||||
|
$id = request()->param('id');
|
||||||
|
$sort = request()->param('sort');
|
||||||
|
|
||||||
|
$banner = SysBannerItemModel::bypk($id)->find();
|
||||||
|
if (empty($banner)) {
|
||||||
|
return error('请确认操作对象是否存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($banner->sort != $sort) {
|
||||||
|
$banner->sort = $sort;
|
||||||
|
if (!$banner->save()) {
|
||||||
|
return error('操作失败');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return success('操作成功');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 导出
|
||||||
|
public function export()
|
||||||
|
{
|
||||||
|
$schema = [
|
||||||
|
'id' => 'ID',
|
||||||
|
'banner_name' => '分类名称',
|
||||||
|
'title' => '横幅名称',
|
||||||
|
'title_txt_color' => '横幅名称字体颜色',
|
||||||
|
'desc' => '描述',
|
||||||
|
'desc_txt_color' => '描述字体颜色',
|
||||||
|
'type' => '前台显示类型',
|
||||||
|
'image' => '图片地址',
|
||||||
|
'video' => '视频地址',
|
||||||
|
'link_to' => '链接类型',
|
||||||
|
'link' => '链接地址',
|
||||||
|
'sort' => '排序值',
|
||||||
|
'status' => '状态',
|
||||||
|
'created_at' => '添加时间'
|
||||||
|
];
|
||||||
|
|
||||||
|
// 获取要导出的横幅项数据
|
||||||
|
$banners = $this->getBannerExportData();
|
||||||
|
|
||||||
|
// 导出
|
||||||
|
return xlsx_writer($banners, $schema);
|
||||||
|
}
|
||||||
|
// 获取导出数据
|
||||||
|
private function getBannerExportData()
|
||||||
|
{
|
||||||
|
$param = request()->param([
|
||||||
|
'title',
|
||||||
|
'banner_id',
|
||||||
|
'created_at'
|
||||||
|
]);
|
||||||
|
return SysBannerItemModel::withoutField([
|
||||||
|
'updated_at',
|
||||||
|
'deleted_at'
|
||||||
|
])
|
||||||
|
->with(['banner' => function ($query) {
|
||||||
|
$query->field(['id', 'name' => 'banner_name']);
|
||||||
|
}])
|
||||||
|
->withSearch(['title', 'created_at'], [
|
||||||
|
'title' => $param['title'] ?? null,
|
||||||
|
'created_at' => !empty($param['created_att']) ? explode(',', $param['created_att']) : null,
|
||||||
|
])
|
||||||
|
->bannerId($param['banner_id'] ?? null)
|
||||||
|
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||||
|
->select()
|
||||||
|
->bindAtt('banner', ['banner_name'])
|
||||||
|
->hidden(['banner_id', 'banner']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除
|
||||||
|
public function delete()
|
||||||
|
{
|
||||||
|
$id = request()->param('id');
|
||||||
|
|
||||||
|
$banner = SysBannerItemModel::bypk($id)->find();
|
||||||
|
if (empty($banner)) {
|
||||||
|
return error('请确认操作对象是否存在');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$banner->delete()) {
|
||||||
|
return error('操作失败');
|
||||||
|
}
|
||||||
|
return success('操作成功');
|
||||||
|
}
|
||||||
|
}
|
||||||
56
app/admin/model/v1/SysBannerItemModel.php
Normal file
56
app/admin/model/v1/SysBannerItemModel.php
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\admin\model\v1;
|
||||||
|
|
||||||
|
use app\common\model\SysBannerItemBaseModel;
|
||||||
|
use think\model\concern\SoftDelete;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 横幅数据项模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class SysBannerItemModel extends SysBannerItemBaseModel
|
||||||
|
{
|
||||||
|
// 启用软删除
|
||||||
|
use SoftDelete;
|
||||||
|
// 软删除字段
|
||||||
|
protected $deleteTime = 'deleted_at';
|
||||||
|
|
||||||
|
// 关联分类
|
||||||
|
public function banner()
|
||||||
|
{
|
||||||
|
return $this->belongsTo(SysBannerModel::class, 'banner_id', 'id');
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按横幅标题搜索
|
||||||
|
public function searchTitleAttr($query, $value, $data)
|
||||||
|
{
|
||||||
|
if (empty($value)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$query->where('title', 'like', "%{$value}%");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按添加时间搜索
|
||||||
|
public function searchCreatedAtAttr($query, $value, $data)
|
||||||
|
{
|
||||||
|
if (empty($value)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (is_array($value)) {
|
||||||
|
if (count($value) > 1) {
|
||||||
|
$query->whereBetweenTime ('created_at', $value[0], $value[1]);
|
||||||
|
} else {
|
||||||
|
$query->whereTime('created_at', '>=', $value[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 根据横幅id查询
|
||||||
|
public function scopeBannerId($query, $value)
|
||||||
|
{
|
||||||
|
if (is_null($value)) return;
|
||||||
|
$query->where('banner_id', '=', $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
19
app/admin/model/v1/SysBannerModel.php
Normal file
19
app/admin/model/v1/SysBannerModel.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\admin\model\v1;
|
||||||
|
|
||||||
|
use app\common\model\SysBannerBaseModel;
|
||||||
|
use think\model\concern\SoftDelete;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 横幅模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class SysBannerModel extends SysBannerBaseModel
|
||||||
|
{
|
||||||
|
// 启用软删除
|
||||||
|
use SoftDelete;
|
||||||
|
// 软删除字段
|
||||||
|
protected $deleteTime = 'deleted_at';
|
||||||
|
}
|
||||||
@@ -182,6 +182,30 @@ 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::post('sort/:id', 'Banner/sort');
|
||||||
|
|
||||||
|
// 导出
|
||||||
|
Route::get('export', 'Banner/export');
|
||||||
|
|
||||||
|
// 横幅删除
|
||||||
|
Route::delete('delete/:id', 'Banner/delete');
|
||||||
|
});
|
||||||
|
|
||||||
// 产品模块
|
// 产品模块
|
||||||
Route::group('product', function () {
|
Route::group('product', function () {
|
||||||
// 产品分页列表
|
// 产品分页列表
|
||||||
|
|||||||
63
app/admin/validate/v1/SysBannerItemValidate.php
Normal file
63
app/admin/validate/v1/SysBannerItemValidate.php
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\admin\validate\v1;
|
||||||
|
|
||||||
|
use think\Validate;
|
||||||
|
|
||||||
|
class SysBannerItemValidate extends Validate
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 定义验证规则
|
||||||
|
* 格式:'字段名' => ['规则1','规则2'...]
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $rule = [
|
||||||
|
'id' => 'require|integer',
|
||||||
|
'banner_id' => 'require|integer',
|
||||||
|
'title' => 'require|max:128',
|
||||||
|
'title_txt_color' => 'max:7',
|
||||||
|
'desc' => 'max:255',
|
||||||
|
'desc_txt_color' => 'max:7',
|
||||||
|
'type' => 'in:image,video',
|
||||||
|
'image' => 'max:255',
|
||||||
|
'video' => 'max:255',
|
||||||
|
'link_to' => 'max:64|in:article,goods_category,goods,custom',
|
||||||
|
'link' => 'max:255',
|
||||||
|
'sort' => 'integer',
|
||||||
|
'status' => 'in:-1,1'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 定义错误信息
|
||||||
|
* 格式:'字段名.规则名' => '错误信息'
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $message = [
|
||||||
|
'id.require' => 'ID不能为空',
|
||||||
|
'id.integer' => 'ID必须是整数',
|
||||||
|
'banner_id.require' => '横幅项分类不能为空',
|
||||||
|
'banner_id.integer' => '横幅项分类必须是整数',
|
||||||
|
'title.require' => '名称不能为空',
|
||||||
|
'title.max' => '名称最多不能超过128个字符',
|
||||||
|
'title_txt_color.max' => '名称字体颜色最多不能超过7个字符',
|
||||||
|
'desc.max' => '描述最多不能超过255个字符',
|
||||||
|
'desc_txt_color.max' => '描述字体颜色最多不能超过7个字符',
|
||||||
|
'type.in' => '显示类型必须是image或video',
|
||||||
|
'image.max' => '图片地址最多不能超过255个字符',
|
||||||
|
'video.max' => '视频地址最多不能超过255个字符',
|
||||||
|
'link_to.max' => '连接类型最多不能超过64个字符',
|
||||||
|
'link_to.in' => '连接类型必须是article,goods_category,goods,custom中之一',
|
||||||
|
'link.max' => '连接最多不能超过255个字符',
|
||||||
|
'sort.integer' => '排序值必须是整数',
|
||||||
|
'status.in' => '状态必须是-1或1'
|
||||||
|
];
|
||||||
|
|
||||||
|
// 新增场景
|
||||||
|
public function sceneAdd()
|
||||||
|
{
|
||||||
|
return $this->remove('id', 'require|integer');
|
||||||
|
}
|
||||||
|
}
|
||||||
34
app/common/model/SysBannerBaseModel.php
Normal file
34
app/common/model/SysBannerBaseModel.php
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\common\model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 横幅模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class SysBannerBaseModel extends BaseModel
|
||||||
|
{
|
||||||
|
// 表名
|
||||||
|
protected $name = 'sys_banner';
|
||||||
|
|
||||||
|
// 主键
|
||||||
|
protected $pk = 'id';
|
||||||
|
|
||||||
|
// 字段信息
|
||||||
|
protected $schema = [
|
||||||
|
'id' => 'int',
|
||||||
|
'language_id' => 'int',
|
||||||
|
'at_platform' => 'string',
|
||||||
|
'at_page' => 'string',
|
||||||
|
'at_position' => 'int',
|
||||||
|
'unique_label' => 'string',
|
||||||
|
'name' => 'string',
|
||||||
|
'desc' => 'string',
|
||||||
|
'sort' => 'sort',
|
||||||
|
'status' => 'int',
|
||||||
|
'created_at' => 'datetime',
|
||||||
|
'updated_at' => 'datetime',
|
||||||
|
'deleted_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
37
app/common/model/SysBannerItemBaseModel.php
Normal file
37
app/common/model/SysBannerItemBaseModel.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
declare (strict_types = 1);
|
||||||
|
|
||||||
|
namespace app\common\model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 横幅数据项模型
|
||||||
|
* @mixin \think\Model
|
||||||
|
*/
|
||||||
|
class SysBannerItemBaseModel extends BaseModel
|
||||||
|
{
|
||||||
|
// 表名
|
||||||
|
protected $name = 'sys_banner_item';
|
||||||
|
|
||||||
|
// 主键
|
||||||
|
protected $pk = 'id';
|
||||||
|
|
||||||
|
// 字段信息
|
||||||
|
protected $schema = [
|
||||||
|
'id' => 'int',
|
||||||
|
'banner_id' => 'int',
|
||||||
|
'title' => 'string',
|
||||||
|
'title_txt_color' => 'string',
|
||||||
|
'desc' => 'string',
|
||||||
|
'desc_txt_color' => 'string',
|
||||||
|
'type' => 'int',
|
||||||
|
'image' => 'string',
|
||||||
|
'video' => 'string',
|
||||||
|
'link_to' => 'string',
|
||||||
|
'link' => 'string',
|
||||||
|
'sort' => 'int',
|
||||||
|
'status' => 'int',
|
||||||
|
'created_at' => 'datetime',
|
||||||
|
'updated_at' => 'datetime',
|
||||||
|
'deleted_at' => 'datetime'
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -36,7 +36,7 @@ class CreateSysBannerItem extends Migrator
|
|||||||
->addColumn('type', 'string', ['limit' => 16, 'null' => false, 'comment' => '类型: image为图片, video为视频'])
|
->addColumn('type', 'string', ['limit' => 16, 'null' => false, 'comment' => '类型: image为图片, video为视频'])
|
||||||
->addColumn('image', 'string', ['limit' => 255, 'null' => false, 'comment' => '图片'])
|
->addColumn('image', 'string', ['limit' => 255, 'null' => false, 'comment' => '图片'])
|
||||||
->addColumn('video', 'string', ['limit' => 255, 'null' => false, 'comment' => '视频'])
|
->addColumn('video', 'string', ['limit' => 255, 'null' => false, 'comment' => '视频'])
|
||||||
->addColumn('link_to', 'string', ['limit' => 64, 'null' => false, 'comment' => '链接到(类型): article:文章, goods_category:商品分类, goods:商品, link:自定义链接'])
|
->addColumn('link_to', 'string', ['limit' => 64, 'null' => false, 'comment' => '链接到(类型): article:文章, goods_category:商品分类, goods:商品, custom:自定义链接'])
|
||||||
->addColumn('link', 'string', ['limit' => 255, 'null' => false, 'comment' => '链接'])
|
->addColumn('link', 'string', ['limit' => 255, 'null' => false, 'comment' => '链接'])
|
||||||
->addColumn('sort', 'integer', ['limit' => 11, 'null' => false, 'default' => 0, 'comment' => '排序'])
|
->addColumn('sort', 'integer', ['limit' => 11, 'null' => false, 'default' => 0, 'comment' => '排序'])
|
||||||
->addColumn('status', 'boolean', ['limit' => 1, 'null' => false, 'default' => 1, 'comment' => '-1为禁用, 1为启用'])
|
->addColumn('status', 'boolean', ['limit' => 1, 'null' => false, 'default' => 1, 'comment' => '-1为禁用, 1为启用'])
|
||||||
|
|||||||
Reference in New Issue
Block a user