feat: 添加新增/更新/设置排序值/删除接口

This commit is contained in:
2025-02-20 14:13:30 +08:00
parent 9b60efd6f2
commit a93d44cbaa
6 changed files with 226 additions and 6 deletions

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace app\admin\controller\v1; namespace app\admin\controller\v1;
use app\admin\model\v1\VideoModel; use app\admin\model\v1\VideoModel;
use app\admin\validate\v1\VideoValidate;
/** /**
* 视频管理控制器 * 视频管理控制器
@@ -69,4 +70,123 @@ class Video
return success('获取成功', $video); return success('获取成功', $video);
} }
/**
* 添加视频信息
*/
public function save()
{
$post = request()->post([
'name',
'category_id',
'sort',
'recommend',
'desc',
'image',
'video',
'link',
'status' => 1,
'seo_title',
'seo_keywords',
'seo_desc'
]);
$data = array_merge($post, ['language_id' => request()->lang_id]);
$validate = new VideoValidate;
if (!$validate->scene('create')->check($data)) {
return error($validate->getError());
}
$video = VideoModel::create($data);
if ($video->isEmpty()) {
return error('操作失败');
}
return success('操作成功');
}
/**
* 更新视频信息
*/
public function update()
{
$id = request()->param('id');
$put = request()->put([
'name',
'category_id',
'sort',
'recommend',
'desc',
'image',
'video',
'link',
'status' => 1,
'seo_title',
'seo_keywords',
'seo_desc'
]);
$validate = new VideoValidate;
if (!$validate->scene('update')->check(array_merge($put, ['id' => $id]))) {
return error($validate->getError());
}
$video = VideoModel::bypk($id)->find();
if (empty($video)) {
return error('请确认操作对象是不是存在');
}
if (!$video->save($put)) {
return error('操作失败');
}
return success('操作成功');
}
/**
* 设置排序值
*/
public function sort()
{
$id = request()->param('id');
$sort = request()->post('sort');
$validate = new VideoValidate;
if (!$validate->scene('sort')->check(['id' => $id, 'sort' => $sort])) {
return error($validate->getError());
}
$video = VideoModel::bypk($id)->find();
if (empty($video)) {
return error('请确认操作对象是不是存在');
}
$video->sort = $sort;
if (!$video->save()) {
return error('操作失败');
}
return success('操作成功');
}
/**
* 视频信息删除
*/
public function delete()
{
$id = request()->param('id');
$validate = new VideoValidate;
if (!$validate->scene('delete')->check(['id' => $id])) {
return error($validate->getError());
}
$video = VideoModel::bypk($id)->find();
if (empty($video)) {
return error('请确认操作对象是不存在');
}
if (!$video->delete()) {
return error('操作失败');
}
return success('操作成功');
}
} }

View File

@@ -11,6 +11,13 @@ use app\common\model\VideoBaseModel;
*/ */
class VideoModel extends VideoBaseModel class VideoModel extends VideoBaseModel
{ {
// 启用软件删除
use SoftDelete;
// 软件删除字段
protected $deleteTime = 'deleted_at';
// 自动写入的时间格式
protected $autoWriteTimestamp = 'datetime';
// 关联分类 // 关联分类
public function category() public function category()
{ {

View File

@@ -58,6 +58,9 @@ Route::group('v1', function () {
// 视频信息更新 // 视频信息更新
Route::put('update/:id', 'Video/update'); Route::put('update/:id', 'Video/update');
// 视频信息设置排序值
Route::post('sort/:id', 'Video/sort');
// 视频信息删除 // 视频信息删除
Route::delete('delete/:id', 'Video/delete'); Route::delete('delete/:id', 'Video/delete');
}); });

View File

@@ -0,0 +1,94 @@
<?php
declare (strict_types = 1);
namespace app\admin\validate\v1;
use think\Validate;
class VideoValidate extends Validate
{
/**
* 定义验证规则
* 格式:'字段名' => ['规则1','规则2'...]
*
* @var array
*/
protected $rule = [
'id' => 'require|integer',
'language_id' => 'require|integer',
'category_id' => 'require|integer',
'name' => 'require|max:64',
'desc' => 'max:255',
'image' => 'max:125',
'video' => 'max:125',
'link' => 'url|max:125',
'sort' => 'integer',
'recommend' => 'integer|in:1,0',
'status' => 'integer|in:1,-1',
'seo_title' => 'max:255',
'seo_keywords' => 'max:255',
'seo_desc' => 'max:255',
];
/**
* 定义错误信息
* 格式:'字段名.规则名' => '错误信息'
*
* @var array
*/
protected $message = [
'id.require' => 'ID不能为空',
'id.integer' => 'ID参数类型错误',
'language_id.require' => '语言不能为空',
'language_id.integer' => '语言参数类型错误',
'category_id.require' => '分类不能为空',
'category_id.integer' => '分类参数类型错误',
'name.require' => '名称不能为空',
'name.max' => '名称不能超过64个字符',
'desc.max' => '描述不能超过255个字符',
'image.max' => '图片不能超过125个字符',
'video.max' => '视频不能超过125个字符',
'link.url' => '链接格式错误',
'link.max' => '链接不能超过125个字符',
'sort.integer' => '排序参数类型错误',
'recommend.integer' => '推荐参数类型错误',
'recommend.in' => '推荐参数错误必须是1或0',
'status.integer' => '状态参数类型错误',
'status.in' => '状态参数错误必须是1或-1',
'seo_title.max' => 'SEO标题不能超过255个字符',
'seo_keywords.max' => 'SEO关键字不能超过255个字符',
'seo_desc.max' => 'SEO描述不能超过255个字符',
];
/**
* 新增韬晦
*/
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']);
}
/**
* 删除韬晦
*/
public function sceneDelete()
{
return $this->only(['id']);
}
}

View File

@@ -3,12 +3,10 @@ declare (strict_types = 1);
namespace app\common\model; namespace app\common\model;
use think\Model;
/** /**
* @mixin \think\Model * @mixin \think\Model
*/ */
class VideoBaseModel extends Model class VideoBaseModel extends BaseModel
{ {
// 表名 // 表名
protected $name = 'video'; protected $name = 'video';

View File

@@ -3,12 +3,10 @@ declare (strict_types = 1);
namespace app\common\model; namespace app\common\model;
use think\Model;
/** /**
* @mixin \think\Model * @mixin \think\Model
*/ */
class VideoCategoryBaseModel extends Model class VideoCategoryBaseModel extends BaseModel
{ {
// 表名 // 表名
protected $name = 'video_category'; protected $name = 'video_category';