Compare commits

..

No commits in common. "4da257c2e2f674a112f6ee13373c07b22309c3c7" and "a8e16f5972a2d8f3ef54144cda3faa6425098a9a" have entirely different histories.

5 changed files with 111 additions and 145 deletions

View File

@ -1,95 +0,0 @@
import { API, IAnyData, GeneralCallbackResult } from "../core/Api";
import { EventType } from "../core/Emitter";
interface ILoginEvent {
/**
* session
*/
expire: GeneralCallbackResult;
/**
*
*/
unauthorized: GeneralCallbackResult;
/**
*
*/
error: GeneralCallbackResult;
/**
*
*/
badData: GeneralCallbackResult;
}
/**
* API
* @template I API
* @template O API
* @template E API
* @template U
*/
abstract class EduBase<
I extends IAnyData = IAnyData,
O extends IAnyData = IAnyData,
E extends Record<EventType, any> = {}
> extends API<I, O, {
[P in (keyof ILoginEvent | keyof E)]: P extends keyof ILoginEvent ? ILoginEvent[P] : E[P]
}> {
protected useEduCallback(
parseFunction: (data: any) => O
): void {
this.addFailedCallBack();
this.on("success", (data) => {
let isSuccess = true;
let errMsg = "";
let res: O | undefined;
const info: any = data.data;
// 数据缺失检测
if(!info) {
isSuccess = false;
errMsg = "Bad Data";
this.emit("badData", { errMsg });
}
if (isSuccess) switch (info.code) {
case (1):
res = parseFunction(info.data);
errMsg = info.err_msg ?? "Success";
this.emit("ok", res!);
break;
case (2):
isSuccess = false;
errMsg = info.err_msg ?? "Session Expire";
this.emit("expire", { errMsg });
break;
case (3):
isSuccess = false;
errMsg = info.err_msg ?? "Unauthorized";
this.emit("unauthorized", { errMsg });
break;
case (4):
isSuccess = false;
errMsg = info.err_msg ?? "Error";
this.emit("error", { errMsg });
break;
}
if (!isSuccess) this.emit("no", { errMsg });
this.emit("done", { errMsg, data: res });
});
}
}
export { EduBase };
export default EduBase;

View File

@ -1,5 +1,4 @@
import { HTTPMethod, IParamSetting } from "../core/Api"; import { API, HTTPMethod, IParamSetting, GeneralCallbackResult } from "../core/Api";
import { EduBase } from "./EduBase";
interface ILoginInput { interface ILoginInput {
@ -43,12 +42,35 @@ interface ILoginOutput {
eduSession: string; eduSession: string;
} }
interface ILoginEvent {
/**
* session
*/
expire: GeneralCallbackResult;
/**
*
*/
unauthorized: GeneralCallbackResult;
/**
*
*/
error: GeneralCallbackResult;
/**
*
*/
badData: GeneralCallbackResult;
}
/** /**
* Login API * Login API
* API * API
* session * session
*/ */
class Login extends EduBase<ILoginInput, ILoginOutput> { class Login extends API<ILoginInput, ILoginOutput, ILoginEvent> {
public override url: string = "/login"; public override url: string = "/login";
@ -70,13 +92,57 @@ class Login extends EduBase<ILoginInput, ILoginOutput> {
super(); super();
this.initDebugLabel("Login"); this.initDebugLabel("Login");
this.useEduCallback((data) => ({ this.addFailedCallBack();
idCardLast6: data.idCard,
eduService: data.ip, this.on("success", (data) => {
actualName: data.name,
isSubscribeWxAccount: data.official, let isSuccess = true;
eduSession: data.session let errMsg = "";
})); let res: ILoginOutput | undefined;
const info: any = data.data;
// 数据缺失检测
if(!info) {
isSuccess = false;
errMsg = "Bad Data";
this.emit("badData", { errMsg });
}
if (isSuccess) switch (info.code) {
case (1):
res = {
idCardLast6: info.data.idCard,
eduService: info.data.ip,
actualName: info.data.name,
isSubscribeWxAccount: info.data.official,
eduSession: info.data.session
};
errMsg = info.err_msg ?? "Success";
this.emit("ok", res);
break;
case (2):
isSuccess = false;
errMsg = info.err_msg ?? "Session Expire";
this.emit("expire", { errMsg });
break;
case (3):
isSuccess = false;
errMsg = info.err_msg ?? "Unauthorized";
this.emit("unauthorized", { errMsg });
break;
case (4):
isSuccess = false;
errMsg = info.err_msg ?? "Error";
this.emit("error", { errMsg });
break;
}
if (!isSuccess) this.emit("no", { errMsg });
this.emit("done", { errMsg, data: res });
});
} }
} }

