diff --git a/app/admin/controller/v1/Video.php b/app/admin/controller/v1/Video.php index 7fccf10e..139cf151 100644 --- a/app/admin/controller/v1/Video.php +++ b/app/admin/controller/v1/Video.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace app\admin\controller\v1; use app\admin\model\v1\VideoModel; +use app\admin\validate\v1\VideoValidate; /** * 视频管理控制器 @@ -69,4 +70,123 @@ class 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('操作成功'); + } } diff --git a/app/admin/model/v1/VideoModel.php b/app/admin/model/v1/VideoModel.php index 38e0a2db..027da4a2 100644 --- a/app/admin/model/v1/VideoModel.php +++ b/app/admin/model/v1/VideoModel.php @@ -11,6 +11,13 @@ use app\common\model\VideoBaseModel; */ class VideoModel extends VideoBaseModel { + // 启用软件删除 + use SoftDelete; + // 软件删除字段 + protected $deleteTime = 'deleted_at'; + // 自动写入的时间格式 + protected $autoWriteTimestamp = 'datetime'; + // 关联分类 public function category() { diff --git a/app/admin/route/v1.php b/app/admin/route/v1.php index de7cf779..95ed2590 100644 --- a/app/admin/route/v1.php +++ b/app/admin/route/v1.php @@ -58,6 +58,9 @@ Route::group('v1', function () { // 视频信息更新 Route::put('update/:id', 'Video/update'); + // 视频信息设置排序值 + Route::post('sort/:id', 'Video/sort'); + // 视频信息删除 Route::delete('delete/:id', 'Video/delete'); }); diff --git a/app/admin/validate/v1/VideoValidate.php b/app/admin/validate/v1/VideoValidate.php new file mode 100644 index 00000000..7ebd0a20 --- /dev/null +++ b/app/admin/validate/v1/VideoValidate.php @@ -0,0 +1,94 @@ + ['规则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']); + } +} diff --git a/app/common/model/VideoBaseModel.php b/app/common/model/VideoBaseModel.php index 138a99a6..800636e9 100644 --- a/app/common/model/VideoBaseModel.php +++ b/app/common/model/VideoBaseModel.php @@ -3,12 +3,10 @@ declare (strict_types = 1); namespace app\common\model; -use think\Model; - /** * @mixin \think\Model */ -class VideoBaseModel extends Model +class VideoBaseModel extends BaseModel { // 表名 protected $name = 'video'; diff --git a/app/common/model/VideoCategoryBaseModel.php b/app/common/model/VideoCategoryBaseModel.php index c7ae635b..d78ae9cb 100644 --- a/app/common/model/VideoCategoryBaseModel.php +++ b/app/common/model/VideoCategoryBaseModel.php @@ -3,12 +3,10 @@ declare (strict_types = 1); namespace app\common\model; -use think\Model; - /** * @mixin \think\Model */ -class VideoCategoryBaseModel extends Model +class VideoCategoryBaseModel extends BaseModel { // 表名 protected $name = 'video_category';