Add API Moudle!
This commit is contained in:
		
							parent
							
								
									41519b78e4
								
							
						
					
					
						commit
						0140136a0b
					
				| @ -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<ITestApiInput, {}> { | ||||
| 
 | ||||
|             public override key:string = "TestApi"; | ||||
|          | ||||
|             public override params: IParamSetting<ITestApiInput> = { | ||||
|                 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(); | ||||
|     } | ||||
| }) | ||||
| @ -6,6 +6,8 @@ interface IAnyData { | ||||
|     [x:string]: any | ||||
| } | ||||
| 
 | ||||
| type IWxRequestOption<O> = WechatMiniprogram.RequestOption<O>; | ||||
| 
 | ||||
| type DeepReadonly<T> = { | ||||
|     readonly [P in keyof T]: DeepReadonly<T[P]>; | ||||
| }; | ||||
| @ -22,14 +24,26 @@ type IParamSetting<T extends IAnyData> = { | ||||
|         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<Partial<T>>) => T[P]), | ||||
|         parse?: ((data:T[P], key:string, all:DeepReadonly<Partial<T>>) => T[P] | undefined), | ||||
| 
 | ||||
|         /** | ||||
|          * 是否为请求头数据 | ||||
| @ -53,6 +67,10 @@ type IParamSetting<T extends IAnyData> = { | ||||
|  */ | ||||
| class API<I extends IAnyData, O extends IAnyData> { | ||||
| 
 | ||||
|     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<I extends IAnyData, O extends IAnyData> { | ||||
|      */ | ||||
|     public key:string = "API"; | ||||
| 
 | ||||
|     /** | ||||
|      * API url | ||||
|      */ | ||||
|     public url:string = "/"; | ||||
| 
 | ||||
|     /** | ||||
|      * API 需要的参数列表 | ||||
|      */ | ||||
| @ -75,12 +98,22 @@ class API<I extends IAnyData, O extends IAnyData> { | ||||
|     /** | ||||
|      * API 需要的数据 | ||||
|      */ | ||||
|     public data:Partial<I>; | ||||
|     public data?:Partial<I>; | ||||
| 
 | ||||
|     /** | ||||
|      * 请求数据 | ||||
|      */ | ||||
|     public requestData:IAnyData = {} as any; | ||||
|     public requestData?:IWxRequestOption<O>; | ||||
| 
 | ||||
|     /** | ||||
|      * 超时时间 (wx.request) | ||||
|      */ | ||||
|     public timeout?:number; | ||||
| 
 | ||||
|     /** | ||||
|      * 请求方法 (wx.request) | ||||
|      */ | ||||
|     public method:HTTPMethod = HTTPMethod.GET; | ||||
| 
 | ||||
|     /** | ||||
|      * 构造函数 | ||||
| @ -104,10 +137,17 @@ class API<I extends IAnyData, O extends IAnyData> { | ||||
|      * 初始化数据 | ||||
|      */ | ||||
|     public initData() { | ||||
| 
 | ||||
|         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) { | ||||
| @ -126,7 +166,7 @@ class API<I extends IAnyData, O extends IAnyData> { | ||||
|                  | ||||
|                 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) { | ||||
|                     testRes = tester(data!); | ||||
| @ -144,18 +184,54 @@ class API<I extends IAnyData, O extends IAnyData> { | ||||
|                     ); | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|             // 数据预解析
 | ||||
|             if(parse !== void 0 && data !== void 0) { | ||||
|                 this.data[key] = parse(data!, key, this.data as DeepReadonly<Partial<I>>); | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * 收集请求数据 | ||||
|      */ | ||||
|     public collectData() {} | ||||
|     public collectData() { | ||||
| 
 | ||||
|         if (this.data === void 0) { | ||||
|             Logger.log(`收集请求数据异常: 没有输入 [data] 数据!`,  | ||||
|             LevelLogLabel.FatalLabel, this.LogLabel); | ||||
|             return; | ||||
|         } | ||||
| 
 | ||||
|         // 重置请求数据
 | ||||
|         const requestData:IWxRequestOption<O> = 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<Partial<I>>); | ||||
|             } | ||||
| 
 | ||||
|             // 加载数据
 | ||||
|             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; | ||||
|  | ||||
							
								
								
									
										63
									
								
								miniprogram/pages/Timetable/TestCore.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								miniprogram/pages/Timetable/TestCore.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,63 @@ | ||||
| import { Modular, Manager, ILifetime } from "../../core/Module"; | ||||
| import { API, IParamSetting } from "../../core/Api"; | ||||
| import { Storage } from "../../core/Storage"; | ||||
| 
 | ||||
| /** | ||||
|  * 顶部状态栏 | ||||
|  */ | ||||
| class TestCore<M extends Manager> extends Modular<M>  | ||||
| implements Partial<ILifetime> { | ||||
| 
 | ||||
|     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<ITestApiInput, {}> { | ||||
| 
 | ||||
|             public override key:string = "TestApi"; | ||||
|          | ||||
|             public override params: IParamSetting<ITestApiInput> = { | ||||
|                 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 }; | ||||
| @ -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"); | ||||
| }) | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 ben.qin
						ben.qin