AccountController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Http\Controllers\Account;
  3. use App\Facade\Site;
  4. use App\Consts\ErrorConst;
  5. use App\Exceptions\ApiException;
  6. use App\Libs\ApiResponse;
  7. use App\Libs\Utils;
  8. use App\Services\Account\AccountService;
  9. use App\Services\Account\CaptchaService;
  10. use App\Transformer\Account\AccountTransformer;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Routing\Controller as BaseController;
  13. use Illuminate\Support\Facades\Redis;
  14. use Illuminate\Support\Facades\Validator;
  15. class AccountController extends BaseController
  16. {
  17. use ApiResponse;
  18. protected $accountService;
  19. protected $captchaService;
  20. public function __construct(
  21. AccountService $accountService,
  22. CaptchaService $captchaService
  23. ) {
  24. $this->accountService = $accountService;
  25. $this->captchaService = $captchaService;
  26. }
  27. public function index(Request $request)
  28. {
  29. // 入口页面禁止缓存,保证每次更新后普通刷新即可加载最新版本
  30. return response()
  31. ->view('index')
  32. ->header('Cache-Control', 'no-store, no-cache, must-revalidate')
  33. ->header('Pragma', 'no-cache')
  34. ->header('Expires', '0')
  35. ->header('X-Accel-Expires', '0');
  36. }
  37. /**
  38. * 动漫音频管理平台入口
  39. *
  40. * @param Request $request
  41. * @return \Illuminate\Contracts\View\View
  42. */
  43. public function animeIndex(Request $request)
  44. {
  45. // 入口页面禁止缓存,保证每次更新后普通刷新即可加载最新版本
  46. return response()
  47. ->view('animeIndex')
  48. ->header('Cache-Control', 'no-store, no-cache, must-revalidate')
  49. ->header('Pragma', 'no-cache')
  50. ->header('Expires', '0')
  51. ->header('X-Accel-Expires', '0');
  52. }
  53. /**
  54. * 登录
  55. *
  56. * @param Request $request
  57. * @return mixed
  58. * @throws ApiException
  59. */
  60. public function login(Request $request)
  61. {
  62. $all = $request->all();
  63. $account = trim(getProp($all, 'account'));
  64. $passwd = trim(getProp($all, 'passwd'));
  65. if (strlen($account) < 1 || strlen($passwd) < 1) {
  66. Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
  67. }
  68. // 图片验证码校验(config('captcha.enabled') 开启时生效)
  69. if (config('captcha.enabled')) {
  70. $this->captchaService->verify(
  71. (string)getProp($all, 'captcha_key'),
  72. (string)getProp($all, 'captcha_code')
  73. );
  74. }
  75. // 登录
  76. $user = $this->accountService->login($account, $passwd);
  77. return $this->success($user);
  78. }
  79. /**
  80. * 退出登录
  81. *
  82. * @return mixed
  83. */
  84. public function logout()
  85. {
  86. // 当前登录用户
  87. $token = Site::getToken();
  88. // 退出
  89. $result = $this->accountService->logout($token);
  90. return $this->success(['success' => $result ? 1 : 0]);
  91. }
  92. /**
  93. * 刷新 token
  94. *
  95. * 通过 refresh_token 换取新的 token 与 refresh_token(token 过期后调用)
  96. *
  97. * @param Request $request
  98. * @return mixed
  99. * @throws ApiException
  100. */
  101. public function refreshToken(Request $request)
  102. {
  103. $refreshToken = getProp($request->all(), 'refresh_token');
  104. $user = $this->accountService->refreshToken($refreshToken);
  105. return $this->success($user);
  106. }
  107. public function userInfo()
  108. {
  109. // 登录
  110. $user = $this->accountService->userInfo();
  111. return $this->success($user);
  112. }
  113. /**
  114. * 修改账号(仅允许修改一次)
  115. *
  116. * @param Request $request
  117. * @return mixed
  118. * @throws ApiException
  119. */
  120. public function updateAccount(Request $request)
  121. {
  122. $account = (string)getProp($request->all(), 'account', '');
  123. $this->accountService->updateAccount((int)Site::getUid(), $account);
  124. return $this->success(['success' => 1]);
  125. }
  126. }