refactor: 产品购买链接同型号平台校验

This commit is contained in:
2025-02-26 13:42:19 +08:00
parent abfc68ff2f
commit 8a07e12635
3 changed files with 34 additions and 7 deletions

View File

@@ -267,6 +267,7 @@ class ProductPurchaseLink
$data = [ $data = [
'id' => $id, 'id' => $id,
'link' => $put['link'], 'link' => $put['link'],
'language_id' => request()->lang_id,
'platform_id' => $put['platform_id'] 'platform_id' => $put['platform_id']
]; ];
$validate = new ProductPurchaseLinkValidate; $validate = new ProductPurchaseLinkValidate;

View File

@@ -4,6 +4,8 @@ declare(strict_types=1);
namespace app\admin\validate\v1; namespace app\admin\validate\v1;
use app\admin\controller\v1\ProductPurchaseLink;
use app\admin\model\v1\ProductPurchaseLinkModel;
use think\Validate; use think\Validate;
class ProductPurchaseLinkValidate extends Validate class ProductPurchaseLinkValidate extends Validate
@@ -17,7 +19,7 @@ class ProductPurchaseLinkValidate extends Validate
protected $rule = [ protected $rule = [
'id' => 'require|integer', 'id' => 'require|integer',
'link' => 'url|max:255', 'link' => 'url|max:255',
'platform_id' => 'integer' 'platform_id' => 'sameSpuPlatformCheck|integer'
]; ];
/** /**
@@ -27,14 +29,38 @@ class ProductPurchaseLinkValidate extends Validate
* @var array * @var array
*/ */
protected $message = [ protected $message = [
'id.require' => 'id不能为空', 'id.require' => 'id不能为空',
'id.integer' => 'id字段类型错误', 'id.integer' => 'id字段类型错误',
'link.url' => '链接格式不正确', 'link.url' => '链接格式不正确',
'link.max' => '链接不能超过255个字符', 'link.max' => '链接不能超过255个字符',
'platform_id.integer' => '平台id类型错误' 'platform_id.sameSpuPlatformCheck' => '同型号平台不能重复',
'platform_id.integer' => '平台id类型错误'
]; ];
/**
* 同型号平台不能重复校验
*/
protected function sameSpuPlatformCheck($value, $rule, $data = [])
{
$sql = ProductPurchaseLinkModel::alias('s')
->field(['s.product_id'])
->where('s.id', '=', $data['id'])
->limit(1)
->buildSql();
$platforms = ProductPurchaseLinkModel::alias('p')
->where('p.product_id', '=', \think\facade\Db::raw($sql))
->where('p.id', '<>', $data['id'])
->where('p.language_id', '=', $data['language_id'])
->column(['p.platform_id']);
if (!empty($platforms) && in_array($value, $platforms)) {
return false;
}
return true;
}
/** /**
* 更新场景 * 更新场景
*/ */

View File

@@ -77,7 +77,7 @@ class ProductValidate extends Validate
'skus.*.sku.max' => 'sku不能超过125个字符', 'skus.*.sku.max' => 'sku不能超过125个字符',
'skus.*.main_image.max' => 'sku主图不能超过255个字符', 'skus.*.main_image.max' => 'sku主图不能超过255个字符',
'skus.*.sort.integer' => 'sku排序值类型错误', 'skus.*.sort.integer' => 'sku排序值类型错误',
'skus.*.attrs.checkSkusAttrsItemType' => 'sku属性值错误', 'skus.*.attrs.checkSkusAttrsItemType' => 'sku属性值错误',
'skus.*.attrs.checkSkusAttrsItemMax' => 'sku属性值错误', 'skus.*.attrs.checkSkusAttrsItemMax' => 'sku属性值错误',
'related.*.related_product_id.integer' => '关联产品ID格式错误', 'related.*.related_product_id.integer' => '关联产品ID格式错误',
'related.*.sort.integer' => '关联产品排序值类型错误', 'related.*.sort.integer' => '关联产品排序值类型错误',