2
0

AuthController.php 4.2 KB

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