Files
orico-official-website-old/app/mobile/validate/Customer.php
2024-10-29 14:04:59 +08:00

72 lines
2.7 KiB
PHP
Executable File

<?php
namespace app\mobile\validate;
use think\Validate;
class Customer extends Validate
{
protected $rule = [
['telephone', 'require|number|check_value:telephone', '手机号不能为空|手机号必须为数字'],
['firstname', 'require|min:2|max:20', '用户名不能为空|用户名至少2个字符|用户名最多20个字符'],
['password', 'require|check_value:password', '密码不能为空'],
['repassword', 'require|confirm:password', '请确认您的密码|两次密码不一致'],
];
protected $scene = [
'register' => ['telephone', 'firstname', 'password', 'repassword'],
'login' => ['telephone', 'password'],
'change_pwd' => ['telephone', 'password', 'repassword']
];
protected function check_value($value, $rule) {
$telephone = input('telephone');
switch ($rule) {
case 'telephone':
if (!preg_match("/^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/", $value)) {
return '手机号错误';
}
$user = model('customer')->where(['stat' => 0, 'telephone' => $value])->find();
if ($this->currentScene == 'register') {
if ($user) {
// 如果场景为注册,且该手机用户存在
return '该手机号已存在';
} else {
return true;
}
}elseif ($this->currentScene == 'login' || $this->currentScene == 'change_pwd') {
if (!$user) {
// 如果场景为登录或者修改密码,且找不到该手机用户
return '该手机号不存在';
} else {
return true;
}
}else {
return '验证场景不存在';
}
return true;
break;
case 'password':
if ($this->currentScene != 'login') {
if (!preg_match("/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/", $value)) {
return '密码必须包含8-20个字符,且包含数字和字母';
} else {
return true;
}
}
$user = model('customer')->where(['stat' => 0, 'telephone' => $telephone])->find();
if (!$user || $user['password'] != md5($value)) {
return '密码错误';
}
return true;
break;
default:
break;
}
}
}