import { request } from '../api/request' ; import { buildCachedRequest } from '../cache/utils' ; import { SYSTEM_USERID } from '../utils/consts' ; export interface UserBaseInfo { _id : string ; email : string ; nickname : string ; discriminator : string ; avatar : string | null ; temporary : boolean ; } export interface UserLoginInfo extends UserBaseInfo { token : string ; createdAt : string ; } const builtinUserInfo = { [ SYSTEM_USERID ] : { _id : SYSTEM_USERID , email : 'admin@msgbyte.com' , nickname : '系统' , discriminator : '0000' , avatar : null , temporary : false } } ; /**
 * 用户私信列表
 */ export interface UserDMList { userId : string ; converseIds : string [ ] ; } param email param password export async function loginWithEmail ( email , password ) { const { data } = await request . post ( '/api/user/login' , { email , password } ) ; return data ; } param token export async function loginWithToken ( token ) { const { data } = await request . post ( '/api/user/resolveToken' , { token } ) ; return data ; } param email param password export async function registerWithEmail ( email , password ) { const { data } = await request . post ( '/api/user/register' , { email , password } ) ; return data ; } param nickname export async function createTemporaryUser ( nickname ) { const { data } = await request . post ( '/api/user/createTemporaryUser' , { nickname } ) ; return data ; } /**
 * 认领访客账号
 */ export async function claimTemporaryUser ( userId , email , password ) { const { data } = await request . post ( '/api/user/claimTemporaryUser' , { userId , email , password } ) ; return data ; } param uniqueName export async function searchUserWithUniqueName ( uniqueName ) { const { data } = await request . post ( '/api/user/searchUserWithUniqueName' , { uniqueName } ) ; return data ; } param userId export async function fetchUserInfo ( userId ) { if ( builtinUserInfo [ userId ] ) { return builtinUserInfo [ userId ] ; } const { data } = await request . get ( '/api/user/getUserInfo' , { params : { userId } } ) ; return data ; } /**
 * 获取用户在线状态
 */ export async function getUserOnlineStatus ( userIds ) { const { data } = await request . get < boolean [ ] > ( '/api/gateway/checkUserOnline' , { params : { userIds } } ) ; return data ; } /**
 * 将会话添加到用户私信列表
 * 如果已添加则后端忽略
 */ export async function appendUserDMConverse ( converseId ) { const { data } = await request . post < UserDMList > ( '/api/user/dmlist/addConverse' , { converseId } ) ; return data ; } param fieldName param fieldValue type AllowedModifyField = 'nickname' | 'avatar' ; export async function modifyUserField ( fieldName , fieldValue ) { const { data } = await request . post ( '/api/user/updateUserField' , { fieldName , fieldValue } ) ; return data ; } /**
 * 检查Token是否可用
 */ export const checkTokenValid = buildCachedRequest ( 'tokenValid' , async ( token ) : Promise < boolean > => { const { data } = await request . post < boolean > ( '/api/user/checkTokenValid' , { token } ) ; return data ; } ) ;