AuthController.php 4.3 KB

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