update
This commit is contained in:
+33
-33
@@ -1,30 +1,21 @@
|
||||
import {
|
||||
Attachment,
|
||||
CaptchaData,
|
||||
LoginRequest,
|
||||
LoginResponse,
|
||||
RegisterRequest,
|
||||
UserAttachType,
|
||||
UserProfileView,
|
||||
UserView
|
||||
} from "../types";
|
||||
import { axios } from "./BaseAPI";
|
||||
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(captchaData: CaptchaData<RegisterRequest>): Promise<LoginResponse> {
|
||||
return axios.post(`${BASE_URI}/register`, captchaData);
|
||||
async function register(req: CaptchaData<RegisterRequest>): Promise<LoginResponse> {
|
||||
return await axios.post(`${BASE_URI}/register`, req);
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录
|
||||
*
|
||||
* @param captchaData 验证码登录对象
|
||||
* @param req 验证码登录对象
|
||||
* @returns LoginResponse
|
||||
*/
|
||||
async function login(captchaData: CaptchaData<LoginRequest>): Promise<LoginResponse> {
|
||||
return axios.post(`${BASE_URI}/login`, captchaData);
|
||||
async function login(req: CaptchaData<LoginRequest>): Promise<LoginResponse> {
|
||||
return await axios.post(`${BASE_URI}/login`, req);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,12 +23,20 @@ async function login(captchaData: CaptchaData<LoginRequest>): Promise<LoginRespo
|
||||
*
|
||||
* @returns true 为已登录
|
||||
*/
|
||||
async function login4Token(): Promise<LoginResponse> {
|
||||
return axios.post(`${BASE_URI}/login/token`);
|
||||
async function loginByToken(): Promise<LoginResponse> {
|
||||
return await axios.post(`${BASE_URI}/login/token`);
|
||||
}
|
||||
|
||||
async function logout(): Promise<void> {
|
||||
return axios.post(`${BASE_URI}/logout`);
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,29 +45,28 @@ async function logout(): Promise<void> {
|
||||
* @param id 用户 ID
|
||||
* @returns 用户数据
|
||||
*/
|
||||
async function view(id: number): Promise<UserView> {
|
||||
return axios.post(`${BASE_URI}/view/${id}`);
|
||||
async function view(id: string): Promise<User> {
|
||||
return await axios.post(`${BASE_URI}/view/${id}`);
|
||||
}
|
||||
|
||||
function getAvatarURL(profile?: UserProfileView) {
|
||||
if (profile && profile.attachmentList) {
|
||||
return findAttachmentByType(profile.attachmentList, [UserAttachType.AVATAR, UserAttachType.DEFAULT_AVATAR]);
|
||||
function getAvatarURL(user?: User) {
|
||||
if (user?.attachmentList) {
|
||||
return findAttachmentByType(user.attachmentList, [UserAttachType.AVATAR, UserAttachType.DEFAULT_AVATAR]);
|
||||
}
|
||||
}
|
||||
|
||||
function getWrapperURL(profile?: UserProfileView) {
|
||||
if (profile && profile.attachmentList) {
|
||||
return findAttachmentByType(profile.attachmentList, [UserAttachType.WRAPPER, UserAttachType.DEFAULT_WRAPPER]);
|
||||
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 = (<any>UserAttachType)[attachmentList[i].attachType!];
|
||||
for (let type of types) {
|
||||
if (attachType === type) {
|
||||
return CommonAPI.getAttachmentReadAPI(attachmentList[i].mongoId);
|
||||
}
|
||||
const attachType = attachmentList[i].attachType as UserAttachType | undefined;
|
||||
const id = attachmentList[i].id;
|
||||
if (id && attachType && types.includes(attachType)) {
|
||||
return CommonAPI.getAttachmentReadAPI(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,8 +74,10 @@ function findAttachmentByType(attachmentList: Attachment[], types: UserAttachTyp
|
||||
export default {
|
||||
register,
|
||||
login,
|
||||
login4Token,
|
||||
loginByToken,
|
||||
logout,
|
||||
updateCurrent,
|
||||
updatePassword,
|
||||
|
||||
view,
|
||||
getAvatarURL,
|
||||
|
||||
Reference in New Issue
Block a user