2025-07-01 19:39:13 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\common\validate;
|
|
|
|
|
|
|
2025-07-09 19:11:57 +08:00
|
|
|
|
use app\common\model\Corp;
|
2025-07-01 19:39:13 +08:00
|
|
|
|
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',
|
2025-07-09 19:11:57 +08:00
|
|
|
|
'system_code' => 'require|checkSystemCode',
|
|
|
|
|
|
'corp_abbr' => 'require|checkCorpAbbr',
|
|
|
|
|
|
'feishu_code' => 'require|string'
|
2025-07-01 19:39:13 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
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' => '系统码不能为空',
|
2025-07-09 19:11:57 +08:00
|
|
|
|
'system_code.checkSystemCode' => '系统码不存在或不合法',
|
|
|
|
|
|
'corp_abbr.require' => '公司标识不能为空',
|
|
|
|
|
|
'corp_abbr.checkCorpAbbr' => '公司标识不存在或不合法',
|
|
|
|
|
|
'feishu_code.require' => '飞书code不能为空',
|
|
|
|
|
|
'feishu_code.string' => '飞书code必须是字符串'
|
2025-07-01 19:39:13 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
protected $scene = [
|
2025-07-09 19:11:57 +08:00
|
|
|
|
'upwd_register' => ['name', 'account', 'password', 'confirm_password'],
|
|
|
|
|
|
'feishu_register' => ['feishu_code'],
|
|
|
|
|
|
'upwd_login' => ['account' => 'require|checkAccount', 'password'],
|
|
|
|
|
|
'feishu_auth' => ['corp_abbr', 'system_code', 'login_type'],
|
|
|
|
|
|
'feishu_login' => ['feishu_code']
|
2025-07-01 19:39:13 +08:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 验证账号格式(手机号或邮箱)
|
|
|
|
|
|
*/
|
|
|
|
|
|
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();
|
|
|
|
|
|
}
|
2025-07-09 19:11:57 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 验证 corp_abbr 企业标识是否存在
|
|
|
|
|
|
*/
|
|
|
|
|
|
protected function checkCorpAbbr($value, $rule, $data, $field)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Corp::where('abbr', $value)->exists();
|
|
|
|
|
|
}
|
2025-07-01 19:39:13 +08:00
|
|
|
|
}
|