diff --git a/miniprogram/api/EduBase.ts b/miniprogram/api/EduBase.ts new file mode 100644 index 0000000..041fbb5 --- /dev/null +++ b/miniprogram/api/EduBase.ts @@ -0,0 +1,95 @@ +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 = {} +> extends API { + + 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; \ No newline at end of file diff --git a/miniprogram/api/Login.ts b/miniprogram/api/Login.ts index 47b3caf..d44b376 100644 --- a/miniprogram/api/Login.ts +++ b/miniprogram/api/Login.ts @@ -1,4 +1,5 @@ -import { API, HTTPMethod, IParamSetting, GeneralCallbackResult } from "../core/Api"; +import { HTTPMethod, IParamSetting } from "../core/Api"; +import { EduBase } from "./EduBase"; interface ILoginInput { @@ -42,35 +43,12 @@ interface ILoginOutput { eduSession: string; } -interface ILoginEvent { - - /** - * session 过期 - */ - expire: GeneralCallbackResult; - - /** - * 登录失败 - */ - unauthorized: GeneralCallbackResult; - - /** - * 未知的问题 - */ - error: GeneralCallbackResult; - - /** - * 数据损坏或丢失 - */ - badData: GeneralCallbackResult; -} - /** * Login API * 此 API 用来向教务处发起登录请求 * 请求成功后将获得教务处返回的 session */ -class Login extends API { +class Login extends EduBase { public override url: string = "/login"; @@ -92,57 +70,13 @@ class Login extends API { super(); this.initDebugLabel("Login"); - this.addFailedCallBack(); - - this.on("success", (data) => { - - let isSuccess = true; - 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 }); - }); + this.useEduCallback((data) => ({ + idCardLast6: data.idCard, + eduService: data.ip, + actualName: data.name, + isSubscribeWxAccount: data.official, + eduSession: data.session + })); } } diff --git a/miniprogram/core/Api.ts b/miniprogram/core/Api.ts index 7c772f1..b83e58c 100644 --- a/miniprogram/core/Api.ts +++ b/miniprogram/core/Api.ts @@ -698,4 +698,4 @@ enum HTTPMethod { } export default API; -export { API, IParamSetting, IAppAPIParam, ICallBack, HTTPMethod, RequestPolicy, GeneralCallbackResult } \ No newline at end of file +export { API, IParamSetting, IAppAPIParam, IAnyData, ICallBack, HTTPMethod, RequestPolicy, GeneralCallbackResult } \ No newline at end of file