feat: 接口统一优化

This commit is contained in:
2026-05-23 13:45:58 +08:00
parent a3b1fba3ea
commit 7d68db723b
20 changed files with 302 additions and 302 deletions
+17
View File
@@ -0,0 +1,17 @@
// app/utils/fetch.ts - $fetch 请求工具
/** 从 `$fetch` 错误里提取安全文案;服务端已经保证 message/msg 不含敏感上游细节 */
export const getClientErrorMessage = (error: unknown, fallback: string) => {
if (typeof error !== "object" || error === null) return fallback;
const data = "data" in error ? (error as { data?: unknown }).data : undefined;
if (typeof data === "object" && data !== null) {
const message = (data as { message?: unknown; msg?: unknown }).message;
const msg = (data as { message?: unknown; msg?: unknown }).msg;
if (typeof message === "string" && message) return message;
if (typeof msg === "string" && msg) return msg;
}
return fallback;
};
+17 -6
View File
@@ -6,19 +6,18 @@
* 输入:`A.\n错\nB.\n对`
* 输出:`A. 错\nB. 对`
*/
export function formatOptions(options: string): string {
export const formatOptions = (options: string): string =>
// 匹配类似 "A." 开头的选项标记,将其后紧跟的换行内容合并到同一行
return options.replace(/^([A-Za-z]\.)\s*\n\s*/gm, "$1 ");
}
options.replace(/^([A-Za-z]\.)\s*\n\s*/gm, "$1 ");
/**
* 将选项字符串拆分为逐行结构,标记哪些行是正确答案。
* answer 按 # 分隔(多选),与选项正文做等值匹配(大小写不敏感)。
*/
export function getOptionLines(
export const getOptionLines = (
options: string,
answer: string
): { text: string; isCorrect: boolean }[] {
): { text: string; isCorrect: boolean }[] => {
const segments = answer
.split("#")
.map((s) => s.trim().toUpperCase())
@@ -33,4 +32,16 @@ export function getOptionLines(
const isCorrect = content !== "" && segments.includes(content);
return { text: line, isCorrect };
});
}
};
/** 把运行秒数格式化为 dashboard 更容易扫读的时长 */
export const formatUptime = (seconds: number) => {
const safeSeconds = Math.max(0, Math.floor(seconds));
const days = Math.floor(safeSeconds / 86_400);
const hours = Math.floor((safeSeconds % 86_400) / 3_600);
const minutes = Math.floor((safeSeconds % 3_600) / 60);
if (days > 0) return `${days}${hours} 小时`;
if (hours > 0) return `${hours} 小时 ${minutes} 分钟`;
return `${Math.max(1, minutes)} 分钟`;
};
+1
View File
@@ -1,3 +1,4 @@
export * from "./clipboard";
export * from "./fetch";
export * from "./format";
export * from "./sortableTable";