| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Http\Api;
- use App\Http\Resources\AiAssistant;
- use App\Models\AiModel;
- 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();
- if (!$user) {
- return AiAssistantApi::getByUuid($id);
- }
- return UserApi::userInfo($user);
- }
- public static function getListByUuid($uuid)
- {
- if (!$uuid || !is_array($uuid)) {
- return null;
- };
- $users = UserInfo::whereIn('userid', $uuid)->get();
- $assistants = AiModel::whereIn('uid', $uuid)->get();
- $output = array();
- foreach ($uuid as $key => $id) {
- foreach ($users as $user) {
- if ($user->userid === $id) {
- $output[] = UserApi::userInfo($user);
- continue;
- };
- }
- foreach ($assistants as $assistant) {
- if ($assistant->uid === $id) {
- $output[] = AiAssistantApi::userInfo($assistant);
- 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;
- }
- }
|