Login.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\user\controller;
  3. use think\Controller;
  4. use think\Lang;
  5. use think\Validate;
  6. class Login extends Controller
  7. {
  8. public function _initialize()
  9. {
  10. parent::_initialize();
  11. Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/login.lang.php');
  12. }
  13. /**
  14. * 登录
  15. * @return mixed
  16. */
  17. public function index()
  18. {
  19. if (session('user_id')) {
  20. $this->success('已经登录', 'User/Index/index');
  21. }
  22. if (request()->isPost()) {
  23. $user_email = input('post.user_email');
  24. $user_password = input('post.user_password');
  25. // $captcha = input('post.captcha');
  26. $http = input('post.http');
  27. $data = array(
  28. 'user_email' => $user_email,
  29. 'user_password' => $user_password,
  30. // 'captcha' => $captcha,
  31. );
  32. //验证数据 BEGIN
  33. $rule = [
  34. ['user_email', 'require|min:2', '帐号为必填|帐号长度至少为2位'],
  35. ['user_password', 'require|min:2', '密码为必填|帐号长度至少为6位'],
  36. // ['captcha', 'require|min:3', '验证码为必填|帐号长度至少为3位'],
  37. ];
  38. $validate = new Validate($rule);
  39. $validate_result = $validate->check($data);
  40. if (!$validate_result) {
  41. $this->error($validate->getError());
  42. }
  43. //验证数据 END
  44. // if (!captcha_check(input('post.captcha'))) {
  45. // //验证失败
  46. // $this->error('验证码错误');
  47. // }
  48. $condition['user_email'] = $user_email;
  49. $condition['user_password'] = md5($user_password);
  50. $user_info = db('user')->where($condition)->find();
  51. if (is_array($user_info) and !empty($user_info)) {
  52. //更新 admin 最新信息
  53. $update_info = array(
  54. // 'admin_login_num' => ($admin_info['admin_login_num'] + 1),
  55. 'user_loginTime' => TIMESTAMP
  56. );
  57. db('user')->where('user_id', $user_info['user_id'])->update($update_info);
  58. //设置 session
  59. session('user_id', $user_info['user_id']);
  60. session('user_email', $user_info['user_email']);
  61. $login = input('get.login');
  62. if($login === '0'){
  63. $path = parse_url($http);
  64. $oldPath = substr($path['path'],0,strrpos($path['path'],"."));
  65. return $this->redirect($oldPath . '?' . $path['query']);
  66. }else{
  67. return $this->redirect('User/Index/index');
  68. }
  69. } else {
  70. $this->success('帐号密码错误');
  71. }
  72. } else {
  73. if(!empty($_SERVER['HTTP_REFERER'])){
  74. $http = $_SERVER['HTTP_REFERER'];
  75. $this->assign('http', $http);
  76. }
  77. return $this->fetch();
  78. }
  79. }
  80. /**
  81. * 退出登录
  82. */
  83. public function logout()
  84. {
  85. //设置 session
  86. session(null);
  87. return $this->redirect('User/Login/index');
  88. exit;
  89. }
  90. }
  91. ?>