feat: 添加视频分页/详情接口
This commit is contained in:
@@ -1,12 +1,72 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\v1;
|
||||
|
||||
use app\admin\model\v1\VideoModel;
|
||||
|
||||
/**
|
||||
* 视频管理控制器
|
||||
*/
|
||||
class Video
|
||||
{
|
||||
|
||||
/**
|
||||
* 视频信息分页数据
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$params = request()->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);
|
||||
}
|
||||
}
|
||||
|
||||
15
app/admin/model/v1/VideoCategoryModel.php
Normal file
15
app/admin/model/v1/VideoCategoryModel.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use app\common\model\VideoCategoryBaseModel;
|
||||
|
||||
/**
|
||||
* 视频分类模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class VideoCategoryModel extends VideoCategoryBaseModel
|
||||
{
|
||||
//
|
||||
}
|
||||
52
app/admin/model/v1/VideoModel.php
Normal file
52
app/admin/model/v1/VideoModel.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use app\common\model\VideoBaseModel;
|
||||
|
||||
/**
|
||||
* 视频信息模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class VideoModel extends VideoBaseModel
|
||||
{
|
||||
// 关联分类
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(VideoCategoryModel::class, 'category_id', 'id');
|
||||
}
|
||||
|
||||
// 名称搜索器
|
||||
public function searchNameAttr($query, $value, $data)
|
||||
{
|
||||
if (empty($value)) {
|
||||
return;
|
||||
}
|
||||
$query->where('name', 'like', '%' . $value . '%');
|
||||
}
|
||||
|
||||
// 搜索发布时间
|
||||
public function searchCreatedAtAttr($query, $value, $data)
|
||||
{
|
||||
if (empty($value)) {
|
||||
return;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
if (count($value) == 2) {
|
||||
$query->whereBetweenTime('created_at', $value[0], $value[1]);
|
||||
} else {
|
||||
$query->whereTime('created_at', '>=', $value[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 分类查询
|
||||
public function scopeCategoryId($query, $value)
|
||||
{
|
||||
if (empty($value)) {
|
||||
return;
|
||||
}
|
||||
$query->where('category_id', '=', $value);
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,21 @@ Route::group('v1', function () {
|
||||
Route::group('video', function () {
|
||||
// 视频上传
|
||||
Route::post('/:module/upload', 'Upload/video');
|
||||
|
||||
// 视频信息列表
|
||||
Route::get('index', 'Video/index');
|
||||
|
||||
// 视频信息详情
|
||||
Route::get('read/:id', 'Video/read');
|
||||
|
||||
// 视频信息添加
|
||||
Route::post('save', 'Video/save');
|
||||
|
||||
// 视频信息更新
|
||||
Route::put('update/:id', 'Video/update');
|
||||
|
||||
// 视频信息删除
|
||||
Route::delete('delete/:id', 'Video/delete');
|
||||
});
|
||||
|
||||
// 文章模块
|
||||
|
||||
39
app/common/model/VideoBaseModel.php
Normal file
39
app/common/model/VideoBaseModel.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class VideoBaseModel extends Model
|
||||
{
|
||||
// 表名
|
||||
protected $name = 'video';
|
||||
|
||||
// 主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 字段信息
|
||||
protected $schema = [
|
||||
'id' => 'int',
|
||||
'language_id' => 'int',
|
||||
'category_id' => 'int',
|
||||
'name' => 'string',
|
||||
'desc' => 'string',
|
||||
'image' => 'string',
|
||||
'video' => 'string',
|
||||
'link' => 'string',
|
||||
'sort' => 'int',
|
||||
'recommend' => 'int',
|
||||
'status' => 'int',
|
||||
'seo_title' => 'string',
|
||||
'seo_keywords' => 'string',
|
||||
'seo_desc' => 'string',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'deleted_at' => 'datetime'
|
||||
];
|
||||
}
|
||||
29
app/common/model/VideoCategoryBaseModel.php
Normal file
29
app/common/model/VideoCategoryBaseModel.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class VideoCategoryBaseModel extends Model
|
||||
{
|
||||
// 表名
|
||||
protected $name = 'video_category';
|
||||
|
||||
// 主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 字段信息
|
||||
protected $schema = [
|
||||
'id' => 'int',
|
||||
'language_id' => 'int',
|
||||
'name' => 'string',
|
||||
'sort' => 'int',
|
||||
'is_show' => 'int',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use Phinx\Db\Adapter\MysqlAdapter;
|
||||
use think\migration\Migrator;
|
||||
|
||||
class CreateVideo extends Migrator
|
||||
@@ -37,10 +38,12 @@ class CreateVideo extends Migrator
|
||||
->addColumn('link', 'string', ['limit' => 125, 'null' => true, 'default' => null, 'comment' => '外链地址'])
|
||||
->addColumn('sort', 'integer', ['null' => false, 'default' => 0, 'comment' => '排序'])
|
||||
->addColumn('recommend', 'boolean', ['null' => false, 'default' => 1, 'comment' => '是否推荐:1是,0否'])
|
||||
->addColumn('status', MysqlAdapter::INT_TINY, ['null' => false, 'default' => 1, 'comment' => '状态:1启用,-1禁用'])
|
||||
->addColumn('seo_title', 'string', ['limit' => 255, 'default' => null, 'comment' => 'SEO标题'])
|
||||
->addColumn('seo_keywords', 'string', ['limit' => 255, 'default' => null, 'comment' => 'SEO关键字'])
|
||||
->addColumn('seo_desc', 'string', ['limit' => 255, 'default' => null, 'comment' => 'SEO描述'])
|
||||
->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
|
||||
->addColumn('updated_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', 'comment' => '更新时间'])
|
||||
->addColumn('deleted_at', 'timestamp', ['null' => true, 'comment' => '删除时间'])
|
||||
->create();
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ class CreateVideoCategory extends Migrator
|
||||
->addColumn('name', 'string', ['limit' => 64 , 'null' => false, 'comment' => '分类名称'])
|
||||
->addColumn('sort', 'integer', ['null' => false, 'default' => 0, 'comment' => '排序'])
|
||||
->addColumn('is_show', 'boolean', ['null' => false, 'default' => 1, 'comment' => '是否显示:1是,0否'])
|
||||
->addColumn('created_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP', 'comment' => '创建时间'])
|
||||
->addColumn('updated_at', 'timestamp', ['null' => false, 'default' => 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', 'comment' => '更新时间'])
|
||||
->create();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user