diff --git a/miniprogram/app.ts b/miniprogram/app.ts index 20ce685..8c29ae4 100644 --- a/miniprogram/app.ts +++ b/miniprogram/app.ts @@ -1,7 +1,5 @@ import { Logger } from "./core/Logger"; import { LevelLogLabel, LifeCycleLogLabel } from "./core/PresetLogLabel"; -import { API, IParamSetting } from "./core/Api"; -import { Storage } from "./core/Storage"; App({ @@ -17,56 +15,5 @@ App({ onLaunch() { Logger.log("小程序启动...", LevelLogLabel.TraceLabel, LifeCycleLogLabel.OnLaunchLabel); - - let s = new Storage("test", { - a: new Date(), - be: 2 - }); - - setTimeout(() => { - s.set("be", 12); - }, 1000) - - interface ITestApiInput { - name: string, - id: number, - info: { - data: string - } - } - - class TestApi extends API { - - public override key:string = "TestApi"; - - public override params: IParamSetting = { - name: { - tester: "123" - }, - id: { - parse: (i) => ++i, - }, - info: {} - } - - public constructor(data:ITestApiInput) { - super(data); - this.initLabel(); - this.initData(); - } - } - - function testApi() { - let api = new TestApi({ - name: "123", - id: 456, - info: { - data: "abc" - } - }); - console.log(api); - } - - testApi(); } }) \ No newline at end of file diff --git a/miniprogram/core/Api.ts b/miniprogram/core/Api.ts index e3098c8..486d673 100644 --- a/miniprogram/core/Api.ts +++ b/miniprogram/core/Api.ts @@ -6,6 +6,8 @@ interface IAnyData { [x:string]: any } +type IWxRequestOption = WechatMiniprogram.RequestOption; + type DeepReadonly = { readonly [P in keyof T]: DeepReadonly; }; @@ -22,14 +24,26 @@ type IParamSetting = { defaultValue?: T[P], /** - * 测试 + * ### 数据测试 + * 1、支持正则表达式测试 \ + * 2、支持使用 string === string 测试 \ + * 3、支持使用 number === number 测试 \ + * 4、支持使用自定义函数测试 */ - tester?: RegExp | ((data:T[P]) => boolean) | string, + tester?: RegExp | ((data:T[P]) => boolean) | string | number, /** - * 预解析函数 + * ### 预解析函数 + * 1、此函数用来处理该键值 \ + * 2、当返回 undefined 时此键值被遗弃 \ + * 3、返回值时,此键值被覆盖 + * + * @param data 当前给定数据 + * @param key 当前给定数据键值 + * @param all 全部输入数据 + * @returns 解析结果 */ - parse?: ((data:T[P], key:string, all:DeepReadonly>) => T[P]), + parse?: ((data:T[P], key:string, all:DeepReadonly>) => T[P] | undefined), /** * 是否为请求头数据 @@ -53,6 +67,10 @@ type IParamSetting = { */ class API { + public static get baseUrl():string { + return "https://xxx.xxx" + } + public static defaultLogLabel:LogLabel = new LogLabel( `API:API`, colorRadio(200, 120, 222) ); @@ -67,6 +85,11 @@ class API { */ public key:string = "API"; + /** + * API url + */ + public url:string = "/"; + /** * API 需要的参数列表 */ @@ -75,19 +98,29 @@ class API { /** * API 需要的数据 */ - public data:Partial; + public data?:Partial; /** * 请求数据 */ - public requestData:IAnyData = {} as any; + public requestData?:IWxRequestOption; + + /** + * 超时时间 (wx.request) + */ + public timeout?:number; + + /** + * 请求方法 (wx.request) + */ + public method:HTTPMethod = HTTPMethod.GET; /** * 构造函数 * 注意:data 是不安全的,请传入数据副本 * @param data API需要的全部数据 */ - public constructor(data? :Partial) { + public constructor(data?: Partial) { this.data = data ?? {}; } @@ -104,31 +137,38 @@ class API { * 初始化数据 */ public initData() { - for(let key in this.params) { + + if (this.data === void 0) { + Logger.log(`数据初始化异常: 没有输入 [data] 数据!`, + LevelLogLabel.FatalLabel, this.LogLabel); + return; + } + + for (let key in this.params) { let data = this.data[key]; - let { defaultValue, Optional, tester, parse } = this.params[key]; + let { defaultValue, Optional, tester } = this.params[key]; // 默认值赋予 - if(data === void 0 && defaultValue !== void 0) { + if (data === void 0 && defaultValue !== void 0) { this.data[key] = defaultValue; } // 数据存在测试 - if(data === void 0 && !Optional) { + if (data === void 0 && !Optional) { Logger.log(`数据校验异常: 数据 [${key}] 是必须的,但是并没有接收到!`, LevelLogLabel.FatalLabel, this.LogLabel); } // 用户自定义测试 - if(data !== void 0 && tester !== void 0) { + if (data !== void 0 && tester !== void 0) { let testRes:boolean = false; - if(tester instanceof RegExp) { + if (tester instanceof RegExp) { testRes = tester.test(data!); - } else if(typeof tester === "string") { + } else if (typeof tester === "string" || typeof tester === "number") { testRes = tester === data; - } else if(tester instanceof Function) { + } else if (tester instanceof Function) { testRes = tester(data!); } else { Logger.logMultiple( @@ -137,25 +177,61 @@ class API { ); } - if(!testRes) { + if (!testRes) { Logger.logMultiple( [LevelLogLabel.FatalLabel, this.LogLabel], `数据校验异常: [${ key }] 参数数据未通过自定义的 tester:`, data ); } } - - // 数据预解析 - if(parse !== void 0 && data !== void 0) { - this.data[key] = parse(data!, key, this.data as DeepReadonly>); - } } } /** * 收集请求数据 */ - public collectData() {} + public collectData() { + + if (this.data === void 0) { + Logger.log(`收集请求数据异常: 没有输入 [data] 数据!`, + LevelLogLabel.FatalLabel, this.LogLabel); + return; + } + + // 重置请求数据 + const requestData:IWxRequestOption = this.requestData = { + url: API.baseUrl + this.url, + }; + + for (let key in this.params) { + + let data = this.data[key]; + let { isHeader, isTemplate, parse } = this.params[key]; + + // 数据预解析 + if (parse !== void 0) { + data = parse(data!, key, this.data as DeepReadonly>); + } + + // 加载数据 + if (!isTemplate) { + if (isHeader) { + wx.request + } + } + } + } +} + +enum HTTPMethod { + OPTIONS = "OPTIONS", + GET = "GET", + HEAD = "HEAD", + POST = "POST", + PUT = "PUT", + DELETE = "DELETE", + TRACE = "TRACE", + CONNECT = "CONNECT" } export default API; diff --git a/miniprogram/pages/Timetable/TestCore.ts b/miniprogram/pages/Timetable/TestCore.ts new file mode 100644 index 0000000..0cb5ac3 --- /dev/null +++ b/miniprogram/pages/Timetable/TestCore.ts @@ -0,0 +1,63 @@ +import { Modular, Manager, ILifetime } from "../../core/Module"; +import { API, IParamSetting } from "../../core/Api"; +import { Storage } from "../../core/Storage"; + +/** + * 顶部状态栏 + */ +class TestCore extends Modular +implements Partial { + + public onLoad() { + + let s = new Storage("test", { + a: new Date(), + be: 2 + }); + + setTimeout(() => { + s.set("be", 12); + }, 1000) + + interface ITestApiInput { + name: string, + id: number, + info: { + data: string + } + } + + class TestApi extends API { + + public override key:string = "TestApi"; + + public override params: IParamSetting = { + name: { + tester: "123" + }, + id: { + parse: (i) => ++i, + }, + info: {} + } + + public constructor(data:ITestApiInput) { + super(data); + this.initLabel(); + this.initData(); + } + } + + let api = new TestApi({ + name: "123", + id: 456, + info: { + data: "abc" + } + }); + console.log(api); + } +} + +export default TestCore; +export { TestCore }; \ No newline at end of file diff --git a/miniprogram/pages/Timetable/Timetable.ts b/miniprogram/pages/Timetable/Timetable.ts index 82a4874..05c0898 100644 --- a/miniprogram/pages/Timetable/Timetable.ts +++ b/miniprogram/pages/Timetable/Timetable.ts @@ -1,5 +1,6 @@ import { Manager} from "../../core/Module"; import { StatusBar } from "./StatusBar"; +import { TestCore } from "./TestCore"; /** * 此页面使用 Manager 进行模块化管理 @@ -7,4 +8,5 @@ import { StatusBar } from "./StatusBar"; */ Manager.Page((manager)=>{ manager.addModule(StatusBar, "statusBar"); + manager.addModule(TestCore, "testCore"); }) \ No newline at end of file