Add Login API(#20)

This commit is contained in:
MrKBear 2022-01-12 15:24:59 +08:00
parent 478eafa4a3
commit 4f83dd6822
3 changed files with 179 additions and 44 deletions

149
miniprogram/api/Login.ts Normal file
View File

@ -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<ILoginInput, ILoginOutput, ILoginEvent> {
public override url: string = "/login";
public override method: HTTPMethod = HTTPMethod.POST;
public override params: IParamSetting<ILoginInput> = {
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 };

View File

@ -32,6 +32,11 @@ type DeepReadonly<T> = {
*/
type IParamSetting<T extends IAnyData> = {
[P in keyof T]: {
/**
*
*/
mapKey?: string,
/**
*
@ -130,13 +135,13 @@ type IAPIResultEvent<O extends IAnyData, U extends IAnyData> = {
*
*
*/
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 }
export { API, IParamSetting, IAppAPIParam, ICallBack, HTTPMethod, RequestPolicy, GeneralCallbackResult }

View File

@ -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<ILifetime> {
setTimeout(() => {
s.set("be", 12);
}, 1000)
interface ITestApiInput {
name: string,
id: number,
info: {
data: string
}
}
class TestApi extends API<ITestApiInput, {}> {
public override params: IParamSetting<ITestApiInput> = {
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)}
});
}
}