User.php 3.7 KB

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