AccountController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\Transformer\Account\AccountTransformer;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Routing\Controller as BaseController;
  12. use Illuminate\Support\Facades\Redis;
  13. use Illuminate\Support\Facades\Validator;
  14. class AccountController extends BaseController
  15. {
  16. use ApiResponse;
  17. protected $accountService;
  18. public function __construct(
  19. AccountService $accountService
  20. ) {
  21. $this->accountService = $accountService;
  22. }
  23. public function index(Request $request)
  24. {
  25. // 入口页面禁止缓存,保证每次更新后普通刷新即可加载最新版本
  26. return response()
  27. ->view('index')
  28. ->header('Cache-Control', 'no-cache, must-revalidate')
  29. ->header('Pragma', 'no-cache')
  30. ->header('Expires', '0');
  31. }
  32. /**
  33. * 动漫音频管理平台入口
  34. *
  35. * @param Request $request
  36. * @return \Illuminate\Contracts\View\View
  37. */
  38. public function animeIndex(Request $request)
  39. {
  40. // 入口页面禁止缓存,保证每次更新后普通刷新即可加载最新版本
  41. return response()
  42. ->view('animeIndex')
  43. ->header('Cache-Control', 'no-cache, must-revalidate')
  44. ->header('Pragma', 'no-cache')
  45. ->header('Expires', '0');
  46. }
  47. /**
  48. * 登录
  49. *
  50. * @param Request $request
  51. * @return mixed
  52. * @throws ApiException
  53. */
  54. public function login(Request $request)
  55. {
  56. $all = $request->all();
  57. $account = trim(getProp($all, 'account'));
  58. $passwd = trim(getProp($all, 'passwd'));
  59. if (strlen($account) < 1 || strlen($passwd) < 1) {
  60. Utils::throwError(ErrorConst::PARAM_ERROR_CODE);
  61. }
  62. // 登录
  63. $user = $this->accountService->login($account, $passwd);
  64. return $this->success($user);
  65. }
  66. /**
  67. * 退出登录
  68. *
  69. * @return mixed
  70. */
  71. public function logout()
  72. {
  73. // 当前登录用户
  74. $token = Site::getToken();
  75. // 退出
  76. $result = $this->accountService->logout($token);
  77. return $this->success(['success' => $result ? 1 : 0]);
  78. }
  79. public function userInfo()
  80. {
  81. // 登录
  82. $user = $this->accountService->userInfo();
  83. return $this->success($user);
  84. }
  85. }