refactor: 修改导航父级不能为自身或子导航
This commit is contained in:
@@ -3,6 +3,8 @@ declare (strict_types = 1);
|
|||||||
|
|
||||||
namespace app\admin\validate\v1;
|
namespace app\admin\validate\v1;
|
||||||
|
|
||||||
|
use app\admin\model\v1\SysNavigationItemModel;
|
||||||
|
use think\facade\Db;
|
||||||
use think\Validate;
|
use think\Validate;
|
||||||
|
|
||||||
class NavigationItemValidate extends Validate
|
class NavigationItemValidate extends Validate
|
||||||
@@ -16,7 +18,7 @@ class NavigationItemValidate extends Validate
|
|||||||
protected $rule = [
|
protected $rule = [
|
||||||
'id' => 'require|integer',
|
'id' => 'require|integer',
|
||||||
'nav_id' => 'require|integer',
|
'nav_id' => 'require|integer',
|
||||||
'pid' => 'integer',
|
'pid' => 'integer|different:id|checkPidNotBeChildren',
|
||||||
'name' => 'require|max:64',
|
'name' => 'require|max:64',
|
||||||
'icon' => 'max:64',
|
'icon' => 'max:64',
|
||||||
'link_type' => 'require|integer',
|
'link_type' => 'require|integer',
|
||||||
@@ -38,6 +40,8 @@ class NavigationItemValidate extends Validate
|
|||||||
'nav_id.require' => '导航ID不能为空',
|
'nav_id.require' => '导航ID不能为空',
|
||||||
'nav_id.integer' => '导航ID必须为整数',
|
'nav_id.integer' => '导航ID必须为整数',
|
||||||
'pid.integer' => '父级ID必须为整数',
|
'pid.integer' => '父级ID必须为整数',
|
||||||
|
'pid.different' => '父级ID不能为自身',
|
||||||
|
'pid.checkPidNotBeChildren' => '父级ID不能为自身的子导航',
|
||||||
'name.require' => '导航名称不能为空',
|
'name.require' => '导航名称不能为空',
|
||||||
'name.max' => '导航名称最多不能超过64个字符',
|
'name.max' => '导航名称最多不能超过64个字符',
|
||||||
'icon.max' => '图标最多不能超过64个字符',
|
'icon.max' => '图标最多不能超过64个字符',
|
||||||
@@ -50,6 +54,32 @@ class NavigationItemValidate extends Validate
|
|||||||
'status.in' => '状态只能是1或-1',
|
'status.in' => '状态只能是1或-1',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// 验证pid
|
||||||
|
protected function checkPidNotBeChildren($value, $rule, $data = [])
|
||||||
|
{
|
||||||
|
if ($value == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
$table_name = (new SysNavigationItemModel)->getTable();
|
||||||
|
$children = Db::query(
|
||||||
|
preg_replace(
|
||||||
|
'/\s+/u',
|
||||||
|
' ',
|
||||||
|
"WITH RECURSIVE 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 tree_by t ON t.id = k.pid
|
||||||
|
)
|
||||||
|
SELECT id FROM tree_by WHERE id <> {$data['id']};"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
if (!empty($children) && in_array($data['pid'], array_column($children, 'id'))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// 定义场景
|
// 定义场景
|
||||||
protected $scene = [
|
protected $scene = [
|
||||||
'add' => ['nav_id', 'pid', 'name', 'icon', 'link_type', 'link', 'sort', 'blank', 'status'],
|
'add' => ['nav_id', 'pid', 'name', 'icon', 'link_type', 'link', 'sort', 'blank', 'status'],
|
||||||
|
|||||||
Reference in New Issue
Block a user