UserApi.php 2.8 KB

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