Compare commits
3 Commits
cd98608188
...
7de54e5550
| Author | SHA1 | Date | |
|---|---|---|---|
| 7de54e5550 | |||
| e7ea53c78e | |||
| 1ad2f9b016 |
@@ -82,7 +82,7 @@ class ProductCategory
|
|||||||
$data = array_merge($post, ['language_id' => request()->lang_id]);
|
$data = array_merge($post, ['language_id' => request()->lang_id]);
|
||||||
|
|
||||||
$validate = new ProductCategoryValidate;
|
$validate = new ProductCategoryValidate;
|
||||||
if (!$validate->check($data)) {
|
if (!$validate->scene('add')->check($data)) {
|
||||||
return error($validate->getError());
|
return error($validate->getError());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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'],
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ declare (strict_types = 1);
|
|||||||
|
|
||||||
namespace app\admin\validate\v1;
|
namespace app\admin\validate\v1;
|
||||||
|
|
||||||
|
use app\admin\model\v1\ProductCategoryModel;
|
||||||
|
use think\facade\Db;
|
||||||
use think\Validate;
|
use think\Validate;
|
||||||
|
|
||||||
class ProductCategoryValidate extends Validate
|
class ProductCategoryValidate extends Validate
|
||||||
@@ -14,8 +16,10 @@ class ProductCategoryValidate extends Validate
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $rule = [
|
protected $rule = [
|
||||||
|
'id' => 'require|integer',
|
||||||
'language_id' => 'require|integer',
|
'language_id' => 'require|integer',
|
||||||
'unique_id' => 'require',
|
'unique_id' => 'require',
|
||||||
|
'pid' => 'integer|different:id|checkPidNotBeChildren',
|
||||||
'name' => 'require|max:64',
|
'name' => 'require|max:64',
|
||||||
'icon' => 'max:125',
|
'icon' => 'max:125',
|
||||||
'desc' => 'max:255',
|
'desc' => 'max:255',
|
||||||
@@ -35,9 +39,14 @@ class ProductCategoryValidate extends Validate
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $message = [
|
protected $message = [
|
||||||
|
'id.require' => 'ID不能为空',
|
||||||
|
'id.integer' => 'ID必须为整数',
|
||||||
'language_id.require' => '语言ID不能为空',
|
'language_id.require' => '语言ID不能为空',
|
||||||
'language_id.integer' => '语言ID必须为整数',
|
'language_id.integer' => '语言ID必须为整数',
|
||||||
'unique_id.require' => '唯一标识不能为空',
|
'unique_id.require' => '唯一标识不能为空',
|
||||||
|
'pid.different' => '父级ID不能为自身',
|
||||||
|
'pid.checkPidNotBeChildren' => '父级ID不能为自身的子分类',
|
||||||
|
'pid.integer' => '父级ID必须为整数',
|
||||||
'name.require' => '名称不能为空',
|
'name.require' => '名称不能为空',
|
||||||
'name.max' => '名称最多不能超过64个字符',
|
'name.max' => '名称最多不能超过64个字符',
|
||||||
'icon.max' => '图标最多不能超过125个字符',
|
'icon.max' => '图标最多不能超过125个字符',
|
||||||
@@ -50,4 +59,36 @@ class ProductCategoryValidate extends Validate
|
|||||||
'seo_keywords.max' => 'SEO关键字最多不能超过255个字符',
|
'seo_keywords.max' => 'SEO关键字最多不能超过255个字符',
|
||||||
'seo_desc.max' => 'SEO描述最多不能超过255个字符',
|
'seo_desc.max' => 'SEO描述最多不能超过255个字符',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// 验证pid
|
||||||
|
protected function checkPidNotBeChildren($value, $rule, $data = [])
|
||||||
|
{
|
||||||
|
if ($value == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
$table_name = (new ProductCategoryModel)->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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 新增场景
|
||||||
|
public function sceneAdd()
|
||||||
|
{
|
||||||
|
return $this->remove('id', 'require|integer')->remove('pid', 'different|checkPidNotBeChildren');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ declare (strict_types = 1);
|
|||||||
|
|
||||||
namespace app\admin\validate\v1;
|
namespace app\admin\validate\v1;
|
||||||
|
|
||||||
|
use app\admin\model\v1\SysMenuModel;
|
||||||
|
use think\facade\Db;
|
||||||
use think\Validate;
|
use think\Validate;
|
||||||
|
|
||||||
class SysMenuValidate extends Validate
|
class SysMenuValidate extends Validate
|
||||||
@@ -15,7 +17,7 @@ class SysMenuValidate extends Validate
|
|||||||
*/
|
*/
|
||||||
protected $rule = [
|
protected $rule = [
|
||||||
'id' => 'require|integer',
|
'id' => 'require|integer',
|
||||||
'pid' => 'integer',
|
'pid' => 'integer|different:id|checkPidNotBeChildren',
|
||||||
'title' => 'require|max:64',
|
'title' => 'require|max:64',
|
||||||
'name' => 'require|unique:sys_menu|max:64',
|
'name' => 'require|unique:sys_menu|max:64',
|
||||||
'path' => 'require|max:128',
|
'path' => 'require|max:128',
|
||||||
@@ -43,6 +45,8 @@ class SysMenuValidate extends Validate
|
|||||||
'id.require' => '菜单ID不能为空',
|
'id.require' => '菜单ID不能为空',
|
||||||
'id.integer' => '菜单ID必须为整数',
|
'id.integer' => '菜单ID必须为整数',
|
||||||
'pid.integer' => '父级菜单ID必须为整数',
|
'pid.integer' => '父级菜单ID必须为整数',
|
||||||
|
'pid.different' => '父级菜单ID不能为自身',
|
||||||
|
'pid.checkPidNotBeChildren' => '父级菜单不能为自身的子菜单',
|
||||||
'title.require' => '菜单名称不能为空',
|
'title.require' => '菜单名称不能为空',
|
||||||
'title.max' => '菜单名称最多64个字符',
|
'title.max' => '菜单名称最多64个字符',
|
||||||
'name.require' => '菜单Name不能为空',
|
'name.require' => '菜单Name不能为空',
|
||||||
@@ -64,9 +68,35 @@ class SysMenuValidate extends Validate
|
|||||||
'menu_ability_permission.*.sort.integer' => '菜单能力权限排序必须为整数',
|
'menu_ability_permission.*.sort.integer' => '菜单能力权限排序必须为整数',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// 验证pid
|
||||||
|
protected function checkPidNotBeChildren($value, $rule, $data = [])
|
||||||
|
{
|
||||||
|
if ($value == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
$table_name = (new SysMenuModel)->getTable();
|
||||||
|
$children = Db::query(
|
||||||
|
preg_replace(
|
||||||
|
'/\s+/u',
|
||||||
|
' ',
|
||||||
|
"WITH RECURSIVE menu_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 menu_tree_by t ON t.id = k.pid
|
||||||
|
)
|
||||||
|
SELECT id FROM menu_tree_by WHERE id <> {$data['id']};"
|
||||||
|
)
|
||||||
|
);
|
||||||
|
if (!empty($children) && in_array($data['pid'], array_column($children, 'id'))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
// 新增验证场景
|
// 新增验证场景
|
||||||
public function sceneCreate()
|
public function sceneCreate()
|
||||||
{
|
{
|
||||||
return $this->remove('id', 'require|integer');
|
return $this->remove('id', 'require|integer')->remove('pid', 'different|checkPidNotBeChildren');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user