LoginController.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Powerless < wzxaini9@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\user\controller;
  12. use think\facade\Validate;
  13. use cmf\controller\HomeBaseController;
  14. use app\user\model\UserModel;
  15. class LoginController extends HomeBaseController
  16. {
  17. /**
  18. * 登录
  19. */
  20. public function index()
  21. {
  22. $redirect = $this->request->param("redirect");
  23. if (empty($redirect)) {
  24. $redirect = $this->request->server('HTTP_REFERER');
  25. } else {
  26. if (strpos($redirect, '/') === 0 || strpos($redirect, 'http') === 0) {
  27. } else {
  28. $redirect = base64_decode($redirect);
  29. }
  30. }
  31. if(!empty($redirect)){
  32. session('login_http_referer', $redirect);
  33. }
  34. if (cmf_is_user_login()) { //已经登录时直接跳到首页
  35. return redirect($this->request->root() . '/');
  36. } else {
  37. return $this->fetch(":login");
  38. }
  39. }
  40. /**
  41. * 登录验证提交
  42. */
  43. public function doLogin()
  44. {
  45. if ($this->request->isPost()) {
  46. $validate = new \think\Validate();
  47. $validate->rule([
  48. 'captcha' => 'require',
  49. 'username' => 'require',
  50. 'password' => 'require|min:6|max:32',
  51. ]);
  52. $validate->message([
  53. 'username.require' => '用户名不能为空',
  54. 'password.require' => '密码不能为空',
  55. 'password.max' => '密码不能超过32个字符',
  56. 'password.min' => '密码不能小于6个字符',
  57. 'captcha.require' => '验证码不能为空',
  58. ]);
  59. $data = $this->request->post();
  60. if (!$validate->check($data)) {
  61. $this->error($validate->getError());
  62. }
  63. if (!cmf_captcha_check($data['captcha'])) {
  64. $this->error(lang('CAPTCHA_NOT_RIGHT'));
  65. }
  66. $userModel = new UserModel();
  67. $user['user_pass'] = $data['password'];
  68. if (Validate::is($data['username'], 'email')) {
  69. $user['user_email'] = $data['username'];
  70. $log = $userModel->doEmail($user);
  71. } else if (cmf_check_mobile($data['username'])) {
  72. $user['mobile'] = $data['username'];
  73. $log = $userModel->doMobile($user);
  74. } else {
  75. $user['user_login'] = $data['username'];
  76. $log = $userModel->doName($user);
  77. }
  78. $session_login_http_referer = session('login_http_referer');
  79. $redirect = empty($session_login_http_referer) ? $this->request->root() : $session_login_http_referer;
  80. switch ($log) {
  81. case 0:
  82. cmf_user_action('login');
  83. $this->success(lang('LOGIN_SUCCESS'), $redirect);
  84. break;
  85. case 1:
  86. $this->error(lang('PASSWORD_NOT_RIGHT'));
  87. break;
  88. case 2:
  89. $this->error('账户不存在');
  90. break;
  91. case 3:
  92. $this->error('账号被禁止访问系统');
  93. break;
  94. default :
  95. $this->error('未受理的请求');
  96. }
  97. } else {
  98. $this->error("请求错误");
  99. }
  100. }
  101. /**
  102. * 找回密码
  103. */
  104. public function findPassword()
  105. {
  106. return $this->fetch('/find_password');
  107. }
  108. /**
  109. * 用户密码重置
  110. */
  111. public function passwordReset()
  112. {
  113. if ($this->request->isPost()) {
  114. $validate = new \think\Validate();
  115. $validate->rule([
  116. 'captcha' => 'require',
  117. 'verification_code' => 'require',
  118. 'password' => 'require|min:6|max:32',
  119. ]);
  120. $validate->message([
  121. 'verification_code.require' => '验证码不能为空',
  122. 'password.require' => '密码不能为空',
  123. 'password.max' => '密码不能超过32个字符',
  124. 'password.min' => '密码不能小于6个字符',
  125. 'captcha.require' => '验证码不能为空',
  126. ]);
  127. $data = $this->request->post();
  128. if (!$validate->check($data)) {
  129. $this->error($validate->getError());
  130. }
  131. $captchaId = empty($data['_captcha_id']) ? '' : $data['_captcha_id'];
  132. if (!cmf_captcha_check($data['captcha'], $captchaId)) {
  133. $this->error('验证码错误');
  134. }
  135. $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
  136. if (!empty($errMsg)) {
  137. $this->error($errMsg);
  138. }
  139. $userModel = new UserModel();
  140. if (Validate::is($data['username'], 'email')) {
  141. $log = $userModel->emailPasswordReset($data['username'], $data['password']);
  142. } else if (cmf_check_mobile($data['username'])) {
  143. $user['mobile'] = $data['username'];
  144. $log = $userModel->mobilePasswordReset($data['username'], $data['password']);
  145. } else {
  146. $log = 2;
  147. }
  148. switch ($log) {
  149. case 0:
  150. $this->success('密码重置成功', cmf_url('user/Profile/center'));
  151. break;
  152. case 1:
  153. $this->error("您的账户尚未注册");
  154. break;
  155. case 2:
  156. $this->error("您输入的账号格式错误");
  157. break;
  158. default :
  159. $this->error('未受理的请求');
  160. }
  161. } else {
  162. $this->error("请求错误");
  163. }
  164. }
  165. }