diff --git a/miniprogram/api/Login.ts b/miniprogram/api/Login.ts index d44b376..6a52f92 100644 --- a/miniprogram/api/Login.ts +++ b/miniprogram/api/Login.ts @@ -32,11 +32,6 @@ interface ILoginOutput { */ actualName: string; - /** - * 用户是否关注了公共号 - */ - isSubscribeWxAccount: boolean; - /** * 教务处的 session */ diff --git a/miniprogram/data/StudentInfo.ts b/miniprogram/data/StudentInfo.ts index aa6a2a8..2643800 100644 --- a/miniprogram/data/StudentInfo.ts +++ b/miniprogram/data/StudentInfo.ts @@ -1,12 +1,46 @@ import { Data } from "../core/Data"; +import { Storage } from "../core/Storage"; +import { Login, ILoginOutput } from "../api/Login"; /** * 登录状态 */ enum LoginStatus { - + + /** + * 已认证 + */ + verified = 1, + + /** + * 失效的认证 + * 通常为用户名密码错误 + */ + invalid = 2, + + /** + * 没有登录信息 + */ + none = 3 } +/** + * API 返回数据 + */ +type ILoginApiData = { + [P in keyof ILoginOutput]: { + type: ILoginOutput[P]; + getAsync: () => Promise; + } +} + +/** + * Storage 缓存数据类型 + */ +type IStudentInfoStorageData = ILoginOutput & { + [P in keyof IStudentInfoData]: IStudentInfoData[P]["type"]; +}; + /** * 学生信息数据结构 */ @@ -27,49 +61,94 @@ type IStudentInfoData = { }; /** - * 身份证后六位 - * 用于尝试水卡登录 + * 登录状态 */ - idCardLast6: { - type: string - }; + loginStatus: { + type: LoginStatus + } /** - * 使用的教务处链接 + * 上次登录时间 + * 时间戳 */ - eduService: { - type: string - }; - + lastLoginTime: { + type: number + } + /** - * 用户的真实姓名 + * 距离上次登录后 + * 学号和密码是否发生过改变 */ - actualName: { - type: string - }; - - /** - * 用户是否关注了公共号 - */ - isSubscribeWxAccount: { + isUserInfoChange: { type: boolean - }; - - /** - * 教务处的 session - */ - eduSession: { - type: string - }; + } } /** * 学生信息 */ -class StudentInfo extends Data { +class StudentInfo extends Data { + + /** + * 学生信息缓存 + */ + private eduStorage = new Storage("StudentInfo", { + idCardLast6: "", + eduService: "", + actualName: "", + eduSession: "", + studentId: "", + password: "", + loginStatus: LoginStatus.none, + lastLoginTime: 0, + isUserInfoChange: false + }); public override onLoad() { } -} \ No newline at end of file + /** + * 用户登录 + */ + private async login(): Promise { + + // 获取账号密码 + const stuId = this.eduStorage.get("studentId"); + const pwd = this.eduStorage.get("password"); + + if (!stuId || !pwd) return false; + + // 发送请求 + const data = await new Login().param({ + studentId: stuId, + password: pwd + }).request().wait(); + + // 请求成功 + let res = data.data; + if (res) { + + // 保存数据 + this.eduStorage.set("actualName", res.actualName); + this.eduStorage.set("eduService", res.eduService); + this.eduStorage.set("eduSession", res.eduSession); + this.eduStorage.set("idCardLast6", res.idCardLast6); + + // 记录时间 + this.eduStorage.set("lastLoginTime", new Date().getTime()); + + return true; + } else { + return false; + } + } + + /** + * 获取状态 + */ + private async getStatus() {} +} + +export { StudentInfo }; +export default StudentInfo; \ No newline at end of file