123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?php
- namespace app\admin\controller;
- /**
- * 登录
- */
- class Login extends Admin{
- protected $User = null;
- protected $Role = null;
- public function init(){
- $this->User = model('User');
- $this->Role = model('Role');
- }
- public function index(){
- if($user = is_login('admin')){
- session('admin_auth', null);
- }
- $this->assign('meta_title','管理员登录');
- return $this->fetch();
- }
- public function check(){
- if ($this->request->isPost()) {
- if (config('captcha.on')) {
- $vercode = input('post.vercode');
- if(!captcha_check($vercode)){
- return json(['data'=>$vercode,'code'=>1,'msg'=>'验证码错误']);
- };
- }
- $adminrole = $this->Role->where(['name'=>'admin','state'=>1])->find();
- if (!$adminrole) {
- return json(['data'=>null,'code'=>1,'msg'=>'缺少管理员角色']);
- }
- $user_name = input('post.username');
- $user_password = input('post.password');
- $where = "u.name = '".$user_name."' and u.state = 1 and (r.pid = ".$adminrole['id']." or r.id = ".$adminrole['id'].")";
- $user = $this->User->field('u.id,u.name,u.password,u.state,u.role_id,u.salt,u.mobile')
- ->alias('u')
- ->join('Role r','r.id = u.role_id')
- ->where($where)->find();
- if(!$user || 1 != $user['state']) {
- output(1,'用户不存在或已被禁用');
- }
- $salt = $user['salt'];
- $user_password = md5(md5($user_password).$salt);
- if($user['password'] !== $user_password){
- output(1,'密码错误');
- }
- if($this->updateInfo($user)){
- $jump_url = url('admin/index/index');
- return json(['data'=>['jump_url'=>$jump_url],'code'=>0,'msg'=>'登录成功']);
- }else{
- return json(['data'=>'','code'=>1,'msg'=>'登录失败']);
- }
- }
- }
- //更新用户登录信息
- private function updateInfo($user){
- /* 更新登录信息 */
- $data = array(
- 'id' => $user['id'],
- 'login_times' => ['inc', 1],
- 'last_login_time' => $this->request->time(),
- 'last_login_ip' => $this->request->ip(),
- );
- if($this->User->update($data)){
- $auth['id'] = $user['id'];
- $auth['name'] = $user['name'];
- $auth['mobile'] = $user['mobile'];
- $auth['role'] = $user['role']->toArray();
- session('admin_auth', $auth);
- return true;
- }else{
- return false;
- }
- }
- public function forget(){
- if($this->request->isPost()){
- $username = input('post.username');
- if (empty($username)) {
- $this->output(1,'请输入用户名');
- }
- $user = $this->User->where(['name'=>$username])->find();
- if(!$user){
- $this->output(1,'用户名错误');
- }
- $mobile = input('post.mobile');
- if (empty($mobile)) {
- $this->output(1,'手机号码不能为空');
- }
- if($mobile != $user['mobile']){
- $this->output(1,'手机号错误');
- }
- $password = input('post.password');
- if (empty($password)) {
- $this->output(1,'请输入新的密码');
- }
- $this->checksms($mobile);
- $salt = $user['salt'];
- $user->password = md5(md5($password).$salt);
- $user->update_time = $this->request->time();
- $result = $user->save();
- if (!$result) {
- $this->output(1,'保存失败');
- }
- $jump_url = url('login/index');
- $this->output(0,'保存成功',['jump_url'=>$jump_url]);
- }else{
- $this->assign('meta_title','重置密码');
- return $this->fetch();
- }
- }
- }
|