AuthController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserInfo;
  4. use App\Services\AuthService;
  5. use Firebase\JWT\JWT;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\Response;
  8. use Illuminate\Support\Facades\App;
  9. use Illuminate\Support\Facades\Storage;
  10. class AuthController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return Response
  16. */
  17. public function index()
  18. {
  19. //
  20. }
  21. /**
  22. * Store a newly created resource in storage.
  23. *
  24. * @return Response
  25. */
  26. public function store(Request $request)
  27. {
  28. //
  29. }
  30. /**
  31. * Display the specified resource.
  32. *
  33. * @param int $id
  34. * @return Response
  35. */
  36. public function show($id)
  37. {
  38. //
  39. }
  40. /**
  41. * Update the specified resource in storage.
  42. *
  43. * @param int $id
  44. * @return Response
  45. */
  46. public function update(Request $request, $id)
  47. {
  48. //
  49. }
  50. /**
  51. * Remove the specified resource from storage.
  52. *
  53. * @param int $id
  54. * @return Response
  55. */
  56. public function destroy($id)
  57. {
  58. //
  59. }
  60. public function signIn(Request $request)
  61. {
  62. $query = UserInfo::where(function ($query) use ($request) {
  63. $query->where('username', $request->input('username'))
  64. ->where('password', md5($request->input('password')));
  65. })
  66. ->orWhere(function ($query) use ($request) {
  67. $query->where('email', $request->input('username'))
  68. ->where('password', md5($request->input('password')));
  69. });
  70. // Log::info($query->toSql());
  71. $user = $query->first();
  72. if ($user) {
  73. $ExpTime = time() + 60 * 60 * 24 * 365;
  74. $key = AuthService::getJwtKey();
  75. $payload = [
  76. 'nbf' => time(),
  77. 'exp' => $ExpTime,
  78. 'uid' => $user->userid,
  79. 'id' => $user->id,
  80. ];
  81. $jwt = JWT::encode($payload, $key, 'HS512');
  82. return $this->ok($jwt);
  83. } else {
  84. return $this->error('invalid token');
  85. }
  86. }
  87. public function getUserInfoByToken(Request $request)
  88. {
  89. $curr = AuthService::current($request);
  90. if (! $curr) {
  91. return $this->error('invalid token', 401, 401);
  92. }
  93. $userInfo = UserInfo::where('userid', $curr['user_uid'])
  94. ->first();
  95. $user = [
  96. 'id' => $curr['user_uid'],
  97. 'nickName' => $userInfo->nickname,
  98. 'realName' => $userInfo->username,
  99. 'avatar' => '',
  100. 'token' => \substr($request->header('Authorization'), 7),
  101. ];
  102. // role为空 返回[]
  103. $user['roles'] = [];
  104. if (! empty($userInfo->role)) {
  105. $roles = json_decode($userInfo->role);
  106. if (is_array($roles)) {
  107. $user['roles'] = $roles;
  108. }
  109. }
  110. if ($curr['user_uid'] === config('mint.admin.root_uuid')) {
  111. $user['roles'] = ['root'];
  112. }
  113. if ($userInfo->avatar) {
  114. $img = str_replace('.jpg', '_s.jpg', $userInfo->avatar);
  115. if (App::environment('local')) {
  116. $user['avatar'] = Storage::url($img);
  117. } else {
  118. $user['avatar'] = Storage::temporaryUrl($img, now()->addDays(6));
  119. }
  120. }
  121. return $this->ok($user);
  122. }
  123. }