refactor: 修改附件分类支持上下级
This commit is contained in:
@@ -11,20 +11,48 @@ use app\admin\validate\v1\AttachmentCategoryValidate;
|
||||
*/
|
||||
class AttachmentCategory
|
||||
{
|
||||
/**
|
||||
* 列表树形结构
|
||||
*/
|
||||
public function tree()
|
||||
{
|
||||
$params = request()->param([
|
||||
'name',
|
||||
'is_show'
|
||||
]);
|
||||
|
||||
$categorys = AttachmentCategoryModel::field([
|
||||
'id',
|
||||
'pid',
|
||||
'name'
|
||||
])
|
||||
->withSearch(['name'], [
|
||||
'name' => $params['name']??null
|
||||
])
|
||||
->where(function($query) use($params) {
|
||||
if (isset($params['is_show'])) {
|
||||
$query->where('is_show', '=', $params['is_show']);
|
||||
}
|
||||
})
|
||||
->language(request()->lang_id)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select();
|
||||
|
||||
return success('获取成功', array_to_tree($categorys->toArray(), 0, 'pid', 1, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页数据
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$params = request()->param([
|
||||
'name',
|
||||
'is_show',
|
||||
'page/d' => 1,
|
||||
'size/d' => 10
|
||||
'name'
|
||||
]);
|
||||
|
||||
$categorys = AttachmentCategoryModel::field([
|
||||
'id',
|
||||
'pid',
|
||||
'name',
|
||||
'sort',
|
||||
'is_show'
|
||||
@@ -33,23 +61,10 @@ class AttachmentCategory
|
||||
'name' => $params['name']??null
|
||||
])
|
||||
->language(request()->lang_id)
|
||||
->order(['sort' => 'asc', 'id' => 'desc']);
|
||||
if (!request()->has('scene')) {
|
||||
$categorys = $categorys->paginate([
|
||||
'list_rows' => $params['size'],
|
||||
'page' => $params['page']
|
||||
]);
|
||||
} else if ('all' == request()->param('scene')) {
|
||||
$categorys = $categorys->where(function($query) use($params) {
|
||||
if (isset($params['is_show'])) {
|
||||
$query->where('is_show', '=', $params['is_show']);
|
||||
}
|
||||
})
|
||||
->select()
|
||||
->hidden(['sort', 'is_show']);
|
||||
}
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->select();
|
||||
|
||||
return success('获取成功', $categorys);
|
||||
return success('获取成功', array_to_tree($categorys->toArray(), 0, 'pid', 1, false));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,6 +95,7 @@ class AttachmentCategory
|
||||
public function save()
|
||||
{
|
||||
$post = request()->post([
|
||||
'pid' => 0,
|
||||
'name',
|
||||
'sort',
|
||||
'is_show'
|
||||
@@ -105,6 +121,7 @@ class AttachmentCategory
|
||||
{
|
||||
$id = request()->param('id');
|
||||
$put = request()->put([
|
||||
'pid' => 0,
|
||||
'name',
|
||||
'sort',
|
||||
'is_show'
|
||||
|
||||
@@ -373,12 +373,12 @@ Route::group('v1', function () {
|
||||
// 附件(下载管理)删除
|
||||
Route::delete('delete/:id', 'Attachment/delete');
|
||||
|
||||
// 附件(下载管理)分类列表
|
||||
Route::get('categorys', 'AttachmentCategory/index')->append(['scene' => 'all']);
|
||||
// 附件(下载管理)分类列表树
|
||||
Route::get('tree', 'AttachmentCategory/tree');
|
||||
|
||||
// 附件(下载管理)分类
|
||||
Route::group('category', function () {
|
||||
// 附件(下载管理)分类分页
|
||||
// 附件(下载管理)分类树
|
||||
Route::get('index', 'AttachmentCategory/index');
|
||||
|
||||
// 附件(下载管理)分类详情
|
||||
|
||||
@@ -3,6 +3,8 @@ declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use app\admin\model\v1\AttachmentCategoryModel;
|
||||
use think\facade\Db;
|
||||
use think\Validate;
|
||||
|
||||
class AttachmentCategoryValidate extends Validate
|
||||
@@ -15,6 +17,7 @@ class AttachmentCategoryValidate extends Validate
|
||||
*/
|
||||
protected $rule = [
|
||||
'id' => 'require|integer',
|
||||
'pid' => 'integer|checkPidNotBeChildren',
|
||||
'language_id' => 'require|integer',
|
||||
'name' => 'require|max:64',
|
||||
'sort' => 'integer',
|
||||
@@ -28,22 +31,50 @@ class AttachmentCategoryValidate extends Validate
|
||||
* @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',
|
||||
'id.require' => 'ID不能为空',
|
||||
'id.integer' => 'ID必须是整数',
|
||||
'pid.integer' => '父级ID必须是整数',
|
||||
'pid.checkPidNotBeChildren' => '父级分类不能为自身或自身的子分类',
|
||||
'language_id.require' => '语言ID不能为空',
|
||||
'language_id.integer' => '语言ID必须是整数',
|
||||
'name.require' => '名称不能为空',
|
||||
'name.max' => '名称不能超过64个字符',
|
||||
'sort.integer' => '排序必须是整数',
|
||||
'is_show.in' => '是否显示必须是0或1',
|
||||
];
|
||||
|
||||
// 验证pid
|
||||
protected function checkPidNotBeChildren($value, $rule, $data = [])
|
||||
{
|
||||
if ($value == 0) {
|
||||
return true;
|
||||
}
|
||||
$table_name = (new AttachmentCategoryModel)->getTable();
|
||||
$children = Db::query(
|
||||
preg_replace(
|
||||
'/\s+/u',
|
||||
' ',
|
||||
"WITH RECURSIVE attachment_tree_by AS (
|
||||
SELECT a.id, a.pid FROM $table_name a WHERE a.id = {$data['id']}
|
||||
UNION ALL
|
||||
SELECT k.id, k.pid FROM $table_name k INNER JOIN attachement_tree_by t ON t.id = k.pid
|
||||
)
|
||||
SELECT id FROM attachment_tree_by WHERE id <> {$data['id']};"
|
||||
)
|
||||
);
|
||||
if (!empty($children) && in_array($data['pid'], array_column($children, 'id'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增场景
|
||||
*/
|
||||
public function sceneCreate()
|
||||
{
|
||||
return $this->remove('id', 'require|integer');
|
||||
return $this->remove('id', 'require|integer')->remove('pid', 'checkPidNotBeChildren');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user