Login.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 登录
  5. */
  6. class Login extends Admin{
  7. protected $User = null;
  8. protected $Role = null;
  9. public function init(){
  10. $this->User = model('User');
  11. $this->Role = model('Role');
  12. }
  13. public function index(){
  14. if($user = is_login('admin')){
  15. session('admin_auth', null);
  16. }
  17. $this->assign('meta_title','管理员登录');
  18. return $this->fetch();
  19. }
  20. public function check(){
  21. if ($this->request->isPost()) {
  22. if (config('captcha.on')) {
  23. $vercode = input('post.vercode');
  24. if(!captcha_check($vercode)){
  25. return json(['data'=>$vercode,'code'=>1,'msg'=>'验证码错误']);
  26. };
  27. }
  28. $adminrole = $this->Role->where(['name'=>'admin','state'=>1])->find();
  29. if (!$adminrole) {
  30. return json(['data'=>null,'code'=>1,'msg'=>'缺少管理员角色']);
  31. }
  32. $user_name = input('post.username');
  33. $user_password = input('post.password');
  34. $where = "u.name = '".$user_name."' and u.state = 1 and (r.pid = ".$adminrole['id']." or r.id = ".$adminrole['id'].")";
  35. $user = $this->User->field('u.id,u.name,u.password,u.state,u.role_id,u.salt,u.mobile')
  36. ->alias('u')
  37. ->join('Role r','r.id = u.role_id')
  38. ->where($where)->find();
  39. if(!$user || 1 != $user['state']) {
  40. output(1,'用户不存在或已被禁用');
  41. }
  42. $salt = $user['salt'];
  43. $user_password = md5(md5($user_password).$salt);
  44. if($user['password'] !== $user_password){
  45. output(1,'密码错误');
  46. }
  47. if($this->updateInfo($user)){
  48. $jump_url = url('admin/index/index');
  49. return json(['data'=>['jump_url'=>$jump_url],'code'=>0,'msg'=>'登录成功']);
  50. }else{
  51. return json(['data'=>'','code'=>1,'msg'=>'登录失败']);
  52. }
  53. }
  54. }
  55. //更新用户登录信息
  56. private function updateInfo($user){
  57. /* 更新登录信息 */
  58. $data = array(
  59. 'id' => $user['id'],
  60. 'login_times' => ['inc', 1],
  61. 'last_login_time' => $this->request->time(),
  62. 'last_login_ip' => $this->request->ip(),
  63. );
  64. if($this->User->update($data)){
  65. $auth['id'] = $user['id'];
  66. $auth['name'] = $user['name'];
  67. $auth['mobile'] = $user['mobile'];
  68. $auth['role'] = $user['role']->toArray();
  69. session('admin_auth', $auth);
  70. return true;
  71. }else{
  72. return false;
  73. }
  74. }
  75. public function forget(){
  76. if($this->request->isPost()){
  77. $username = input('post.username');
  78. if (empty($username)) {
  79. $this->output(1,'请输入用户名');
  80. }
  81. $user = $this->User->where(['name'=>$username])->find();
  82. if(!$user){
  83. $this->output(1,'用户名错误');
  84. }
  85. $mobile = input('post.mobile');
  86. if (empty($mobile)) {
  87. $this->output(1,'手机号码不能为空');
  88. }
  89. if($mobile != $user['mobile']){
  90. $this->output(1,'手机号错误');
  91. }
  92. $password = input('post.password');
  93. if (empty($password)) {
  94. $this->output(1,'请输入新的密码');
  95. }
  96. $this->checksms($mobile);
  97. $salt = $user['salt'];
  98. $user->password = md5(md5($password).$salt);
  99. $user->update_time = $this->request->time();
  100. $result = $user->save();
  101. if (!$result) {
  102. $this->output(1,'保存失败');
  103. }
  104. $jump_url = url('login/index');
  105. $this->output(0,'保存成功',['jump_url'=>$jump_url]);
  106. }else{
  107. $this->assign('meta_title','重置密码');
  108. return $this->fetch();
  109. }
  110. }
  111. }