refactor: 修改产品分类中tco分类绑定验证
All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 4s

This commit is contained in:
2025-12-08 16:30:23 +08:00
parent 4d34d76c4f
commit db1ad0f88a

View File

@@ -23,7 +23,7 @@ class ProductCategoryValidate extends Validate
'name' => 'require|max:64',
'icon' => 'max:125',
'desc' => 'max:255',
'related_tco_category' => 'string',
'related_tco_category' => 'string|checkRelatedTcoCategory',
'sort' => 'integer',
'level' => 'integer',
'is_show' => 'in:0,1',
@@ -52,6 +52,7 @@ class ProductCategoryValidate extends Validate
'icon.max' => '图标最多不能超过125个字符',
'desc.max' => '描述最多不能超过255个字符',
'related_tco_category.string' => '关联TCO分类格式错误',
'related_tco_category.checkRelatedTcoCategory' => '该TCO分类已绑定',
'sort.integer' => '排序格式错误',
'level.integer' => '级别格式错误',
'is_show.in' => '是否显示格式错误',
@@ -84,4 +85,32 @@ class ProductCategoryValidate extends Validate
{
return $this->remove('id', 'require|integer')->remove('pid', 'different|checkPidNotBeChildren');
}
// 验证related_tco_category
protected function checkRelatedTcoCategory($value, $rule, $data = [])
{
if (empty($value)) {
return true;
}
$val = ProductCategoryModel::where(function($query) use($value) {
$arr = explode(",", $value);
foreach ($arr as $v) {
$query->whereFindInSet('related_tco_category', $v, 'OR');
}
})->column('id');
if (!empty($val)) {
$size = count($val);
// 如果存在超过一个,直接验证失败
if ($size > 1) {
return false;
}
// 如果存在一个并存在的id不为自身验证失败考虑更新场景查到自身情况
if ($size == 1 && $val[0] != $data['id']) {
return false;
}
}
return true;
}
}