UserApi.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Http\Api;
  3. use App\Models\UserInfo;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Support\Facades\Storage;
  6. use Illuminate\Support\Facades\App;
  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. return UserApi::userInfo($user);
  35. }
  36. public static function getListByUuid($uuid)
  37. {
  38. if (!$uuid || !is_array($uuid)) {
  39. return null;
  40. };
  41. $users = UserInfo::whereIn('userid', $uuid)->get();
  42. $output = array();
  43. foreach ($uuid as $key => $id) {
  44. foreach ($users as $user) {
  45. if ($user->userid === $id) {
  46. $output[] = UserApi::userInfo($user);
  47. continue;
  48. };
  49. }
  50. }
  51. return $output;
  52. }
  53. public static function userInfo($user)
  54. {
  55. if (!$user) {
  56. Log::error('$user=null;');
  57. return [
  58. 'id' => 0,
  59. 'nickName' => 'unknown',
  60. 'userName' => 'unknown',
  61. 'realName' => 'unknown',
  62. 'avatar' => '',
  63. ];
  64. }
  65. $data = [
  66. 'id' => $user->userid,
  67. 'nickName' => $user->nickname,
  68. 'userName' => $user->username,
  69. 'realName' => $user->username,
  70. 'sn' => $user->id,
  71. ];
  72. if (!empty($user->role)) {
  73. $data['roles'] = json_decode($user->role);
  74. }
  75. if ($user->avatar) {
  76. $img = str_replace('.jpg', '_s.jpg', $user->avatar);
  77. if (App::environment('local')) {
  78. $data['avatar'] = Storage::url($img);
  79. } else {
  80. $data['avatar'] = Storage::temporaryUrl($img, now()->addDays(6));
  81. }
  82. }
  83. return $data;
  84. }
  85. }