From 4f83dd6822b95c05647d4ddf579a90344a65d2fb Mon Sep 17 00:00:00 2001 From: MrKBear Date: Wed, 12 Jan 2022 15:24:59 +0800 Subject: [PATCH] Add Login API(#20) --- miniprogram/api/Login.ts | 149 ++++++++++++++++++++++++ miniprogram/core/Api.ts | 29 ++++- miniprogram/pages/Timetable/TestCore.ts | 45 ++----- 3 files changed, 179 insertions(+), 44 deletions(-) create mode 100644 miniprogram/api/Login.ts diff --git a/miniprogram/api/Login.ts b/miniprogram/api/Login.ts new file mode 100644 index 0000000..47b3caf --- /dev/null +++ b/miniprogram/api/Login.ts @@ -0,0 +1,149 @@ +import { API, HTTPMethod, IParamSetting, GeneralCallbackResult } from "../core/Api"; + +interface ILoginInput { + + /** + * 学号 + */ + studentId: string; + + /** + * 教务处密码 + */ + password: string; +} + +interface ILoginOutput { + + /** + * 身份证后六位 + * 用于尝试水卡登录 + */ + idCardLast6: string; + + /** + * 使用的教务处链接 + */ + eduService: string; + + /** + * 用户的真实姓名 + */ + actualName: string; + + /** + * 用户是否关注了公共号 + */ + isSubscribeWxAccount: boolean; + + /** + * 教务处的 session + */ + eduSession: string; +} + +interface ILoginEvent { + + /** + * session 过期 + */ + expire: GeneralCallbackResult; + + /** + * 登录失败 + */ + unauthorized: GeneralCallbackResult; + + /** + * 未知的问题 + */ + error: GeneralCallbackResult; + + /** + * 数据损坏或丢失 + */ + badData: GeneralCallbackResult; +} + +/** + * Login API + * 此 API 用来向教务处发起登录请求 + * 请求成功后将获得教务处返回的 session + */ +class Login extends API { + + public override url: string = "/login"; + + public override method: HTTPMethod = HTTPMethod.POST; + + public override params: IParamSetting = { + + studentId: { + mapKey: "id", + tester: /^\d{8,12}$/, + }, + + password: { + mapKey: "pwd" + } + }; + + public constructor() { + 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 }); + }); + } +} + +export { Login, ILoginInput, ILoginOutput }; \ No newline at end of file diff --git a/miniprogram/core/Api.ts b/miniprogram/core/Api.ts index 05bab2f..21dda78 100644 --- a/miniprogram/core/Api.ts +++ b/miniprogram/core/Api.ts @@ -32,6 +32,11 @@ type DeepReadonly = { */ type IParamSetting = { [P in keyof T]: { + + /** + * 键值映射 + */ + mapKey?: string, /** * 默认值 @@ -130,13 +135,13 @@ type IAPIResultEvent = { * 无论因为什么 * 总之数据获取到 */ - no: { message: string } & U, + no: GeneralCallbackResult & U, /** * 完成了 * 无论失败与否 */ - done: { message: string, data: O } & U + done: { data?: O } & GeneralCallbackResult & U } /** @@ -343,14 +348,15 @@ class API< for (let key in this.params) { let data = this.data[key]; - let { isHeader, isTemplate } = this.params[key]; + let { isHeader, isTemplate, mapKey } = this.params[key]; + let useKey = mapKey ?? key; // 加载数据 if (!isTemplate) { if (isHeader) { - requestData.header![key] = data; + requestData.header![useKey] = data; } else { - (requestData.data as IAnyData)[key] = data; + (requestData.data as IAnyData)[useKey] = data; } } } @@ -593,6 +599,17 @@ class API< return this; } + /** + * 自动挂载失败回调 + */ + public addFailedCallBack(): this { + this.on("fail", (e) => { + this.emit("no", e as any); + this.emit("done", e as any); + }); + return this; + } + /** * 请求失败后的提示语句 */ @@ -680,4 +697,4 @@ enum HTTPMethod { } export default API; -export { API, IParamSetting, IAppAPIParam, ICallBack, HTTPMethod, RequestPolicy } \ No newline at end of file +export { API, IParamSetting, IAppAPIParam, ICallBack, HTTPMethod, RequestPolicy, GeneralCallbackResult } \ No newline at end of file diff --git a/miniprogram/pages/Timetable/TestCore.ts b/miniprogram/pages/Timetable/TestCore.ts index 70259d0..6a81997 100644 --- a/miniprogram/pages/Timetable/TestCore.ts +++ b/miniprogram/pages/Timetable/TestCore.ts @@ -1,5 +1,5 @@ import { Modular, Manager, ILifetime } from "../../core/Module"; -import { API, IParamSetting } from "../../core/Api"; +import { Login } from "../../api/Login"; import { Storage } from "../../core/Storage"; /** @@ -27,44 +27,13 @@ implements Partial { setTimeout(() => { s.set("be", 12); }, 1000) - - interface ITestApiInput { - name: string, - id: number, - info: { - data: string - } - } - class TestApi extends API { - - public override params: IParamSetting = { - name: { - tester: "123", - isHeader: true - }, - id: { - parse: (i) => ++i, - }, - info: {} - } - - public constructor() { - super(); - this.initDebugLabel("TestApi"); - } - } - - let api = new TestApi(); - api.param({ - name: "123", - id: 456, - info: { - data: "abc" - } - }).request().waitRequest({ - success: (d) => console.log(d) - }) + new Login().param({studentId: "1806240113", password: "qazxsw123"}) + .request().wait({ + ok: (w) => {console.log("ok", w)}, + no: (w) => {console.log("no", w)}, + done: (w) => {console.log("done", w)} + }); } }