Files
timi-web/src/api/UserAPI.ts
T
2026-07-30 18:28:25 +08:00

86 lines
2.1 KiB
TypeScript

import {Attachment, CaptchaData, LoginRequest, LoginResponse, RegisterRequest, UpdatePasswordRequest, User, UserAttachType} from "../types";
import {axios} from "./BaseAPI";
import CommonAPI from "./CommonAPI";
const BASE_URI = "/user";
async function register(req: CaptchaData<RegisterRequest>): Promise<LoginResponse> {
return await axios.post(`${BASE_URI}/register`, req);
}
/**
* 登录
*
* @param req 验证码登录对象
* @returns LoginResponse
*/
async function login(req: CaptchaData<LoginRequest>): Promise<LoginResponse> {
return await axios.post(`${BASE_URI}/login`, req);
}
/**
* 验证是否已登录
*
* @returns true 为已登录
*/
async function loginByToken(): Promise<LoginResponse> {
return await axios.post(`${BASE_URI}/login/token`);
}
async function logout(): Promise<void> {
return await axios.post(`${BASE_URI}/logout`);
}
async function updateCurrent(req: User): Promise<LoginResponse> {
return await axios.post(`${BASE_URI}/current/update`, req);
}
async function updatePassword(req: UpdatePasswordRequest): Promise<void> {
return await axios.post(`${BASE_URI}/update/password`, req);
}
/**
* 获取用户数据
*
* @param id 用户 ID
* @returns 用户数据
*/
async function view(id: string): Promise<User> {
return await axios.post(`${BASE_URI}/view/${id}`);
}
function getAvatarURL(user?: User) {
if (user?.attachmentList) {
return findAttachmentByType(user.attachmentList, [UserAttachType.AVATAR, UserAttachType.DEFAULT_AVATAR]);
}
}
function getWrapperURL(user?: User) {
if (user?.attachmentList) {
return findAttachmentByType(user.attachmentList, [UserAttachType.WRAPPER, UserAttachType.DEFAULT_WRAPPER]);
}
}
function findAttachmentByType(attachmentList: Attachment[], types: UserAttachType[]) {
for (let i = 0; i < attachmentList.length; i++) {
const attachType = attachmentList[i].attachType as UserAttachType | undefined;
const id = attachmentList[i].id;
if (id && attachType && types.includes(attachType)) {
return CommonAPI.getAttachmentReadAPI(id);
}
}
}
export default {
register,
login,
loginByToken,
logout,
updateCurrent,
updatePassword,
view,
getAvatarURL,
getWrapperURL
};