Files
fdesk/app/common/validate/UserValidate.php
T
2025-07-01 19:39:13 +08:00

132 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace app\common\validate;
use app\common\model\Systems;
use app\common\model\User;
use think\Validate;
class UserValidate extends Validate
{
protected $rule = [
'name' => 'chsDash|max:60',
'avatar' => 'regex:/^(https?:\/\/\S+|\\/[\S\/]+)$/',
'password' => 'require|min:8|max:20|alphaNum',
'confirm_password' => 'require|confirm:password',
'account' => 'require|checkAccount|checkAccountUnique',
'phone' => 'checkMobile|checkPhoneUnique',
'email' => 'email|checkEmailUnique',
'login_type' => 'require|in:upwd,feishu,qrcode',
'system_code' => 'require|checkSystemCode'
];
protected $message = [
'name.chsDash' => '姓名只能是汉字、字母、数字和下划线_及破折号-',
'name.max' => '姓名最多不能超过60个字符',
'avatar.regex' => '头像必须是合法的URL或有效的文件路径',
'password.require' => '密码不能为空',
'password.min' => '密码长度不能少于8位',
'password.max' => '密码长度不能超过20位',
'password.alphaNum' => '密码只能包含字母和数字',
'confirm_password.require' => '请再次输入密码',
'confirm_password.confirm' => '两次输入的密码不一致',
'account.require' => '账号不能为空',
'account.checkAccount' => '账号必须是有效的手机号或邮箱',
'account.checkAccountUnique' => '账号已被注册',
'phone.checkMobile' => '手机号格式不正确',
'phone.checkPhoneUnique' => '手机号已被使用',
'email.email' => '邮箱格式不正确',
'email.checkEmailUnique' => '邮箱已被使用',
'login_type.require' => '请选择登录方式',
'login_type.in' => '登录方式不支持',
'system_code.require' => '系统码不能为空',
'system_code.checkSystemCode' => '系统码不存在或不合法'
];
protected $scene = [
'upwd_register' => ['name', 'account', 'password', 'confirm_password', 'login_type', 'system_code'],
'upwd_login' => ['account', 'password', 'login_type', 'system_code'],
];
/**
* 验证账号格式(手机号或邮箱)
*/
protected function checkAccount($value)
{
return $this->isMobile($value) || $this->isEmail($value);
}
/**
* 验证账号唯一性(注册场景用)
*/
protected function checkAccountUnique($value, $rule, $data, $field, $title)
{
// 登录场景不检查唯一性
if (isset($this->currentScene) && $this->currentScene === 'login') {
return true;
}
$type = $this->getAccountType($value);
$field = ($type === 'phone') ? 'phone' : 'email';
$value = ($type === 'phone') ? $this->formatPhone($value) : $value;
return !User::where($field, $value)->exists();
}
/**
* 判断是否为手机号
*/
protected function isMobile($value)
{
$mobile = preg_replace('/[\s+\-]/', '', $value);
return preg_match('/^(\+86|0086)?1[3-9]\d{9}$/', $mobile);
}
/**
* 判断是否为邮箱
*/
protected function isEmail($value)
{
return filter_var($value, FILTER_VALIDATE_EMAIL);
}
/**
* 获取账号类型(phone/email)
*/
public function getAccountType($account)
{
return $this->isMobile($account) ? 'phone' : 'email';
}
/**
* 格式化手机号(移除+86和空格)
*/
public function formatPhone($phone)
{
return preg_replace('/^(\+86|0086)|[\s\-]/', '', $phone);
}
/**
* 手机号唯一性验证
*/
protected function checkPhoneUnique($value)
{
return !User::where('phone', $this->formatPhone($value))->exists();
}
/**
* 邮箱唯一性验证
*/
protected function checkEmailUnique($value)
{
return !User::where('email', $value)->exists();
}
/**
* 验证 system_code 是否在系统表中存在
*/
protected function checkSystemCode($value, $rule, $data, $field)
{
return Systems::where('code', $value)->exists();
}
}