UserApi.php 2.8 KB

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