feat: 添加视频分类分页/详情/添加/更新/设置排序值/删除接口
This commit is contained in:
156
app/admin/controller/v1/VideoCategoory.php
Normal file
156
app/admin/controller/v1/VideoCategoory.php
Normal file
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller\v1;
|
||||
|
||||
use app\admin\model\v1\VideoCategoryModel;
|
||||
use app\admin\validate\v1\VideoCategoryValidate;
|
||||
|
||||
/**
|
||||
* 视频分类控制器
|
||||
*/
|
||||
class VideoCategoory
|
||||
{
|
||||
/**
|
||||
* 视频分类分页数据
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$param = request()->param([
|
||||
'name' => '',
|
||||
'page/d' => 1,
|
||||
'size/d' => 10,
|
||||
]);
|
||||
|
||||
$categorys = VideoCategoryModel::withoutField([
|
||||
'languange_id',
|
||||
'created_at',
|
||||
'updated_at'
|
||||
])
|
||||
->withSearch(['name'], [
|
||||
'name' => $param['name']??null
|
||||
])
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->paginate([
|
||||
'list_rows' => $param['size'],
|
||||
'page' => $param['page'],
|
||||
]);
|
||||
|
||||
return success('获取成功', $categorys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 视频分类详情
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$category = VideoCategoryModel::withoutField([
|
||||
'created_at',
|
||||
'updated_at'
|
||||
])
|
||||
->bypk($id)
|
||||
->find();
|
||||
if (empty($category)) {
|
||||
return error('分类不存在');
|
||||
}
|
||||
|
||||
return success('获取成功', $category);
|
||||
}
|
||||
|
||||
/**
|
||||
* 视频分类添加
|
||||
*/
|
||||
public function save()
|
||||
{
|
||||
$post = request()->post([
|
||||
'name',
|
||||
'sort',
|
||||
'is_show'
|
||||
]);
|
||||
$data = array_merge($post, ['language_id' => request()->lang_id]);
|
||||
|
||||
$validate = new VideoCategoryValidate;
|
||||
if (!$validate->scene('create')->check($data)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$category = new VideoCategoryModel;
|
||||
if (!$category->save($data)) {
|
||||
return error('操作失败');
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 视频分类更新
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
$put = request()->put([
|
||||
'name',
|
||||
'sort',
|
||||
'is_show'
|
||||
]);
|
||||
|
||||
$validate = new VideoCategoryValidate;
|
||||
if (!$validate->scene('update')->check(array_merge($put, ['id' => $id]))) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$category = VideoCategoryModel::bypk($id)->find();
|
||||
if (empty($category)) {
|
||||
return error('请确认操作对象是否存在');
|
||||
}
|
||||
|
||||
if (!$category->save($put)) {
|
||||
return error('操作失败');
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置视频分类排序值
|
||||
*/
|
||||
public function sort()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
$sort = request()->post('sort');
|
||||
|
||||
$validate = new VideoCategoryValidate;
|
||||
if (!$validate::scene('sort')->check(['id' => $id,'sort' => $sort])) {
|
||||
return error($validate::getError());
|
||||
}
|
||||
|
||||
$category = VideoCategoryModel::bypk($id)->find();
|
||||
if (!$category) {
|
||||
return error('请确认操作对象是否存在');
|
||||
}
|
||||
|
||||
$category->sort = $sort;
|
||||
if (!$category->save()) {
|
||||
return error('操作失败');
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 视频分类删除
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$category = VideoCategoryModel::bypk($id)->find();
|
||||
if (!$category) {
|
||||
return error('请确认操作对象是否存在');
|
||||
}
|
||||
|
||||
if (!$category->delete()) {
|
||||
return error('操作失败');
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
}
|
||||
@@ -11,5 +11,12 @@ use app\common\model\VideoCategoryBaseModel;
|
||||
*/
|
||||
class VideoCategoryModel extends VideoCategoryBaseModel
|
||||
{
|
||||
//
|
||||
// 分类名称搜索
|
||||
public function searchNameAttr($query, $value)
|
||||
{
|
||||
if (empty($value)) {
|
||||
return;
|
||||
}
|
||||
$query->where('name', 'like', '%' . $value . '%');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,6 +66,27 @@ Route::group('v1', function () {
|
||||
|
||||
// 视频信息删除
|
||||
Route::delete('delete/:id', 'Video/delete');
|
||||
|
||||
// 视频分类
|
||||
Route::group('category', function () {
|
||||
// 视频分类分页数据
|
||||
Route::get('index', 'VideoCategory/index');
|
||||
|
||||
// 视频分类详情
|
||||
Route::get('read/:id', 'VideoCategory/read');
|
||||
|
||||
// 视频分类添加
|
||||
Route::post('save', 'VideoCategory/save');
|
||||
|
||||
// 视频分类更新
|
||||
Route::put('update/:id', 'VideoCategory/update');
|
||||
|
||||
// 设置视频分类排序值
|
||||
Route::put('sort/:id', 'VideoCategory/sort');
|
||||
|
||||
// 视频分类删除
|
||||
Route::delete('delete/:id', 'VideoCategory/delete');
|
||||
});
|
||||
});
|
||||
|
||||
// 文章模块
|
||||
|
||||
64
app/admin/validate/v1/VideoCategoryValidate.php
Normal file
64
app/admin/validate/v1/VideoCategoryValidate.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class VideoCategoryValidate extends Validate
|
||||
{
|
||||
/**
|
||||
* 定义验证规则
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rule = [
|
||||
'id' => 'require|integer',
|
||||
'language_id' => 'require|integer',
|
||||
'name' => 'require|max:64',
|
||||
'sort' => 'integer',
|
||||
'is_show' => 'in:0,1',
|
||||
];
|
||||
|
||||
/**
|
||||
* 定义错误信息
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'id.require' => 'ID不能为空',
|
||||
'id.integer' => 'ID必须是整数',
|
||||
'language_id.require' => '语言ID不能为空',
|
||||
'language_id.integer' => '语言ID必须是整数',
|
||||
'name.require' => '名称不能为空',
|
||||
'name.max' => '名称不能超过64个字符',
|
||||
'sort.integer' => '排序必须是整数',
|
||||
'is_show.in' => '是否显示必须是0或1',
|
||||
];
|
||||
|
||||
/**
|
||||
* 新增场景
|
||||
*/
|
||||
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']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user