init
This commit is contained in:
121
app/index/validate/Customer.php
Executable file
121
app/index/validate/Customer.php
Executable file
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
namespace app\index\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', '密码不能为空'],
|
||||
];
|
||||
|
||||
protected $scene = [
|
||||
'register_by_telephone' => ['telephone'],
|
||||
'register_by_email' => ['email'],
|
||||
'login' => ['telephone', 'password'],
|
||||
'update_tel' => ['new_telephone']
|
||||
];
|
||||
|
||||
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 '手机号格式错误';
|
||||
}
|
||||
|
||||
if ($this->currentScene == 'register_by_telephone')
|
||||
{
|
||||
$customer = model('customer')->where(['stat' => 0, 'telephone' => $value])->find();
|
||||
if ($customer)
|
||||
{
|
||||
return '该手机号已存在';
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if ($this->currentScene == 'login')
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return '验证场景不存在';
|
||||
}
|
||||
|
||||
return true;
|
||||
break;
|
||||
case 'email':
|
||||
if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/", $value))
|
||||
{
|
||||
return '邮箱格式错误';
|
||||
}
|
||||
|
||||
$customer = model('customer')->getBasicInfoByEmail($value);
|
||||
if ($this->currentScene == 'register_by_email')
|
||||
{
|
||||
if ($customer)
|
||||
{
|
||||
// 如果场景为注册,且该手机用户存在
|
||||
return '该邮箱已存在';
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return '验证场景不存在';
|
||||
}
|
||||
|
||||
case 'password':
|
||||
if ($this->currentScene == 'login')
|
||||
{
|
||||
$customer = model('customer')->where(['stat' => 0, 'telephone' => $telephone])->find();
|
||||
if (empty($customer))
|
||||
{
|
||||
return '手机号未注册';
|
||||
}
|
||||
|
||||
if ($customer['password'] != md5($value))
|
||||
{
|
||||
return '手机号或密码错误';
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!preg_match("/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/", $value))
|
||||
{
|
||||
return '密码必须包含8-20个字符,且包含数字和字母';
|
||||
}
|
||||
|
||||
return true;
|
||||
break;
|
||||
case 'new_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 '手机号格式错误';
|
||||
}
|
||||
|
||||
$customer = model('customer')->where(['stat' => 0, 'telephone' => $value])->find();
|
||||
if (!empty($customer))
|
||||
{
|
||||
return '改手机号已被注册';
|
||||
}
|
||||
|
||||
return true;
|
||||
break;
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user