Login.php
7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace app\user\controller;
use think\Controller;
use think\facade\Request;
use think\captcha\Captcha;
class Login extends Controller{
protected $sys;
public function initialize(){
if (session('user.id')) {
$this->redirect('index/index');
}
$this->sys = cache('System');
$this->assign('sys',$this->sys);
}
public function index(){
if(request()->isPost()) {
$table = db('users');
$username = input('username');
$password = input('password');
if($this->sys['code']=='open'){
$code = input('vercode');
if(!$this->check($code)){
return array('code' => 0, 'msg' => '验证码错误');
}
}
if(!$username || !$password){
return array('code'=>0,'msg'=>'请填写账号或密码');
}
$user = $table->where("mobile","=",$username)->whereOr('email','=',$username)->find();
if(!$user){
return array('code'=>0,'msg'=>'账号不存在!');
}elseif(md5($password) != $user['password']){
return array('code'=>0,'msg'=>'密码错误!');
}elseif($user['is_lock'] == 1){
return array('code'=>0,'msg'=>'账号异常已被锁定!!!');
}else{
$sessionUser = $table->where("id",'=',$user['id'])->find();
$openid = db('oauth')->where([['uid','=',$user['id']],['type','=','qq']])->value('openid');
if($openid){
$sessionUser['qq'] = 1;
}
session('user',$sessionUser);
return array('code'=>1,'msg'=>'登录成功','url' => url('index/index'));
}
}else{
$plugin = db('plugin')->where(['type'=>'login','status'=>1])->select();
$this->assign('plugin', $plugin);
$this->assign('title','会员登录');
return $this->fetch();
}
}
public function verify(){
$config = [
// 验证码字体大小
'fontSize' => 25,
// 验证码位数
'length' => 4,
// 关闭验证码杂点
'useNoise' => false,
'bg' => [255,255,255],
];
$captcha = new Captcha($config);
return $captcha->entry();
}
public function check($code){
return captcha_check($code);
}
public function reg(){
if(Request::isAjax()) {
$data = input('post.');
if($this->sys['code']=='open') {
$code = $data['vercode'];
if (!$this->check($code)) {
return array('code' => 0, 'msg' => '验证码错误');
}
}
$is_validated = 0 ;
if(is_email($data['email'])){
$is_validated = 1;
$data['email_validated'] = 1;
if(get_user_info($data['email'],1)){
return array('code'=>-1,'msg'=>'邮箱账号已存在');
}
}
if(is_mobile_phone($data['email'])){
$is_validated = 1;
$data['mobile_validated'] = 1;
$data['mobile'] = $data['email']; //手机注册
unset($data['email']);
if(get_user_info($data['mobile'],2)){
return array('code'=>-1,'msg'=>'手机账号已存在');
}
}
if($is_validated != 1){
return array('code'=>0,'msg'=>'请用手机号或邮箱注册');
}
if(!$data['username'] || !$data['password']){
return array('code'=>-1,'msg'=>'请输入昵称或密码');
}
//验证两次密码是否匹配
if($data['password'] != $data['password2']){
return array('code'=>-1,'msg'=>'两次输入密码不一致');
}
unset($data['password2']);
unset($data['vercode']);
//验证是否存在用户名
$data['password'] = md5($data['password']);
$data['reg_time'] = time();
$id = db('users')->insertGetId($data);
if($id === false){
return array('code'=>-1,'msg'=>'注册失败');
}
$user = db('users')->field('id,username')->where("id", $id)->find();
session('user',$user);
return array('code'=>1,'msg'=>'注册成功','result'=>$user);
}else {
$plugin = db('plugin')->where(['type' => 'login', 'status' => 1])->select();
$this->assign('plugin', $plugin);
$this->assign('title', '会员注册');
return $this->fetch();
}
}
public function forget(){
if(request()->isPost()) {
$sender = input('email');
$code =input('code');
$inValid = true; //验证码失效
if(!$code){
return array('code'=>-1,'msg'=>'请输入邮件验证码');
}
$data = session('validate_code');
$timeOut = $data['time'];
if($data['code'] != $code || $data['sender']!=$sender){
$inValid = false;
}
$password = input('password');
$password2 = input('password2');
if($password != $password2){
return array('code'=>-1,'msg'=>'两次输入密码不一致');
}
if(empty($data)){
return array('code'=>-1,'msg'=>'请先获取验证码');
}elseif($timeOut < time()){
return array('code'=>-1,'msg'=>'验证码已超时失效');
}elseif(!$inValid) {
return array('status'=>-1,'msg'=>'验证失败,验证码有误');
}else{
$data['is_check'] = 1; //标示验证通过
session('validate_code',$data);
db('users')->where('email',$sender)->update(['password'=>md5($password)]);
return array('code'=>1,'msg'=>'密码找回成功!');
}
}else{
$this->assign('title','找回密码');
return $this->fetch();
}
}
/**
* 发送邮件验证码
* @param $sender 接收人
* @return json
*/
public function sendEmail(){
if(request()->isPost()) {
$sender = input('email');
$sms_time_out = 180;
//获取上一次的发送时间
$send = session('validate_code');
if (!empty($send) && $send['time'] > time() && $send['sender'] == $sender) {
//在有效期范围内 相同号码不再发送
return json(['code' => -1, 'msg' => '规定时间内,不要重复发送验证码']);
}
$code = mt_rand(1000, 9999);
//检查是否邮箱格式
if (!is_email($sender)) {
return json(['code' => -1, 'msg' => '邮箱码格式有误']);
}
$send = send_email($sender, '验证码', '您好,你的验证码是:' . $code);
if ($send) {
$info['code'] = $code;
$info['sender'] = $sender;
$info['is_check'] = 0;
$info['time'] = time() + $sms_time_out; //有效验证时间
session('validate_code', $info);
return json(['code' => 1, 'msg' => '验证码已发送,请注意查收']);
}else{
return array('code' => -1, 'msg' => '验证码发送失败,请联系管理员');
}
}else{
return json(['code' => -1, 'msg' => '非法请求']);
}
}
}