feat: 大量优化和bug修复
This commit is contained in:
+5
-55
@@ -3,14 +3,12 @@ import { defineStore } from "pinia";
|
||||
import { toast } from "vue-sonner";
|
||||
|
||||
import type {
|
||||
IHealthResponse,
|
||||
IQaRecord,
|
||||
ISearchSuccessResponse,
|
||||
IStatsResponse,
|
||||
QuestionType
|
||||
} from "@/interfaces";
|
||||
import { AnswerService } from "@/services";
|
||||
import { formatUptime, getClientErrorMessage } from "~/utils";
|
||||
import { getClientErrorMessage } from "~/utils";
|
||||
|
||||
/** Dashboard 最近记录表格每页数量,组件直接复用,避免多个地方写死 */
|
||||
export const DASHBOARD_RECORD_PAGE_SIZE = 10;
|
||||
@@ -56,16 +54,10 @@ export const useAnswerStore = defineStore("answer", () => {
|
||||
const searchErrorMessage = ref("");
|
||||
|
||||
// ---- Dashboard 状态 ----
|
||||
/** 健康检查信息 */
|
||||
const health = ref<IHealthResponse>();
|
||||
/** 运行统计信息 */
|
||||
const stats = ref<IStatsResponse>();
|
||||
/** 最近问答记录 */
|
||||
const records = ref<IQaRecord[]>([]);
|
||||
/** Dashboard 错误文案 */
|
||||
const dashboardErrorMessage = ref("");
|
||||
/** 统计信息加载状态 */
|
||||
const statsLoading = ref(false);
|
||||
/** 最近问答记录加载状态 */
|
||||
const recordsLoading = ref(false);
|
||||
/** 缓存清理状态 */
|
||||
@@ -82,11 +74,6 @@ export const useAnswerStore = defineStore("answer", () => {
|
||||
/** Dashboard 表格当前页数据,页码切换会重新请求服务端分页接口 */
|
||||
const pagedRecords = computed(() => records.value);
|
||||
|
||||
/** Dashboard 顶部运行时长展示 */
|
||||
const uptimeText = computed(() => {
|
||||
return stats.value ? formatUptime(stats.value.uptime) : "-";
|
||||
});
|
||||
|
||||
/** 选择表格记录,交给详情弹窗展示完整题目、选项和答案 */
|
||||
const selectRecord = (record?: IQaRecord) => {
|
||||
selectedRecord.value = record;
|
||||
@@ -129,7 +116,7 @@ export const useAnswerStore = defineStore("answer", () => {
|
||||
if (res.data.code === 1) {
|
||||
answerResult.value = res.data;
|
||||
recordsPage.value = 1;
|
||||
await Promise.allSettled([getStats(), getRecords()]);
|
||||
await getRecords();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -143,37 +130,6 @@ export const useAnswerStore = defineStore("answer", () => {
|
||||
}
|
||||
};
|
||||
|
||||
/** 读取健康检查,失败时只写本地错误文案 */
|
||||
const getHealth = async () => {
|
||||
try {
|
||||
const res = await AnswerService.getHealth();
|
||||
health.value = res.data.data;
|
||||
} catch (error) {
|
||||
dashboardErrorMessage.value = getClientErrorMessage(
|
||||
error,
|
||||
"健康检查失败"
|
||||
);
|
||||
toast.error(dashboardErrorMessage.value);
|
||||
}
|
||||
};
|
||||
|
||||
/** 读取运行统计 */
|
||||
const getStats = async () => {
|
||||
statsLoading.value = true;
|
||||
try {
|
||||
const res = await AnswerService.getStats();
|
||||
stats.value = res.data.data;
|
||||
} catch (error) {
|
||||
dashboardErrorMessage.value = getClientErrorMessage(
|
||||
error,
|
||||
"统计信息读取失败"
|
||||
);
|
||||
toast.error(dashboardErrorMessage.value);
|
||||
} finally {
|
||||
statsLoading.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
/** 读取最近问答记录;服务端按 page/size 返回当前页数据 */
|
||||
const getRecords = async (page = recordsPage.value) => {
|
||||
recordsLoading.value = true;
|
||||
@@ -197,10 +153,10 @@ export const useAnswerStore = defineStore("answer", () => {
|
||||
}
|
||||
};
|
||||
|
||||
/** Dashboard 首次加载时并行拉取运行状态、统计和最近记录 */
|
||||
/** Dashboard 首次加载时拉取最近记录 */
|
||||
const loadDashboard = async () => {
|
||||
dashboardErrorMessage.value = "";
|
||||
await Promise.allSettled([getHealth(), getStats(), getRecords()]);
|
||||
await getRecords();
|
||||
};
|
||||
|
||||
/** 清空缓存后刷新统计与表格;$fetch 非 2xx 时会抛出,错误由 catch 处理 */
|
||||
@@ -211,7 +167,7 @@ export const useAnswerStore = defineStore("answer", () => {
|
||||
try {
|
||||
await AnswerService.clearCache();
|
||||
toast.success("缓存已清空");
|
||||
await Promise.allSettled([getStats(), getRecords(1)]);
|
||||
await getRecords(1);
|
||||
} catch (error) {
|
||||
dashboardErrorMessage.value = getClientErrorMessage(
|
||||
error,
|
||||
@@ -230,11 +186,8 @@ export const useAnswerStore = defineStore("answer", () => {
|
||||
searchLoading,
|
||||
answerResult,
|
||||
searchErrorMessage,
|
||||
health,
|
||||
stats,
|
||||
records,
|
||||
dashboardErrorMessage,
|
||||
statsLoading,
|
||||
recordsLoading,
|
||||
cacheClearing,
|
||||
recordsPage,
|
||||
@@ -242,10 +195,7 @@ export const useAnswerStore = defineStore("answer", () => {
|
||||
selectedRecord,
|
||||
pagedRecords,
|
||||
recordCount,
|
||||
uptimeText,
|
||||
searchAnswer,
|
||||
getHealth,
|
||||
getStats,
|
||||
getRecords,
|
||||
loadDashboard,
|
||||
clearCache,
|
||||
|
||||
Reference in New Issue
Block a user