2
0

AuthController.php 3.7 KB

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