User.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. public function initialize(): void
  14. {
  15. parent::initialize();
  16. }
  17. /**
  18. * 会员签入(登录和注册)
  19. * @throws Throwable
  20. */
  21. public function checkIn(): void
  22. {
  23. $openMemberCenter = Config::get('buildadmin.open_member_center');
  24. if (!$openMemberCenter) {
  25. $this->error(__('Member center disabled'));
  26. }
  27. // 检查登录态
  28. if ($this->auth->isLogin()) {
  29. $this->success(__('You have already logged in. There is no need to log in again~'), [
  30. 'type' => $this->auth::LOGGED_IN
  31. ], $this->auth::LOGIN_RESPONSE_CODE);
  32. }
  33. $userLoginCaptchaSwitch = Config::get('buildadmin.user_login_captcha');
  34. if ($this->request->isPost()) {
  35. $params = $this->request->post(['tab', 'email', 'mobile', 'username', 'password', 'keep', 'captcha', 'captchaId', 'captchaInfo', 'registerType']);
  36. // 提前检查 tab ,然后将以 tab 值作为数据验证场景
  37. if (!in_array($params['tab'] ?? '', ['login', 'register'])) {
  38. $this->error(__('Unknown operation'));
  39. }
  40. $validate = new UserValidate();
  41. try {
  42. $validate->scene($params['tab'])->check($params);
  43. } catch (Throwable $e) {
  44. $this->error($e->getMessage());
  45. }
  46. if ($params['tab'] == 'login') {
  47. if ($userLoginCaptchaSwitch) {
  48. $captchaObj = new ClickCaptcha();
  49. if (!$captchaObj->check($params['captchaId'], $params['captchaInfo'])) {
  50. $this->error(__('Captcha error'));
  51. }
  52. }
  53. $res = $this->auth->login($params['username'], $params['password'], !empty($params['keep']));
  54. } elseif ($params['tab'] == 'register') {
  55. $captchaObj = new Captcha();
  56. if (!$captchaObj->check($params['captcha'], $params[$params['registerType']] . 'user_register')) {
  57. $this->error(__('Please enter the correct verification code'));
  58. }
  59. $res = $this->auth->register($params['username'], $params['password'], $params['mobile'], $params['email']);
  60. }
  61. if (isset($res) && $res === true) {
  62. $this->success(__('Login succeeded!'), [
  63. 'userInfo' => $this->auth->getUserInfo(),
  64. 'routePath' => '/user'
  65. ]);
  66. } else {
  67. $msg = $this->auth->getError();
  68. $msg = $msg ?: __('Check in failed, please try again or contact the website administrator~');
  69. $this->error($msg);
  70. }
  71. }
  72. $this->success('', [
  73. 'userLoginCaptchaSwitch' => $userLoginCaptchaSwitch,
  74. 'accountVerificationType' => get_account_verification_type()
  75. ]);
  76. }
  77. public function logout(): void
  78. {
  79. if ($this->request->isPost()) {
  80. $refreshToken = $this->request->post('refreshToken', '');
  81. if ($refreshToken) Token::delete((string)$refreshToken);
  82. $this->auth->logout();
  83. $this->success();
  84. }
  85. }
  86. }