链接管理模块
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\validate\split;
|
||||
|
||||
use app\common\library\CountryIso;
|
||||
use app\common\service\SplitLinkCodeService;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 分流链接验证器
|
||||
*/
|
||||
class Link extends Validate
|
||||
{
|
||||
protected $rule = [
|
||||
'countries' => 'require|checkCountries',
|
||||
'link_code' => 'require|checkLinkCode',
|
||||
'description' => 'require|max:255',
|
||||
'ip_protect' => 'in:0,1',
|
||||
'random_shuffle' => 'in:0,1',
|
||||
'status' => 'in:normal,hidden',
|
||||
];
|
||||
|
||||
protected $message = [
|
||||
'countries.require' => '请至少选择一个投放国家',
|
||||
'link_code.require' => '请填写分流链接',
|
||||
'description.require' => '请填写链接描述',
|
||||
'description.max' => '链接描述不能超过255个字符',
|
||||
];
|
||||
|
||||
protected $scene = [
|
||||
'add' => ['countries', 'link_code', 'description', 'ip_protect', 'random_shuffle', 'status'],
|
||||
'edit' => ['countries', 'description', 'ip_protect', 'random_shuffle', 'status'],
|
||||
];
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool|string
|
||||
*/
|
||||
protected function checkCountries($value)
|
||||
{
|
||||
$codes = is_array($value) ? $value : explode(',', (string)$value);
|
||||
if (CountryIso::normalizeCodes($codes) === []) {
|
||||
return '请至少选择一个有效的投放国家';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验分流链接格式与唯一性(仅新增)
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return bool|string
|
||||
*/
|
||||
protected function checkLinkCode($value)
|
||||
{
|
||||
$code = SplitLinkCodeService::normalize((string) $value);
|
||||
if (!SplitLinkCodeService::isValidFormat($code)) {
|
||||
return '分流链接须为9位小写字母(a-z)';
|
||||
}
|
||||
if (!SplitLinkCodeService::isAvailable($code)) {
|
||||
return '该分流链接已被使用,请更换';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user