refactor API export

This commit is contained in:
Timi
2026-08-21 16:34:39 +08:00
parent 175838f7c5
commit 98ab992728
11 changed files with 335 additions and 399 deletions
+6 -9
View File
@@ -1,5 +1,10 @@
<template> <template>
<div class="root"> <div class="root">
<pass-time-label :timestamp="1786694772700" underline />
<br>
<pass-time-label :timestamp="1786694772700" only-date />
<br>
<pass-time-label :timestamp="1786694772700" only-time />
<copyright /> <copyright />
</div> </div>
</template> </template>
@@ -7,17 +12,9 @@
<script lang="ts" setup> <script lang="ts" setup>
import {axios, Copyright, SettingMapper} from "timi-web"; import {axios, Copyright, SettingMapper} from "timi-web";
import CommonAPI from "../src/api/CommonAPI"; import CommonAPI from "../src/api/CommonAPI";
import PassTimeLabel from "../src/components/passtime-label/index.vue";
onMounted(async () => { onMounted(async () => {
const result = await CommonAPI.settingMap({
"SYSTEM": [
"SETTING_TTL"
]
});
SettingMapper.appendSettingMap(result);
console.log(SettingMapper.getValue("SYSTEM", "SETTING_TTL").value);
}) })
</script> </script>
+6 -8
View File
@@ -1,27 +1,25 @@
import type { Article } from "../types"; import type { Article } from "../types";
import { axios } from "./BaseAPI"; import { axios } from "./BaseAPI";
export const ArticleAPI = {
/** /**
* 获取文章 * 获取文章
* *
* @param id 文章 ID * @param id 文章 ID
* @returns 文章数据 * @returns 文章数据
*/ */
async function view(id: string): Promise<Article> { async view(id: string): Promise<Article> {
return axios.get(`/article/${id}`); return axios.get(`/article/${id}`);
} },
/** /**
* 喜欢文章,后端有限调用,1240ms 一次 * 喜欢文章,后端有限调用,1240ms 一次
* *
* @param id 文章 ID * @param id 文章 ID
* @returns 最新喜欢数量 * @returns 最新喜欢数量
*/ */
async function like(id: string): Promise<number> { async like(id: string): Promise<number> {
return axios.get(`/article/like/${id}`); return axios.get(`/article/like/${id}`);
} }
export default {
view,
like
}; };
export default ArticleAPI;
+16 -14
View File
@@ -4,6 +4,7 @@ import CommonAPI from "./CommonAPI";
const BASE_URI = "/attach"; const BASE_URI = "/attach";
export const AttachmentAPI = {
/** /**
* 上传临时附件 * 上传临时附件
* *
@@ -11,7 +12,7 @@ const BASE_URI = "/attach";
* @param ttl 有效期 * @param ttl 有效期
* @returns 临时附件列表 * @returns 临时附件列表
*/ */
async function upload(file: File | File[], ttl?: string): Promise<TempFile[]> { async upload(file: File | File[], ttl?: string): Promise<TempFile[]> {
const formData = new FormData(); const formData = new FormData();
const fileList = Array.isArray(file) ? file : [file]; const fileList = Array.isArray(file) ? file : [file];
for (const item of fileList) { for (const item of fileList) {
@@ -21,17 +22,15 @@ async function upload(file: File | File[], ttl?: string): Promise<TempFile[]> {
formData.append("ttl", ttl); formData.append("ttl", ttl);
} }
return await axios.post(`${BASE_URI}/temp/upload`, formData); return await axios.post(`${BASE_URI}/temp/upload`, formData);
} },
/** /**
* 按业务差分更新附件 * 按业务差分更新附件
* *
* @param req 更新请求 * @param req 更新请求
*/ */
async function updateByBizId(req: BizIdUpdate): Promise<void> { async updateByBizId(req: BizIdUpdate): Promise<void> {
return await axios.post(`${BASE_URI}/update/biz/id`, req); return await axios.post(`${BASE_URI}/update/biz/id`, req);
} },
/** /**
* 按业务查询附件列表 * 按业务查询附件列表
* *
@@ -39,7 +38,7 @@ async function updateByBizId(req: BizIdUpdate): Promise<void> {
* @param bizId 业务 ID * @param bizId 业务 ID
* @param attachTypeList 附件类型列表 * @param attachTypeList 附件类型列表
*/ */
async function listByBizId(bizType: string, bizId: string, attachTypeList?: string[]): Promise<Attachment[]> { async listByBizId(bizType: string, bizId: string, attachTypeList?: string[]): Promise<Attachment[]> {
return await axios.get(`${BASE_URI}/list/biz/id`, { return await axios.get(`${BASE_URI}/list/biz/id`, {
params: { params: {
bizType, bizType,
@@ -47,12 +46,15 @@ async function listByBizId(bizType: string, bizId: string, attachTypeList?: stri
attachTypeList attachTypeList
} }
}); });
} },
export default { getReadURL(id: string): string {
upload, return CommonAPI.getAttachmentReadAPI(id);
updateByBizId, },
listByBizId,
getReadURL: CommonAPI.getAttachmentReadAPI, getTempReadURL(id: string): string {
getTempReadURL: CommonAPI.getAttachmentTempReadAPI return CommonAPI.getAttachmentTempReadAPI(id);
}
}; };
export default AttachmentAPI;
+6 -8
View File
@@ -3,15 +3,13 @@ import { axios } from "./BaseAPI";
const BASE_URI = "/comment"; const BASE_URI = "/comment";
async function page(page: Page<Comment>): Promise<PageResult<Comment>> { export const CommentAPI = {
async page(page: Page<Comment>): Promise<PageResult<Comment>> {
return axios.post(`${BASE_URI}/list`, page); return axios.post(`${BASE_URI}/list`, page);
} },
async create(captchaData: CaptchaData<Comment>): Promise<void> {
async function create(captchaData: CaptchaData<Comment>): Promise<void> {
return axios.post(`${BASE_URI}/create`, captchaData); return axios.post(`${BASE_URI}/create`, captchaData);
} }
export default {
page,
create,
}; };
export default CommentAPI;
+15 -17
View File
@@ -9,17 +9,20 @@ function getBaseURI(): string {
return baseURL.replace(/\/$/, ""); return baseURL.replace(/\/$/, "");
} }
const getCaptchaAPI = () => `${getBaseURI()}/captcha`; export const CommonAPI = {
getCaptchaAPI(): string {
async function captcha(width: number, height: number): Promise<CaptchaResult> { return `${getBaseURI()}/captcha`;
},
async captcha(width: number, height: number): Promise<CaptchaResult> {
return await axios.get(`/captcha?width=${width}&height=${height}`); return await axios.get(`/captcha?width=${width}&height=${height}`);
} },
getAttachmentReadAPI(id: string): string {
const getAttachmentReadAPI = (id: string) => `${getBaseURI()}/attach/read/${id}`; return `${getBaseURI()}/attach/read/${id}`;
},
const getAttachmentTempReadAPI = (id: string) => `${getBaseURI()}/attach/temp/read?id=${id}`; getAttachmentTempReadAPI(id: string): string {
return `${getBaseURI()}/attach/temp/read?id=${id}`;
async function settingMap(map: Record<string, string[]>): Promise<Map<string, Map<string, Setting>>> { },
async settingMap(map: Record<string, string[]>): Promise<Map<string, Map<string, Setting>>> {
const raw = await axios.post("/setting/map", map); const raw = await axios.post("/setting/map", map);
const moduleMap = new Map<string, Map<string, Setting>>(); const moduleMap = new Map<string, Map<string, Setting>>();
if (!raw || typeof raw !== "object") { if (!raw || typeof raw !== "object") {
@@ -37,11 +40,6 @@ async function settingMap(map: Record<string, string[]>): Promise<Map<string, Ma
} }
return moduleMap; return moduleMap;
} }
export default {
getCaptchaAPI,
captcha,
getAttachmentReadAPI,
getAttachmentTempReadAPI,
settingMap
}; };
export default CommonAPI;
+4 -4
View File
@@ -3,10 +3,10 @@ import { axios } from "./BaseAPI";
const BASE_URI = "/git/developer"; const BASE_URI = "/git/developer";
async function get(): Promise<Developer> { export const DeveloperAPI = {
async get(): Promise<Developer> {
return axios.post(`${BASE_URI}`); return axios.post(`${BASE_URI}`);
} }
export default {
get
}; };
export default DeveloperAPI;
+20 -20
View File
@@ -1,51 +1,51 @@
import { axios } from "./BaseAPI"; import { axios } from "./BaseAPI";
import type { NotifyDetail, Page, PageResult } from "../types"; import type { CaptchaData, Notify, NotifyDetail, Page, PageResult } from "../types";
const BASE_URI = "/notify/internal"; const BASE_URI = "/notify/internal";
export const NotifyAPI = {
async smsSend(captchaData: CaptchaData<Notify>) {
return await axios.post("/sms/captcha/create", captchaData);
},
/** 查询当前用户的站内通知详情。 */ /** 查询当前用户的站内通知详情。 */
async function internalDetail(id: string): Promise<NotifyDetail> { async internalDetail(id: string): Promise<NotifyDetail> {
return await axios.post(`${BASE_URI}/detail`, undefined, { return await axios.post(`${BASE_URI}/detail`, undefined, {
params: { id } params: { id }
}); });
} },
/** 查询当前用户的站内通知。 */ /** 查询当前用户的站内通知。 */
async function listInternal(page?: Page<NotifyDetail>, unreadOnly = false): Promise<PageResult<NotifyDetail>> { async listInternal(page?: Page<NotifyDetail>, unreadOnly = false): Promise<PageResult<NotifyDetail>> {
return await axios.post(`${BASE_URI}/internal`, page, { return await axios.post(`${BASE_URI}/internal`, page, {
params: { unreadOnly } params: { unreadOnly }
}); });
} },
/** 查询当前用户未读站内通知数量。 */ /** 查询当前用户未读站内通知数量。 */
async function countUnreadInternal(): Promise<number> { async countUnreadInternal(): Promise<number> {
return await axios.post(`${BASE_URI}/unread/count`); return await axios.post(`${BASE_URI}/unread/count`);
} },
/** 标记当前用户的一条站内通知已读。 */ /** 标记当前用户的一条站内通知已读。 */
async function markInternalRead(id: string): Promise<void> { async markInternalRead(id: string): Promise<void> {
await axios.post(`${BASE_URI}/read`, undefined, { await axios.post(`${BASE_URI}/read`, undefined, {
params: { id } params: { id }
}); });
} },
/** 删除当前用户的一条站内通知。 */ /** 删除当前用户的一条站内通知。 */
async function deleteInternal(id: string): Promise<void> { async deleteInternal(id: string): Promise<void> {
await axios.post(`${BASE_URI}/delete`, undefined, { await axios.post(`${BASE_URI}/delete`, undefined, {
params: { id } params: { id }
}); });
} },
/** 标记当前用户全部站内通知已读。 */ /** 标记当前用户全部站内通知已读。 */
async function markAllInternalRead(): Promise<void> { async markAllInternalRead(): Promise<void> {
await axios.post(`${BASE_URI}/read/all`); await axios.post(`${BASE_URI}/read/all`);
} }
export default {
internalDetail,
listInternal,
countUnreadInternal,
markInternalRead,
deleteInternal,
markAllInternalRead
}; };
export default NotifyAPI;
+12 -20
View File
@@ -3,35 +3,27 @@ import type { ModuleCode, Page, PageResult, Permission, PermissionPayload } from
const BASE_URI = "/user/permission"; const BASE_URI = "/user/permission";
async function list(req: Page<Permission>): Promise<PageResult<Permission>> { export const PermissionAPI = {
async list(req: Page<Permission>): Promise<PageResult<Permission>> {
return axios.post(`${BASE_URI}/list`, req); return axios.post(`${BASE_URI}/list`, req);
} },
async create(req: PermissionPayload): Promise<void> {
async function create(req: PermissionPayload): Promise<void> {
return axios.post(`${BASE_URI}/create`, req); return axios.post(`${BASE_URI}/create`, req);
} },
async update(req: PermissionPayload): Promise<void> {
async function update(req: PermissionPayload): Promise<void> {
return axios.post(`${BASE_URI}/update`, req); return axios.post(`${BASE_URI}/update`, req);
} },
async remove(id: string): Promise<void> {
async function remove(id: string): Promise<void> {
return axios.post(`${BASE_URI}/delete`, undefined, { return axios.post(`${BASE_URI}/delete`, undefined, {
params: { id } params: { id }
}); });
} },
/** 查询当前登录用户在指定模块内可转授的权限。 */ /** 查询当前登录用户在指定模块内可转授的权限。 */
async function grantableList(moduleCode: ModuleCode): Promise<Permission[]> { async grantableList(moduleCode: ModuleCode): Promise<Permission[]> {
return await axios.post(`${BASE_URI}/grantable/list`, undefined, { return await axios.post(`${BASE_URI}/grantable/list`, undefined, {
params: { moduleCode } params: { moduleCode }
}); });
} }
export default {
list,
create,
update,
remove,
grantableList
}; };
export default PermissionAPI;
+36 -68
View File
@@ -3,124 +3,92 @@ import type { ModuleCode, Page, PageResult, Permission, Role, RolePayload, Autho
const BASE_URI = "/user/role"; const BASE_URI = "/user/role";
async function list(req: Page<Role>): Promise<PageResult<Role>> { export const RoleAPI = {
async list(req: Page<Role>): Promise<PageResult<Role>> {
return axios.post(`${BASE_URI}/list`, req); return axios.post(`${BASE_URI}/list`, req);
} },
async detail(id: string): Promise<Role> {
async function detail(id: string): Promise<Role> {
return axios.post(`${BASE_URI}/detail`, undefined, { return axios.post(`${BASE_URI}/detail`, undefined, {
params: { id } params: { id }
}); });
} },
async create(req: RolePayload): Promise<void> {
async function create(req: RolePayload): Promise<void> {
return axios.post(`${BASE_URI}/create`, req); return axios.post(`${BASE_URI}/create`, req);
} },
async update(req: RolePayload): Promise<void> {
async function update(req: RolePayload): Promise<void> {
return axios.post(`${BASE_URI}/update`, req); return axios.post(`${BASE_URI}/update`, req);
} },
async remove(id: string): Promise<void> {
async function remove(id: string): Promise<void> {
return axios.post(`${BASE_URI}/delete`, undefined, { return axios.post(`${BASE_URI}/delete`, undefined, {
params: { id } params: { id }
}); });
} },
/** 查询当前登录用户在模块内可管理的角色。 */ /** 查询当前登录用户在模块内可管理的角色。 */
async function manageableList(moduleCode: ModuleCode): Promise<Role[]> { async manageableList(moduleCode: ModuleCode): Promise<Role[]> {
return await axios.post(`${BASE_URI}/manageable/list`, undefined, { return await axios.post(`${BASE_URI}/manageable/list`, undefined, {
params: { moduleCode } params: { moduleCode }
}); });
} },
/** 查询当前登录用户在模块内可授权给账号的角色。 */ /** 查询当前登录用户在模块内可授权给账号的角色。 */
async function grantableList(moduleCode: ModuleCode): Promise<Role[]> { async grantableList(moduleCode: ModuleCode): Promise<Role[]> {
return await axios.post(`${BASE_URI}/grantable/list`, undefined, { return await axios.post(`${BASE_URI}/grantable/list`, undefined, {
params: { moduleCode } params: { moduleCode }
}); });
} },
/** 查询当前登录用户直属角色详情,不按可管理范围过滤。 */ /** 查询当前登录用户直属角色详情,不按可管理范围过滤。 */
async function currentDetailList(moduleCode?: ModuleCode): Promise<Role[]> { async currentDetailList(moduleCode?: ModuleCode): Promise<Role[]> {
return await axios.post(`${BASE_URI}/current/detail/list`, undefined, { return await axios.post(`${BASE_URI}/current/detail/list`, undefined, {
params: { moduleCode } params: { moduleCode }
}); });
} },
/** 查询当前登录用户可以给目标角色配置的直接权限 P 候选项。 */ /** 查询当前登录用户可以给目标角色配置的直接权限 P 候选项。 */
async function grantablePermissionList(roleId: string): Promise<Permission[]> { async grantablePermissionList(roleId: string): Promise<Permission[]> {
return await axios.post(`${BASE_URI}/permission/grantable/list`, undefined, { return await axios.post(`${BASE_URI}/permission/grantable/list`, undefined, {
params: { roleId } params: { roleId }
}); });
} },
/** 查询目标角色当前可继续转授的权限 D。 */ /** 查询目标角色当前可继续转授的权限 D。 */
async function delegationList(roleId: string): Promise<Permission[]> { async delegationList(roleId: string): Promise<Permission[]> {
return await axios.post(`${BASE_URI}/delegation/list`, undefined, { return await axios.post(`${BASE_URI}/delegation/list`, undefined, {
params: { roleId } params: { roleId }
}); });
} },
/** 查询当前登录用户可以给目标角色配置的 D 候选项。 */ /** 查询当前登录用户可以给目标角色配置的 D 候选项。 */
async function grantableDelegationList(roleId: string): Promise<Permission[]> { async grantableDelegationList(roleId: string): Promise<Permission[]> {
return await axios.post(`${BASE_URI}/delegation/grantable/list`, undefined, { return await axios.post(`${BASE_URI}/delegation/grantable/list`, undefined, {
params: { roleId } params: { roleId }
}); });
} },
/** 覆盖保存目标角色可继续转授的权限 D。 */ /** 覆盖保存目标角色可继续转授的权限 D。 */
async function updateDelegation(req: Pick<RolePayload, "id" | "delegationPermissionIdList">): Promise<void> { async updateDelegation(req: Pick<RolePayload, "id" | "delegationPermissionIdList">): Promise<void> {
await axios.post(`${BASE_URI}/delegation/update`, req); await axios.post(`${BASE_URI}/delegation/update`, req);
} },
/** 查询目标角色可选管理父级。 */ /** 查询目标角色可选管理父级。 */
async function parentGrantableList(roleId: string): Promise<Role[]> { async parentGrantableList(roleId: string): Promise<Role[]> {
return await axios.post(`${BASE_URI}/parent/grantable/list`, undefined, { return await axios.post(`${BASE_URI}/parent/grantable/list`, undefined, {
params: { roleId } params: { roleId }
}); });
} },
/** 保存目标角色管理父级。 */ /** 保存目标角色管理父级。 */
async function updateParent(req: Pick<RolePayload, "id" | "parentRoleId">): Promise<void> { async updateParent(req: Pick<RolePayload, "id" | "parentRoleId">): Promise<void> {
await axios.post(`${BASE_URI}/parent/update`, req); await axios.post(`${BASE_URI}/parent/update`, req);
} },
async authorizedList(userId: string, moduleCode?: ModuleCode): Promise<Role[]> {
async function authorizedList(userId: string, moduleCode?: ModuleCode): Promise<Role[]> {
return axios.post(`${BASE_URI}/authorized/list`, undefined, { return axios.post(`${BASE_URI}/authorized/list`, undefined, {
params: { params: {
userId, userId,
moduleCode moduleCode
} }
}); });
} },
async authorize(req: AuthorizeUserRole): Promise<void> {
async function authorize(req: AuthorizeUserRole): Promise<void> {
return axios.post(`${BASE_URI}/authorized/create`, req); return axios.post(`${BASE_URI}/authorized/create`, req);
} },
async authorizedPermission(userId: string): Promise<Permission[]> {
async function authorizedPermission(userId: string): Promise<Permission[]> {
return axios.post(`${BASE_URI}/authorized/permission`, undefined, { return axios.post(`${BASE_URI}/authorized/permission`, undefined, {
params: { userId } params: { userId }
}); });
} }
export default {
list,
detail,
create,
update,
remove,
manageableList,
grantableList,
currentDetailList,
grantablePermissionList,
delegationList,
grantableDelegationList,
updateDelegation,
parentGrantableList,
updateParent,
authorizedList,
authorize,
authorizedPermission
}; };
export default RoleAPI;
+51 -68
View File
@@ -10,63 +10,6 @@ type UpdateCurrentRequest = Omit<Partial<User>, "gender"> & {
gender?: Gender | ""; gender?: Gender | "";
}; };
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: UpdateCurrentRequest): 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?: UserAttachmentOwner) {
if (user?.attachmentList) {
return findAttachmentByType(user.attachmentList, "AVATAR");
}
}
function getWrapperURL(user?: UserAttachmentOwner) {
if (user?.attachmentList) {
return findAttachmentByType(user.attachmentList, "WRAPPER");
}
}
function findAttachmentByType(attachmentList: readonly Attachment[], ...types: UserAttachType[]) { function findAttachmentByType(attachmentList: readonly Attachment[], ...types: UserAttachType[]) {
for (let i = 0; i < attachmentList.length; i++) { for (let i = 0; i < attachmentList.length; i++) {
const attachType = attachmentList[i].attachType as UserAttachType | undefined; const attachType = attachmentList[i].attachType as UserAttachType | undefined;
@@ -77,15 +20,55 @@ function findAttachmentByType(attachmentList: readonly Attachment[], ...types: U
} }
} }
export default { export const UserAPI = {
register, async register(req: CaptchaData<RegisterRequest>): Promise<LoginResponse> {
login, return await axios.post(`${BASE_URI}/register`, req);
loginByToken, },
logout, /**
updateCurrent, * 登录
updatePassword, *
* @param req 验证码登录对象
view, * @returns LoginResponse
getAvatarURL, */
getWrapperURL async login(req: CaptchaData<LoginRequest>): Promise<LoginResponse> {
return await axios.post(`${BASE_URI}/login`, req);
},
/**
* 验证是否已登录
*
* @returns true 为已登录
*/
async loginByToken(): Promise<LoginResponse> {
return await axios.post(`${BASE_URI}/login/token`);
},
async logout(): Promise<void> {
return await axios.post(`${BASE_URI}/logout`);
},
async updateCurrent(req: UpdateCurrentRequest): Promise<LoginResponse> {
return await axios.post(`${BASE_URI}/current/update`, req);
},
async updatePassword(req: UpdatePasswordRequest): Promise<void> {
return await axios.post(`${BASE_URI}/update/password`, req);
},
/**
* 获取用户数据
*
* @param id 用户 ID
* @returns 用户数据
*/
async view(id: string): Promise<User> {
return await axios.post(`${BASE_URI}/view/${id}`);
},
getAvatarURL(user?: UserAttachmentOwner) {
if (user?.attachmentList) {
return findAttachmentByType(user.attachmentList, "AVATAR");
}
},
getWrapperURL(user?: UserAttachmentOwner) {
if (user?.attachmentList) {
return findAttachmentByType(user.attachmentList, "WRAPPER");
}
}
}; };
export default UserAPI;
+9 -9
View File
@@ -1,9 +1,9 @@
export { default as ArticleAPI } from "./ArticleAPI"; export { ArticleAPI } from "./ArticleAPI";
export { default as AttachmentAPI } from "./AttachmentAPI"; export { AttachmentAPI } from "./AttachmentAPI";
export { default as CommentAPI } from "./CommentAPI"; export { CommentAPI } from "./CommentAPI";
export { default as CommonAPI } from "./CommonAPI"; export { CommonAPI } from "./CommonAPI";
export { default as DeveloperAPI } from "./DeveloperAPI"; export { DeveloperAPI } from "./DeveloperAPI";
export { default as NotifyAPI } from "./NotifyAPI"; export { NotifyAPI } from "./NotifyAPI";
export { default as PermissionAPI } from "./PermissionAPI"; export { PermissionAPI } from "./PermissionAPI";
export { default as RoleAPI } from "./RoleAPI"; export { RoleAPI } from "./RoleAPI";
export { default as UserAPI } from "./UserAPI"; export { UserAPI } from "./UserAPI";