72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\admin\validate\v1;
|
|
|
|
use app\admin\controller\v1\ProductPurchaseLink;
|
|
use app\admin\model\v1\ProductPurchaseLinkModel;
|
|
use think\Validate;
|
|
|
|
class ProductPurchaseLinkValidate extends Validate
|
|
{
|
|
/**
|
|
* 定义验证规则
|
|
* 格式:'字段名' => ['规则1','规则2'...]
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $rule = [
|
|
'id' => 'require|integer',
|
|
'link' => 'url|max:255',
|
|
'platform_id' => 'sameSpuPlatformCheck|integer'
|
|
];
|
|
|
|
/**
|
|
* 定义错误信息
|
|
* 格式:'字段名.规则名' => '错误信息'
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $message = [
|
|
'id.require' => 'id不能为空',
|
|
'id.integer' => 'id字段类型错误',
|
|
'link.url' => '链接格式不正确',
|
|
'link.max' => '链接不能超过255个字符',
|
|
'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;
|
|
}
|
|
|
|
/**
|
|
* 更新场景
|
|
*/
|
|
public function sceneUpdate()
|
|
{
|
|
return $this->only(['id', 'link', 'platform_id'])->remove('id', 'require');
|
|
}
|
|
}
|