You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.9 KiB
69 lines
1.9 KiB
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\validate;
|
|
|
|
use think\facade\Db;
|
|
use think\Validate;
|
|
|
|
class User extends Validate
|
|
{
|
|
/**
|
|
* 定义验证规则
|
|
* 格式:'字段名' => ['规则1','规则2'...]
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $rule = [
|
|
'phone' => 'require|mobile',
|
|
'password' => 'require|min:6|max:20',
|
|
'sms_code' => 'require',
|
|
];
|
|
|
|
/**
|
|
* 定义错误信息
|
|
* 格式:'字段名.规则名' => '错误信息'
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $message = [
|
|
'phone.require' => '手机号必填',
|
|
'phone.mobile' => '手机号不正确',
|
|
'password.require' => '密码必填',
|
|
'password.min' => '密码长度最短为6个字符',
|
|
'password.max' => '密码长度最长为20个字符',
|
|
'sms_code' => '短信验证码必填'
|
|
];
|
|
|
|
protected $scene = [
|
|
'login' => ['phone','password'],
|
|
'register' => ['phone','password','sms_code'],
|
|
'retrieve' => ['phone','password','sms_code'],
|
|
'modifyPassword' => ['password'],
|
|
'sendCode' => ['phone']
|
|
];
|
|
|
|
/**
|
|
* 手机号短信验证
|
|
* @param $mobile
|
|
* @param $code
|
|
* @return bool|string
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function checkCode($mobile,$code)
|
|
{
|
|
$pin_info = Db::name('pincode')->where('mobile',$mobile)->field('code,time')->find();
|
|
if (empty($pin_info)) {
|
|
return '短信验证码错误';
|
|
}
|
|
if ($pin_info['time'] < time() - 300) {
|
|
return '短信验证码已过期,请重新获取';
|
|
}
|
|
if ($code != $pin_info['code']) {
|
|
return '短信验证码错误';
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|