feat: 添加附件(下载管理)分类分页/详情/新增/更新/设置排序值/删除接口
This commit is contained in:
165
app/admin/controller/v1/AttachmentCategory.php
Normal file
165
app/admin/controller/v1/AttachmentCategory.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller\v1;
|
||||
|
||||
use app\admin\model\v1\AttachmentCategoryModel;
|
||||
use app\admin\validate\v1\AttachmentCategoryValidate;
|
||||
|
||||
/**
|
||||
* 附件(下载管理)分类管理控制器
|
||||
*/
|
||||
class AttachmentCategory
|
||||
{
|
||||
/**
|
||||
* 分页数据
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$params = request()->param([
|
||||
'name',
|
||||
'page/d' => 1,
|
||||
'size/d' => 10
|
||||
]);
|
||||
|
||||
$categorys = AttachmentCategoryModel::field([
|
||||
'id',
|
||||
'name',
|
||||
'sort',
|
||||
'is_show'
|
||||
])
|
||||
->withSearch(['name'], [
|
||||
'name' => $params['name']??null
|
||||
])
|
||||
->language(request()->lang_id)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->paginate([
|
||||
'list_rows' => $params['size'],
|
||||
'page' => $params['page']
|
||||
]);
|
||||
|
||||
return success('获取成功', $categorys);
|
||||
}
|
||||
|
||||
/**
|
||||
* 附件分类详情
|
||||
*/
|
||||
public function read()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$category = AttachmentCategoryModel::withoutField([
|
||||
'language_id',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_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 AttachmentCategoryValidate;
|
||||
if (!$validate->scene('create')->check($post)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$category = AttachmentCategoryModel::create($data);
|
||||
if ($category->isEmpty()) {
|
||||
return error('操作失败');
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 附件分类更新
|
||||
*/
|
||||
public function update()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
$put = request()->put([
|
||||
'name',
|
||||
'sort',
|
||||
'is_show'
|
||||
]);
|
||||
|
||||
$validate = new AttachmentCategoryValidate;
|
||||
if (!$validate->scene('update')->check(array_merge($put, ['id' => $id]))) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$category = AttachmentCategoryModel::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()->param('sort');
|
||||
|
||||
$validate = new AttachmentCategoryValidate;
|
||||
if (!$validate->scene('sort')->check(['sort' => $sort])) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$category = AttachmentCategoryModel::bypk($id)->find();
|
||||
if (empty($category)) {
|
||||
return error('请确认操作对象是否存在');
|
||||
}
|
||||
|
||||
$category->sort = $sort;
|
||||
if (!$category->save()) {
|
||||
return error('操作失败');
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 附件分类删除
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$category = AttachmentCategoryModel::bypk($id)->find();
|
||||
if (empty($category)) {
|
||||
return error('请确认操作对象是否存在');
|
||||
}
|
||||
|
||||
// 检查分类下是否存在附件
|
||||
if (!$category->attachment()->count()) {
|
||||
return error('分类下存在附件,请先删除附件');
|
||||
}
|
||||
|
||||
if (!$category->delete()) {
|
||||
return error('操作失败');
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
}
|
||||
27
app/admin/model/v1/AttachmentCategoryModel.php
Normal file
27
app/admin/model/v1/AttachmentCategoryModel.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use app\common\model\AttachmentCategoryBaseModel;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
/**
|
||||
* 附件(下载管理)分类模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class AttachmentCategoryModel extends AttachmentCategoryBaseModel
|
||||
{
|
||||
// 启用软件删除
|
||||
use SoftDelete;
|
||||
// 软删除字段
|
||||
protected $deleteTime = 'deleted_at';
|
||||
// 自动写入时间格式
|
||||
protected $autoWriteTimestamp = 'datetime';
|
||||
|
||||
// 关联附件
|
||||
public function attachment()
|
||||
{
|
||||
return $this->hasMany(AttachmentModel::class, 'category_id', 'id');
|
||||
}
|
||||
}
|
||||
@@ -293,6 +293,27 @@ Route::group('v1', function () {
|
||||
|
||||
// 附件(下载管理)删除
|
||||
Route::delete('delete/:id', 'Attachment/delete');
|
||||
|
||||
// 附件(下载管理)分类
|
||||
Route::group('category', function () {
|
||||
// 附件(下载管理)分类列表
|
||||
Route::get('index', 'AttachmentCategory/index');
|
||||
|
||||
// 附件(下载管理)分类详情
|
||||
Route::get('read/:id', 'AttachmentCategory/read');
|
||||
|
||||
// 附件(下载管理)分类新增
|
||||
Route::post('save', 'AttachmentCategory/save');
|
||||
|
||||
// 附件(下载管理)分类更新
|
||||
Route::put('update/:id','AttachmentCategory/update');
|
||||
|
||||
// 附件(下载管理)分类设置排序值
|
||||
Route::post('sort/:id', 'AttachmentCategory/sort');
|
||||
|
||||
// 附件(下载管理)分类删除
|
||||
Route::delete('delete/:id', 'AttachmentCategory/delete');
|
||||
});
|
||||
});
|
||||
});
|
||||
})->prefix('v1.');
|
||||
|
||||
64
app/admin/validate/v1/AttachmentCategoryValidate.php
Normal file
64
app/admin/validate/v1/AttachmentCategoryValidate.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class AttachmentCategoryValidate 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']);
|
||||
}
|
||||
}
|
||||
31
app/common/model/AttachmentCategoryBaseModel.php
Normal file
31
app/common/model/AttachmentCategoryBaseModel.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use think\Model;
|
||||
|
||||
/**
|
||||
* 附件(下载管理)分类模型
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class AttachmentCategoryBaseModel extends Model
|
||||
{
|
||||
// 表名
|
||||
protected $name = 'attachment_category';
|
||||
|
||||
// 主键
|
||||
protected $pk = 'id';
|
||||
|
||||
// 字段信息
|
||||
protected $schema = [
|
||||
'id' => 'int',
|
||||
'language_id' => 'int',
|
||||
'name' => 'string',
|
||||
'sort' => 'int',
|
||||
'is_show' => 'int',
|
||||
'created_at' => 'datetime',
|
||||
'updated_at' => 'datetime',
|
||||
'deleted_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user