UserApi.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Http\Resources\AiAssistant;
  4. use App\Models\AiModel;
  5. use App\Models\UserInfo;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Support\Facades\App;
  8. class UserApi
  9. {
  10. public static function getIdByName($name)
  11. {
  12. return UserInfo::where('username', $name)->value('userid');
  13. }
  14. public static function getIdByUuid($uuid)
  15. {
  16. return UserInfo::where('userid', $uuid)->value('id');
  17. }
  18. public static function getIntIdByName($name)
  19. {
  20. return UserInfo::where('username', $name)->value('id');
  21. }
  22. public static function getById($id)
  23. {
  24. $user = UserInfo::where('id', $id)->first();
  25. return UserApi::userInfo($user);
  26. }
  27. public static function getByName($name)
  28. {
  29. $user = UserInfo::where('username', $name)->first();
  30. return UserApi::userInfo($user);
  31. }
  32. public static function getByUuid($id)
  33. {
  34. $user = UserInfo::where('userid', $id)->first();
  35. if (!$user) {
  36. return AiAssistantApi::getByUuid($id);
  37. }
  38. return UserApi::userInfo($user);
  39. }
  40. public static function getListByUuid($uuid)
  41. {
  42. if (!$uuid || !is_array($uuid)) {
  43. return null;
  44. };
  45. $users = UserInfo::whereIn('userid', $uuid)->get();
  46. $assistants = AiModel::whereIn('uid', $uuid)->get();
  47. $output = array();
  48. foreach ($uuid as $key => $id) {
  49. foreach ($users as $user) {
  50. if ($user->userid === $id) {
  51. $output[] = UserApi::userInfo($user);
  52. continue;
  53. };
  54. }
  55. foreach ($assistants as $assistant) {
  56. if ($assistant->uid === $id) {
  57. $output[] = AiAssistantApi::userInfo($assistant);
  58. continue;
  59. };
  60. }
  61. }
  62. return $output;
  63. }
  64. public static function userInfo($user)
  65. {
  66. if (!$user) {
  67. return [
  68. 'id' => 0,
  69. 'nickName' => 'unknown',
  70. 'userName' => 'unknown',
  71. 'realName' => 'unknown',
  72. 'avatar' => '',
  73. ];
  74. }
  75. $data = [
  76. 'id' => $user->userid,
  77. 'nickName' => $user->nickname,
  78. 'userName' => $user->username,
  79. 'realName' => $user->username,
  80. 'sn' => $user->id,
  81. ];
  82. if (!empty($user->role)) {
  83. $data['roles'] = json_decode($user->role);
  84. }
  85. if ($user->avatar) {
  86. $img = str_replace('.jpg', '_s.jpg', $user->avatar);
  87. if (App::environment(['local', 'testing'])) {
  88. $data['avatar'] = Storage::url($img);
  89. } else {
  90. $data['avatar'] = Storage::temporaryUrl($img, now()->addDays(6));
  91. }
  92. }
  93. return $data;
  94. }
  95. }