first commit
This commit is contained in:
136
app/adminapi/validate/user/UserValidate.php
Normal file
136
app/adminapi/validate/user/UserValidate.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
namespace app\adminapi\validate\user;
|
||||
|
||||
|
||||
use app\common\model\user\User;
|
||||
use app\common\validate\BaseValidate;
|
||||
|
||||
/**
|
||||
* 用户验证
|
||||
* Class UserValidate
|
||||
* @package app\adminapi\validate\user
|
||||
*/
|
||||
class UserValidate extends BaseValidate
|
||||
{
|
||||
protected $regex = [
|
||||
'pwd' => '/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-z]|[A-Z]|[0-9]){6,20}$/',
|
||||
'email' => '/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/'
|
||||
];
|
||||
|
||||
protected $rule = [
|
||||
'id' => 'require|checkUser',
|
||||
'field' => 'require|checkField',
|
||||
'value' => 'require',
|
||||
'pwd' => 'require|length:6,20|regex:pwd',
|
||||
'email' => 'require|regex:email',
|
||||
];
|
||||
|
||||
protected $message = [
|
||||
'id.require' => '请选择用户',
|
||||
'field.require' => '请选择操作',
|
||||
'value.require' => '请输入内容',
|
||||
'pwd.require' => '请输入密码',//请输入密码
|
||||
'pwd.length' => '密码须在6-20位之间',//密码须在6-20位之间
|
||||
'pwd.regex' => '密码须为字母数字组合',//密码须为字母数字组合
|
||||
'email.require' => '请输入邮箱',//请输入邮箱
|
||||
'email.regex' => '请输入正确的邮箱地址',//请输入正确的邮箱地址
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* @notes 用户信息校验
|
||||
* @param $value
|
||||
* @param $rule
|
||||
* @param $data
|
||||
* @return bool|string
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @author BD
|
||||
* @date 2023/9/22 17:03
|
||||
*/
|
||||
public function checkUser($value, $rule, $data)
|
||||
{
|
||||
$userIds = is_array($value) ? $value : [$value];
|
||||
|
||||
foreach ($userIds as $item) {
|
||||
if (!User::find($item)) {
|
||||
return '用户不存在!';
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @notes 校验是否可更新信息
|
||||
* @param $value
|
||||
* @param $rule
|
||||
* @param $data
|
||||
* @return bool|string
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:37
|
||||
*/
|
||||
public function checkField($value, $rule, $data)
|
||||
{
|
||||
$allowField = ['account', 'sex', 'mobile', 'real_name'];
|
||||
|
||||
if (!in_array($value, $allowField)) {
|
||||
return '用户信息不允许更新';
|
||||
}
|
||||
|
||||
switch ($value) {
|
||||
case 'account':
|
||||
//验证手机号码是否存在
|
||||
$account = User::where([
|
||||
['id', '<>', $data['id']],
|
||||
['account', '=', $data['value']]
|
||||
])->findOrEmpty();
|
||||
|
||||
if (!$account->isEmpty()) {
|
||||
return '账号已被使用';
|
||||
}
|
||||
break;
|
||||
|
||||
case 'mobile':
|
||||
if (false == $this->validate($data['value'], 'mobile', $data)) {
|
||||
return '手机号码格式错误';
|
||||
}
|
||||
|
||||
//验证手机号码是否存在
|
||||
$mobile = User::where([
|
||||
['id', '<>', $data['id']],
|
||||
['mobile', '=', $data['value']]
|
||||
])->findOrEmpty();
|
||||
|
||||
if (!$mobile->isEmpty()) {
|
||||
return '手机号码已存在';
|
||||
}
|
||||
break;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 修改密码场景
|
||||
* @return UserValidate
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:35
|
||||
*/
|
||||
public function sceneChangePwd()
|
||||
{
|
||||
return $this->only(['pwd']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notes 修改邮箱场景
|
||||
* @return UserValidate
|
||||
* @author BD
|
||||
* @date 2023/9/22 16:35
|
||||
*/
|
||||
public function sceneChangeEmail()
|
||||
{
|
||||
return $this->only(['email']);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user