feat: add multi-account log center

This commit is contained in:
2026-03-27 17:55:01 +08:00
parent 45d4f2b008
commit 58d551eda6
8 changed files with 509 additions and 106 deletions

View File

@@ -1,5 +1,5 @@
import type { Accessor } from "solid-js";
import http from "~/service/http";
import http, { createHttpClient, type HttpClient } from "~/service/http";
import type { CourseType } from "~/types/Course";
import type { userInfoType } from "~/types/Userinfo";
@@ -131,25 +131,37 @@ export type StudyRunnerPayload = {
intervalSeconds: number;
items: StudyRunnerItem[];
isRunningStudy: Accessor<boolean>;
onLog?: (message: string) => void;
client: WkClient;
onLog?: (message: string, accoundID: string) => void;
};
export type WkClient = {
recordApi: (payload: RecordReq) => Promise<RecordRes>;
studyApi: (payload: StudyReq) => Promise<StudyRes>;
logoutApi: () => Promise<LogoutRes>;
};
export const loginApi = async (payload: LoginReq) => {
const res = await http.post<LoginRes>("/api/login", payload);
if (res.data?.session_id) {
sessionStorage.setItem("session_id", res.data.session_id);
}
return res;
};
export const recordApi = async (payload: RecordReq) => {
return await http.post<RecordRes>("/api/v2/record", payload);
};
const createWkClientFromHttp = (client: HttpClient): WkClient => ({
recordApi(payload) {
return client.post<RecordRes>("/api/v2/record", payload);
},
studyApi(payload) {
return client.post<StudyRes>("/api/v2/study", payload);
},
logoutApi() {
return client.post<LogoutRes>("/api/v2/logout");
},
});
export const studyApi = async (payload: StudyReq) => {
return await http.post<StudyRes>("/api/v2/study", payload);
export const createWkClient = (
resolveSessionId: () => string | undefined,
): WkClient => {
return createWkClientFromHttp(createHttpClient(resolveSessionId));
};
const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));
@@ -160,33 +172,32 @@ export const runStudyQueue = async (_payload: StudyRunnerPayload) => {
let currentTime = 0;
let count = 0;
let study_id = 0;
let total = item.totalTime - item.currentTime;
while (currentTime <= item.totalTime) {
while (currentTime <= total) {
if (!stopFlag()) {
_payload.onLog?.("⛔ 已手动停止");
_payload.onLog?.("⛔ 已手动停止", _payload.accountId);
return;
}
const message = `[${item.name}]: ${currentTime}/${item.totalTime}`;
console.log(message);
_payload.onLog?.(message);
const message = `[${item.name}]: ${currentTime}/${total}`;
_payload.onLog?.(message, _payload.accountId);
try {
const resp = await studyApi({
const resp = await _payload.client.studyApi({
node_id: item.nodeId,
study_id: String(study_id),
study_time: String(currentTime),
status: count === 0 ? 1 : currentTime >= item.totalTime ? 3 : 2,
status: count === 0 ? 1 : currentTime >= total ? 3 : 2,
});
study_id = resp.data.studyId;
if (currentTime === item.totalTime) break;
if (currentTime === total) break;
currentTime = Math.min(currentTime + 5, item.totalTime);
currentTime = Math.min(currentTime + 5, total);
count++;
} catch (error) {
const errorMessage = `请求失败: ${error instanceof Error ? error.message : String(error)}`;
console.log(errorMessage);
_payload.onLog?.(errorMessage);
_payload.onLog?.(errorMessage, _payload.accountId);
}
await sleep(5000);
@@ -194,12 +205,6 @@ export const runStudyQueue = async (_payload: StudyRunnerPayload) => {
}
};
export const logoutApi = async () => {
const res = await http.post<LogoutRes>("/api/v2/logout");
sessionStorage.removeItem("session_id");
return res;
};
export const hostApi = async () => {
return await http.get<HostRes>("/api/v1/host");
};