AuthController.php 3.5 KB

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