UserApi.php 2.9 KB

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