feat: 完成整体内容开发
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
// app/services/answer_service.ts - OCS AI 答题服务相关 API
|
||||
import type {
|
||||
IClearCacheResponse,
|
||||
IHealthResponse,
|
||||
IRecordsResponse,
|
||||
ISearchRequest,
|
||||
ISearchResponse,
|
||||
IStatsResponse
|
||||
} from "@/interfaces";
|
||||
|
||||
import BaseClientService from "@/services/base_service";
|
||||
|
||||
/** 当前 Nuxt 站点内 API 的基础路径,部署到子路径时由 Nuxt 自己处理 */
|
||||
const BaseUrl = "";
|
||||
|
||||
/** OCS AI 答题服务请求集合 */
|
||||
export class AnswerService {
|
||||
public static basePath = `${BaseUrl}/api`;
|
||||
|
||||
/** 调用答题接口;前端统一走 JSON POST,服务端仍兼容 OCS 的 GET 调用 */
|
||||
public static search(request: ISearchRequest) {
|
||||
return BaseClientService.post<ISearchResponse, ISearchRequest>(
|
||||
`${AnswerService.basePath}/search`,
|
||||
request
|
||||
);
|
||||
}
|
||||
|
||||
/** 读取健康状态;无需 token */
|
||||
public static getHealth() {
|
||||
return BaseClientService.get<IHealthResponse>(
|
||||
`${AnswerService.basePath}/health`
|
||||
);
|
||||
}
|
||||
|
||||
/** 读取运行统计;配置 ACCESS_TOKEN 时由服务端校验 */
|
||||
public static getStats() {
|
||||
return BaseClientService.get<IStatsResponse>(
|
||||
`${AnswerService.basePath}/stats`
|
||||
);
|
||||
}
|
||||
|
||||
/** 读取最近问答记录,供 Dashboard 表格展示 */
|
||||
public static getRecords() {
|
||||
return BaseClientService.get<IRecordsResponse>(
|
||||
`${AnswerService.basePath}/records`
|
||||
);
|
||||
}
|
||||
|
||||
/** 清空当前进程内的答题缓存 */
|
||||
public static clearCache() {
|
||||
return BaseClientService.post<IClearCacheResponse>(
|
||||
`${AnswerService.basePath}/cache/clear`
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// app/services/base_service.ts - 前端请求基础封装,保持和其他项目相近的调用风格
|
||||
|
||||
/** GET query 可接受的基础值类型 */
|
||||
type QueryValue = string | number | boolean | null | undefined;
|
||||
|
||||
/** GET query 对象;空值会在请求前被过滤掉 */
|
||||
type QueryParams = Record<string, QueryValue>;
|
||||
|
||||
/** 把 undefined/null/空字符串从 query 中移除,避免拼出无意义参数 */
|
||||
const cleanQuery = (query?: QueryParams) => {
|
||||
if (!query) return undefined;
|
||||
|
||||
return Object.fromEntries(
|
||||
Object.entries(query).filter(([, value]) => {
|
||||
return value !== undefined && value !== null && value !== "";
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* 基础请求类
|
||||
*
|
||||
* 其他项目里 service 方法返回 axios response,这里用 `$fetch` 包一层 `{ data }`,
|
||||
* 让 store 层仍然可以写成 `res.data`,后续替换请求库也不会影响组件。
|
||||
*/
|
||||
export default class BaseClientService {
|
||||
/** 发起 GET 请求 */
|
||||
public static async get<T>(url: string, query?: QueryParams) {
|
||||
const data = await $fetch<T>(url, {
|
||||
method: "GET",
|
||||
query: cleanQuery(query)
|
||||
});
|
||||
|
||||
return { data };
|
||||
}
|
||||
|
||||
/** 发起 POST 请求 */
|
||||
public static async post<T, B extends object | undefined = undefined>(
|
||||
url: string,
|
||||
body?: B
|
||||
) {
|
||||
const data = await $fetch<T>(url, {
|
||||
method: "POST",
|
||||
body
|
||||
});
|
||||
|
||||
return { data };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// app/services/index.ts - 前端 service 统一出口
|
||||
export * from "./answer_service";
|
||||
Reference in New Issue
Block a user