This commit is contained in:
Timi
2026-07-30 18:28:25 +08:00
parent 8b24804b22
commit f0d1090932
21 changed files with 915 additions and 268 deletions
+2 -2
View File
@@ -7,7 +7,7 @@ import { axios } from "./BaseAPI";
* @param id 文章 ID
* @returns 文章数据
*/
async function view(id: number): Promise<ArticleView<any>> {
async function view(id: string): Promise<ArticleView<any>> {
return axios.get(`/article/${id}`);
}
@@ -17,7 +17,7 @@ async function view(id: number): Promise<ArticleView<any>> {
* @param id 文章 ID
* @returns 最新喜欢数量
*/
async function like(id: number): Promise<number> {
async function like(id: string): Promise<number> {
return axios.get(`/article/like/${id}`);
}
+58
View File
@@ -0,0 +1,58 @@
import {Attachment, BizUpdateReq, TempFileResp} from "../types";
import {axios} from "./BaseAPI";
import CommonAPI from "./CommonAPI";
const BASE_URI = "/attach";
/**
* 上传临时附件
*
* @param file 文件或文件列表
* @param ttl 有效期
* @returns 临时附件列表
*/
async function uploadTemp(file: File | File[], ttl?: string): Promise<TempFileResp[]> {
const formData = new FormData();
const fileList = Array.isArray(file) ? file : [file];
for (const item of fileList) {
formData.append("file", item);
}
if (ttl) {
formData.append("ttl", ttl);
}
return await axios.post(`${BASE_URI}/temp/upload`, formData);
}
/**
* 按业务差分更新附件
*
* @param req 更新请求
*/
async function updateByBiz(req: BizUpdateReq): Promise<void> {
return await axios.post(`${BASE_URI}/update/biz`, req);
}
/**
* 按业务查询附件列表
*
* @param bizType 业务类型
* @param bizId 业务 ID
* @param attachTypeList 附件类型列表
*/
async function listByBiz(bizType: string, bizId: string, attachTypeList?: string[]): Promise<Attachment[]> {
return await axios.get(`${BASE_URI}/list/biz`, {
params: {
bizType,
bizId,
attachTypeList
}
});
}
export default {
uploadTemp,
updateByBiz,
listByBiz,
getReadURL: CommonAPI.getAttachmentReadAPI,
getTempReadURL: CommonAPI.getAttachmentTempReadAPI
};
+19 -3
View File
@@ -1,9 +1,23 @@
import {Setting, TemplateBizType} from "../types";
import {CaptchaResult, Setting, TemplateBizType} from "../types";
import {axios} from "./BaseAPI";
const getCaptchaAPI = () => axios.defaults.baseURL + "/captcha";
function getBaseURI(): string {
const baseURL = axios.defaults.baseURL;
if (!baseURL || baseURL === "undefined") {
return "";
}
return baseURL.replace(/\/$/, "");
}
const getAttachmentReadAPI = (mongoId: string) => `${axios.defaults.baseURL}/attachment/read/${mongoId}`;
const getCaptchaAPI = () => `${getBaseURI()}/captcha`;
async function captcha(width: number, height: number): Promise<CaptchaResult> {
return await axios.get(`/captcha?width=${width}&height=${height}`);
}
const getAttachmentReadAPI = (id: string) => `${getBaseURI()}/attach/read/${id}`;
const getAttachmentTempReadAPI = (id: string) => `${getBaseURI()}/attach/temp/read?id=${id}`;
async function getTemplate(bizType: TemplateBizType, code: string): Promise<string> {
return axios.get(`/template?bizType=${bizType}&bizCode=${code}`);
@@ -30,7 +44,9 @@ async function settingMap(map: Record<string, string[]>): Promise<Map<string, Ma
export default {
getCaptchaAPI,
captcha,
getAttachmentReadAPI,
getAttachmentTempReadAPI,
getTemplate,
settingMap
};
+29
View File
@@ -0,0 +1,29 @@
import { axios } from "./BaseAPI";
import type { Page, PageResult, Permission, PermissionPayload } from "../types";
const BASE_URI = "/user/permission";
async function list(req: Page<Permission>): Promise<PageResult<Permission>> {
return axios.post(`${BASE_URI}/list`, req);
}
async function create(req: PermissionPayload): Promise<void> {
return axios.post(`${BASE_URI}/create`, req);
}
async function update(req: PermissionPayload): Promise<void> {
return axios.post(`${BASE_URI}/update`, req);
}
async function remove(id: string): Promise<void> {
return axios.post(`${BASE_URI}/delete`, undefined, {
params: { id }
});
}
export default {
list,
create,
update,
remove
};
+55
View File
@@ -0,0 +1,55 @@
import { axios } from "./BaseAPI";
import type { Page, PageResult, Permission, Role, RolePayload, UserRoleAuthorizeReq } from "../types";
const BASE_URI = "/user/role";
async function list(req: Page<Role>): Promise<PageResult<Role>> {
return axios.post(`${BASE_URI}/list`, req);
}
async function detail(id: string): Promise<Role> {
return axios.post(`${BASE_URI}/detail`, undefined, {
params: { id }
});
}
async function create(req: RolePayload): Promise<void> {
return axios.post(`${BASE_URI}/create`, req);
}
async function update(req: RolePayload): Promise<void> {
return axios.post(`${BASE_URI}/update`, req);
}
async function remove(id: string): Promise<void> {
return axios.post(`${BASE_URI}/delete`, undefined, {
params: { id }
});
}
async function authorizedList(userId: string): Promise<Role[]> {
return axios.post(`${BASE_URI}/authorized/list`, undefined, {
params: { userId }
});
}
async function authorize(req: UserRoleAuthorizeReq): Promise<void> {
return axios.post(`${BASE_URI}/authorized/create`, req);
}
async function authorizedPermission(userId: string): Promise<Permission[]> {
return axios.post(`${BASE_URI}/authorized/permission`, undefined, {
params: { userId }
});
}
export default {
list,
detail,
create,
update,
remove,
authorizedList,
authorize,
authorizedPermission
};
+33 -33
View File
@@ -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,
+3
View File
@@ -1,5 +1,8 @@
export { default as ArticleAPI } from "./ArticleAPI";
export { default as AttachmentAPI } from "./AttachmentAPI";
export { default as CommentAPI } from "./CommentAPI";
export { default as CommonAPI } from "./CommonAPI";
export { default as DeveloperAPI } from "./DeveloperAPI";
export { default as PermissionAPI } from "./PermissionAPI";
export { default as RoleAPI } from "./RoleAPI";
export { default as UserAPI } from "./UserAPI";