View File

@ -1,5 +1,4 @@
import { HTTPMethod, IParamSetting } from "../core/Api"; import { API, HTTPMethod, IParamSetting, GeneralCallbackResult} from "../core/Api";
import { EduBase } from "./EduBase";
interface IScheduleInput { interface IScheduleInput {
@ -37,7 +36,7 @@ interface IClassData {
week: string; week: string;
} }
type IScheduleOutput = { interface IScheduleOutput {
/** /**
* *
@ -50,15 +49,38 @@ type IScheduleOutput = {
index: number; index: number;
}[]; }[];
interface IScheduleEvent {
/**
* session
*/
expire: GeneralCallbackResult;
/**
*
*/
unauthorized: GeneralCallbackResult;
/**
*
*/
error: GeneralCallbackResult;
/**
*
*/
badData: GeneralCallbackResult;
}
/** /**
* Schedule API * Schedule API
* session与semester * session与semester
* API * API
* JSON文件 * JSON文件
*/ */
class Schedlue extends EduBase<IScheduleInput, IScheduleOutput> { class Schedlue extends API<IScheduleInput, IScheduleOutput, IScheduleEvent> {
public override baseUrl: string = "https://jwc.2333.pub"; public override baseUrl: string = "jwc.2333.pub";
public override url = "/course_timetable"; public override url = "/course_timetable";
@ -80,31 +102,7 @@ class Schedlue extends EduBase<IScheduleInput, IScheduleOutput> {
super(); super();
this.initDebugLabel("Schedule"); this.initDebugLabel("Schedule");
this.useEduCallback((data) => { this.addFailedCallBack();
const res: IScheduleOutput = [];
for( let i = 0; i < data.length; i++ ) {
const classList: IClassData[] = [];
const CTTDetails = data[i].CTTDetails ?? [];
for( let j = 0; j < CTTDetails.length; j++ ) {
classList.push({
name: CTTDetails[j].Name,
room: CTTDetails[j].Room,
teacher: CTTDetails[j].Teacher,
week: CTTDetails[j].Week,
})
}
res.push({
classList,
index: data[i].Id
})
}
return res;
});
} }
} }

View File

@ -698,4 +698,4 @@ enum HTTPMethod {
} }
export default API; export default API;
export { API, IParamSetting, IAppAPIParam, IAnyData, ICallBack, HTTPMethod, RequestPolicy, GeneralCallbackResult } export { API, IParamSetting, IAppAPIParam, ICallBack, HTTPMethod, RequestPolicy, GeneralCallbackResult }

View File

@ -1,6 +1,5 @@
import { Modular, Manager, ILifetime } from "../../core/Module"; import { Modular, Manager, ILifetime } from "../../core/Module";
import { Login } from "../../api/Login"; import { Login } from "../../api/Login";
import { Schedlue } from "../../api/Schedule"
import { Storage } from "../../core/Storage"; import { Storage } from "../../core/Storage";
/** /**
@ -29,14 +28,12 @@ implements Partial<ILifetime> {
s.set("be", 12); s.set("be", 12);
}, 1000) }, 1000)
// new Login().param({studentId: "2017060129", password: ""}) new Login().param({studentId: "2017060129", password: "hch2000210%"})
// .request().wait({ .request().wait({
// ok: (w) => {console.log("ok", w)}, ok: (w) => {console.log("ok", w)},
// no: (w) => {console.log("no", w)}, no: (w) => {console.log("no", w)},
// done: (w) => {console.log("done", w)} done: (w) => {console.log("done", w)}
// }); });
// new Schedlue().param({cookie:"C729D1AB1B17077485ACCD9279135C22",semester:"2020-2021-2"})
// .request()
} }
} }