| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Http\Api;
- use App\Models\UserInfo;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Facades\App;
- class UserApi
- {
- public static function getIdByName($name)
- {
- return UserInfo::where('username', $name)->value('userid');
- }
- public static function getIdByUuid($uuid)
- {
- return UserInfo::where('userid', $uuid)->value('id');
- }
- public static function getIntIdByName($name)
- {
- return UserInfo::where('username', $name)->value('id');
- }
- public static function getById($id)
- {
- $user = UserInfo::where('id', $id)->first();
- return UserApi::userInfo($user);
- }
- public static function getByName($name)
- {
- $user = UserInfo::where('username', $name)->first();
- return UserApi::userInfo($user);
- }
- public static function getByUuid($id)
- {
- $user = UserInfo::where('userid', $id)->first();
- return UserApi::userInfo($user);
- }
- public static function getListByUuid($uuid)
- {
- if (!$uuid || !is_array($uuid)) {
- return null;
- };
- $users = UserInfo::whereIn('userid', $uuid)->get();
- $output = array();
- foreach ($uuid as $key => $id) {
- foreach ($users as $user) {
- if ($user->userid === $id) {
- $output[] = UserApi::userInfo($user);
- continue;
- };
- }
- }
- return $output;
- }
- public static function userInfo($user)
- {
- if (!$user) {
- Log::error('$user=null;');
- return [
- 'id' => 0,
- 'nickName' => 'unknown',
- 'userName' => 'unknown',
- 'realName' => 'unknown',
- 'avatar' => '',
- ];
- }
- $data = [
- 'id' => $user->userid,
- 'nickName' => $user->nickname,
- 'userName' => $user->username,
- 'realName' => $user->username,
- 'sn' => $user->id,
- ];
- if (!empty($user->role)) {
- $data['roles'] = json_decode($user->role);
- }
- if ($user->avatar) {
- $img = str_replace('.jpg', '_s.jpg', $user->avatar);
- if (App::environment('local')) {
- $data['avatar'] = Storage::url($img);
- } else {
- $data['avatar'] = Storage::temporaryUrl($img, now()->addDays(6));
- }
- }
- return $data;
- }
- }
|