Login.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. $http = array();
  20. if(!empty($_SERVER['HTTP_REFERER'])){
  21. $http[] = $_SERVER['HTTP_REFERER'];
  22. }
  23. if (session('user_id')) {
  24. $this->success('已经登录', 'User/Index/index');
  25. }
  26. if (request()->isPost()) {
  27. $user_email = input('post.user_email');
  28. $user_password = input('post.user_password');
  29. // $captcha = input('post.captcha');
  30. $data = array(
  31. 'user_email' => $user_email,
  32. 'user_password' => $user_password,
  33. // 'captcha' => $captcha,
  34. );
  35. //验证数据 BEGIN
  36. $rule = [
  37. ['user_email', 'require|min:2', '帐号为必填|帐号长度至少为2位'],
  38. ['user_password', 'require|min:2', '密码为必填|帐号长度至少为6位'],
  39. // ['captcha', 'require|min:3', '验证码为必填|帐号长度至少为3位'],
  40. ];
  41. $validate = new Validate($rule);
  42. $validate_result = $validate->check($data);
  43. if (!$validate_result) {
  44. $this->error($validate->getError());
  45. }
  46. //验证数据 END
  47. // if (!captcha_check(input('post.captcha'))) {
  48. // //验证失败
  49. // $this->error('验证码错误');
  50. // }
  51. $condition['user_email'] = $user_email;
  52. $condition['user_password'] = md5($user_password);
  53. $user_info = db('user')->where($condition)->find();
  54. if (is_array($user_info) and !empty($user_info)) {
  55. //更新 admin 最新信息
  56. $update_info = array(
  57. // 'admin_login_num' => ($admin_info['admin_login_num'] + 1),
  58. 'user_loginTime' => TIMESTAMP
  59. );
  60. db('user')->where('user_id', $user_info['user_id'])->update($update_info);
  61. //设置 session
  62. session('user_id', $user_info['user_id']);
  63. session('user_email', $user_info['user_email']);
  64. if(!empty($http)){
  65. // header(location:$http);
  66. // echo '<script>parent.location.href='.$http.';</script>';
  67. header("location:http://homedata.test/home/product/details.html?id=2");
  68. }else{
  69. return $this->redirect('User/Index/index');
  70. }
  71. } else {
  72. $this->success('帐号密码错误');
  73. }
  74. } else {
  75. return $this->fetch();
  76. }
  77. }
  78. /**
  79. * 退出登录
  80. */
  81. public function logout()
  82. {
  83. //设置 session
  84. session(null);
  85. return $this->redirect('User/Login/index');
  86. exit;
  87. }
  88. }
  89. ?>