User.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\api\controller;
  3. use Throwable;
  4. use ba\Captcha;
  5. use ba\ClickCaptcha;
  6. use think\facade\Config;
  7. use app\common\facade\Token;
  8. use app\common\controller\Frontend;
  9. use app\api\validate\User as UserValidate;
  10. class User extends Frontend
  11. {
  12. protected array $noNeedLogin = ['checkIn', 'logout'];
  13. protected array $noNeedPermission = ['index'];
  14. public function initialize(): void
  15. {
  16. parent::initialize();
  17. }
  18. /**
  19. * 会员签入(登录和注册)
  20. * @throws Throwable
  21. */
  22. public function checkIn(): void
  23. {
  24. $openMemberCenter = Config::get('buildadmin.open_member_center');
  25. if (!$openMemberCenter) {
  26. $this->error(__('Member center disabled'));
  27. }
  28. // 检查登录态
  29. if ($this->auth->isLogin()) {
  30. $this->success(__('You have already logged in. There is no need to log in again~'), [
  31. 'type' => $this->auth::LOGGED_IN
  32. ], $this->auth::LOGIN_RESPONSE_CODE);
  33. }
  34. if ($this->request->isPost()) {
  35. $params = $this->request->post(['tab', 'email', 'mobile', 'username', 'password', 'keep', 'captcha', 'captchaId', 'captchaInfo', 'registerType']);
  36. if (!in_array($params['tab'], ['login', 'register'])) {
  37. $this->error(__('Unknown operation'));
  38. }
  39. $validate = new UserValidate();
  40. try {
  41. $validate->scene($params['tab'])->check($params);
  42. } catch (Throwable $e) {
  43. $this->error($e->getMessage());
  44. }
  45. if ($params['tab'] == 'login') {
  46. $captchaObj = new ClickCaptcha();
  47. if (!$captchaObj->check($params['captchaId'], $params['captchaInfo'])) {
  48. $this->error(__('Captcha error'));
  49. }
  50. $res = $this->auth->login($params['username'], $params['password'], (bool)$params['keep']);
  51. } elseif ($params['tab'] == 'register') {
  52. $captchaObj = new Captcha();
  53. if (!$captchaObj->check($params['captcha'], ($params['registerType'] == 'email' ? $params['email'] : $params['mobile']) . 'user_register')) {
  54. $this->error(__('Please enter the correct verification code'));
  55. }
  56. $res = $this->auth->register($params['username'], $params['password'], $params['mobile'], $params['email']);
  57. }
  58. if (isset($res) && $res === true) {
  59. $this->success(__('Login succeeded!'), [
  60. 'userInfo' => $this->auth->getUserInfo(),
  61. 'routePath' => '/user'
  62. ]);
  63. } else {
  64. $msg = $this->auth->getError();
  65. $msg = $msg ?: __('Check in failed, please try again or contact the website administrator~');
  66. $this->error($msg);
  67. }
  68. }
  69. $this->success('', [
  70. 'accountVerificationType' => get_account_verification_type()
  71. ]);
  72. }
  73. public function logout(): void
  74. {
  75. if ($this->request->isPost()) {
  76. $refreshToken = $this->request->post('refreshToken', '');
  77. if ($refreshToken) Token::delete((string)$refreshToken);
  78. $this->auth->logout();
  79. $this->success();
  80. }
  81. }
  82. }