param([ 'name', 'category_id', 'page/d' => 1, 'size/d' => 10 ]); $videos = VideoModel::field([ 'id', 'image', 'name', 'category_id', 'sort', 'recommend', 'status', 'created_at' ]) ->with(['category' => function ($query) { $query->field(['id', 'name']); }]) ->withSearch(['name', 'created_at'], [ 'name' => $params['name'] ?? null, 'created_at' => !empty($params['created_at']) ? explode(",", $params['created_at']) : null ]) ->categoryId($params['category_id'] ?? null) ->order(['sort' => 'desc', 'id' => 'desc']) ->paginate([ 'list_rows' => $params['size'], 'page' => $params['page'], ]) ->hidden(['category', 'category_id']); return success('获取成功', $videos); } /** * 视频信息详情 */ public function read() { $video = VideoModel::withoutField([ 'created_at', 'updated_at', 'deleted_at' ]) ->bypk(request()->param('id')) ->find(); if (empty($video)) { return error('视频不存在'); } 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('操作成功'); } }