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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
| <?php namespace app\user\controller; use app\common\model\System; use app\common\model\Users as M;
use think\db\exception\DbException; use think\exception\HttpResponseException; use think\facade\Db; use think\facade\View; use think\captcha\facade\Captcha; use think\facade\Request; use mailer\tp6\Mailer; use think\facade\Config; use think\Response;
class Login { private $system; public function __construct(){ $system = System::getListField()->toArray(); $system = sysgem_setup($system); $systemArr = []; foreach ($system as $k => $v) { $systemArr[$v['field']] = $v['value']; } $this->system = $systemArr; View::assign(['config'=>$this->system]);
$social = Db::name('plugin')->where(['type'=>'login','status'=>1])->select()->toArray(); View::assign(['social'=>$social]);
$linkList = Db::name('link')->where('open',1)->order('sort asc')->select(); View::assign(['linkList'=>$linkList]); }
public function index(){ if(Request::isPost()){ $data = input('post.'); if($this->system['message_code']==1){ if (!captcha_check($data['vercode'])) { return ['code' => 0, 'msg' => '验证码错误']; } } $user=Db::name('users')->where('username',$data['username'])->find(); if($user){ if($user['password'] == md5($user['token'].$data['password'])){ session('user',$user); return ['code' => 1, 'msg' => '登录成功','url' => (string)url('user/index/index')]; }else{ return ['code' => 0, 'msg' =>'登录失败']; } }else{ return ['code' => 0, 'msg' =>'用户名不存在']; } }
$view =[ 'title'=>'用户登录' ]; return view('',$view); } public function reg(){ if(Request::isPost()){ $data = input('post.'); if (!captcha_check($data['vercode'])) { return ['code' => 1, 'msg' => '验证码错误']; } if(trim($data['username'])==''){ return ['code' => 0, 'msg' =>'请重新设置用户名']; } if(strlen($data['password']) < 6){ return ['code' => 0, 'msg' =>'密码不能低于6位字符']; } $data['token'] = rand_string(); if(Db::name('users')->where('email',$data['email'])->find()){ return ['code' => 0, 'msg' =>'该email已被使用']; } if(Db::name('users')->where('username',$data['username'])->find()){ return ['code' => 0, 'msg' =>'该用户名已被使用']; }
$data['create_time'] = time(); if($data['repass'] == $data['password']){ $data['password'] = md5($data['token'].$data['password']); unset($data['repass']); }else{ return ['status'=>0,'code' => 0, 'msg' =>'两次输入密码不一致']; } $data['avatar'] = '/static/user/images/avatar/default.png'; unset($data['vercode']); $id = Db::name('users')->insertGetId($data); if($id){ $session = rand_string(20); Db::name('users')->where('id',$id)->update(['session'=>$session]); $user = Db::name('users')->where('id',$id)->find(); session('user',$user); return ['code' => 1, 'msg' =>'注册成功!','url'=>(string)url('user/index/index')]; }else{ return ['code' => 0, 'msg' =>'注册失败!']; } } $view =[ 'title'=>'用户注册' ]; return view('',$view); }
public function forget(){ if(request()->isPost()) { $sender = input('email'); $code =input('code'); $inValid = true; if(!$code){ return ['status'=>0,'code' => 0, 'msg' =>'请输入邮件验证码!']; } $data = session('validate_code'); $timeOut = $data['time']; if($data['code'] != $code || $data['sender']!=$sender){ $inValid = false; } $password = input('password'); $password2 = input('password2'); if(strlen($password) < 6){ return ['status'=>0,'code' => 0, 'msg' =>'密码不能低于6位字符!']; } if($password != $password2){ return ['status'=>0,'code' => 0, 'msg' =>'两次输入密码不一致!']; } if(empty($data)){ return ['status'=>0,'code' => 0, 'msg' =>'请先获取验证码!']; }elseif($timeOut < time()){ return ['status'=>0,'code' => 0, 'msg' =>'验证码已超时失效!']; }elseif(!$inValid) { return ['status'=>0,'code' => 0, 'msg' =>'验证失败,验证码有误!']; }else{ $data['is_check'] = 1; session('user.token',$data); $token = Db::name('users')->where('email',$sender)->value('token'); Db::name('users')->where('email',$sender)->update(['password'=>md5($token.$password)]); return ['status'=>0,'code' => 1, 'msg' =>'密码找回成功!','url'=>(string)url('user/login/index')]; } }else { $view =[ 'title'=>'找回密码' ]; return view('',$view); } } public function info(){ if(!session('user.id')){ return redirect(url('user/login/index')); } if(request()->isPost()) { $data = input('post.'); if(trim($data['username'])==''){ return json(['code' => 0, 'msg' =>'请重新设置用户名']); }
if(M::where([['username','=',$data['username']],['id','<>',session('user.id')]])->find()){ return json(['code' => 0, 'msg' =>'该用户名已被使用']); }
if(strlen($data['password']) < 6){ return json(['code' => 0, 'msg' =>'密码不能低于6位字符']); }
if($data['repass'] == $data['password']){ $token = session('user.token'); $data['password'] = md5($token.$data['password']); unset($data['repass']); }else{ return json(['status'=>0,'code' => 0, 'msg' =>'两次输入密码不一致']); } $data['id'] = session('user.id'); $result = M::editSave($data); if($result['code']==0){ return json(['status'=>0,'code' => 1, 'msg' =>'设置成功!','url'=>(string)url('user/index/index')]); }else{ return json(['status'=>0,'code' => 0, 'msg' =>'设置失败!']); } }else{ $user_id = session('user.id'); $info = M::edit($user_id); $view = [ 'info' => $info, ]; return view('info', $view); } } public function sendEmail(){ if(request()->isPost()) { $sender = input('email'); $sms_time_out = $this->system['time_out']; $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' => '邮箱码格式有误']); } Config::set( [ 'driver'=>'smtp', 'host' => $this->system['smtp_server'], 'port' => $this->system['smtp_port'], 'addr'=> $this->system['smtp_email'], 'pass'=> $this->system['smtp_pwd'], 'name'=> $this->system['smtp_user'], ], 'mail' ); $mailer = Mailer::instance(); $sendStatus = $mailer->from($this->system['smtp_email'], $this->system['smtp_user']) ->to($sender) ->subject('验证码') ->text( '您好,你的验证码是:' . $code) ->send(); if ($sendStatus) { $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 json(['code' => -1, 'msg' => '验证码发送失败,请联系管理员']); } }else{ return json(['code' => -1, 'msg' => '非法请求']); }
} public function captcha(){ return Captcha::create(); } public function unbind(){ try { Db::name('user_oauth')->where("uid", session('user.id'))->delete(); success('解绑成功!'); } catch (DbException $e) { error($e->getMessage()); }
}
}
|