AccountController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. public function userInfo()
  93. {
  94. // 登录
  95. $user = $this->accountService->userInfo();
  96. return $this->success($user);
  97. }
  98. }