User.php 4.8 KB

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