mirror of
				https://github.com/Gongxh0901/kunpolibrary
				synced 2025-10-31 03:15:45 +00:00 
			
		
		
		
	仓库中添加内置的demo
This commit is contained in:
		
							
								
								
									
										43
									
								
								demo/assets/script/Net/NetHelper.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								demo/assets/script/Net/NetHelper.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,43 @@ | ||||
| /** | ||||
|  * @Author: Gongxh | ||||
|  * @Date: 2024-05-20 | ||||
|  * @Description:  | ||||
|  */ | ||||
| import { HttpManager, IHttpEvent } from "kunpocc-net"; | ||||
| import { kunpo } from "../header"; | ||||
| import { ServerConfig } from "./header"; | ||||
|  | ||||
|  | ||||
| export class NetHelper { | ||||
|     public static get url(): string { return ServerConfig.url }; | ||||
|     public static get appid(): string { return ServerConfig.appid }; | ||||
|     public static get secret(): string { return ServerConfig.secret }; | ||||
|  | ||||
|     public static send(url: string, data: any, netEvent: IHttpEvent) { | ||||
|         netEvent.data = new Date().getTime(); | ||||
|         let sendData = JSON.stringify(data); | ||||
|         console.log(`http request\n name:${netEvent.name}\n   url=${this.url + url}\n   data=${sendData}`); | ||||
|         HttpManager.post(this.url + url, sendData, "json", netEvent, this.formatHeaders(netEvent.data, data), 5); | ||||
|     } | ||||
|  | ||||
|     private static formatHeaders(time: number, data: any): any[] { | ||||
|         return ["content-type", "application/json", "sign", this.signCalculateJSON(time, data), "time", encodeURIComponent(String(time)), "authorization", ""]; | ||||
|     } | ||||
|  | ||||
|     /** 计算签名 */ | ||||
|     public static signCalculateJSON(time: number, data: any): string { | ||||
|         let signData = { | ||||
|             appid: this.appid, | ||||
|             signkey: this.secret, | ||||
|             time: time, | ||||
|             body: JSON.stringify(data) | ||||
|         } | ||||
|         let keys = Object.keys(signData); | ||||
|         keys.sort(); | ||||
|         let signStr = []; | ||||
|         for (let key of keys) { | ||||
|             signStr.push(`${key}=${signData[key]}&`); | ||||
|         } | ||||
|         return encodeURIComponent(kunpo.md5(signStr.join(""))); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										9
									
								
								demo/assets/script/Net/NetHelper.ts.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								demo/assets/script/Net/NetHelper.ts.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| { | ||||
|   "ver": "4.0.24", | ||||
|   "importer": "typescript", | ||||
|   "imported": true, | ||||
|   "uuid": "e9a1a84a-bf3a-4b94-aeea-9160d84713f8", | ||||
|   "files": [], | ||||
|   "subMetas": {}, | ||||
|   "userData": {} | ||||
| } | ||||
							
								
								
									
										65
									
								
								demo/assets/script/Net/NetTaskBase.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								demo/assets/script/Net/NetTaskBase.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| /** | ||||
|  * @Author: Gongxh | ||||
|  * @Date: 2024-05-20 | ||||
|  * @Description: 网络基类 | ||||
|  */ | ||||
|  | ||||
| import { HttpTask, IHttpResponse } from "kunpocc-net"; | ||||
| import { INetResponse } from "./header"; | ||||
|  | ||||
| export abstract class NetTaskBase extends HttpTask { | ||||
|     protected url: string = ''; | ||||
|     private _succeed: (data: any) => void; | ||||
|     private _httpError: (data: any) => void; | ||||
|     private _taskError: (data: any) => void; | ||||
|  | ||||
|     /** | ||||
|      * 设置回调 | ||||
|      * @param {object} res 回调函数 | ||||
|      * @param {function} res.succeed 任务成功回调 | ||||
|      * @param {function} res.taskError 任务错误回调 (一般是服务端返回错误码) | ||||
|      * @param {function} res.httpError http错误回调 (一般是网络错误) | ||||
|      */ | ||||
|     public setTaskCallback(res: { succeed: (response: any) => void, taskError?: (response: any) => void, httpError?: (response: any) => void }) { | ||||
|         this._succeed = res?.succeed; | ||||
|         this._httpError = res?.httpError; | ||||
|         this._taskError = res?.taskError; | ||||
|     } | ||||
|  | ||||
|     public onComplete(response: IHttpResponse): void { | ||||
|         try { | ||||
|             let data = response.data as INetResponse; | ||||
|             if (data.responseStatus == 0) { | ||||
|                 console.log(`http response\n   name:${this.name}\n   url=${this.url}\n   data=${JSON.stringify(data)}`); | ||||
|                 this.onTaskComplete(data); | ||||
|                 this._succeed?.(data); | ||||
|             } else { | ||||
|                 throw new Error("任务错误"); | ||||
|             } | ||||
|         } catch (error) { | ||||
|             let data = response.data as INetResponse; | ||||
|             console.log(`http response task error\n   name:${this.name}\n   url:${this.url}\n   responseStatus:${data.responseStatus}\n   data:${JSON.stringify(data)}`); | ||||
|             this.onTaskError(data.responseStatus, response.data); | ||||
|             this._taskError?.(response.data); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public onError(response: IHttpResponse): void { | ||||
|         let message = response.message; | ||||
|         let statusCode = response.statusCode; | ||||
|         console.log(`http response error\n   name:${this.name}\n   url:${this.url}\n   message:${message}\n   statusCode:${statusCode}`); | ||||
|         this.onHttpError(message); | ||||
|         this._httpError?.(response.data); | ||||
|     } | ||||
|  | ||||
|     /** 任务完成 子类必须实现 */ | ||||
|     protected abstract onTaskComplete(data: any): void; | ||||
|     /** 任务错误 子类实现 */ | ||||
|     protected onTaskError(errcode: number, data: any): void { | ||||
|  | ||||
|     }; | ||||
|     /** http错误 子类实现 */ | ||||
|     protected onHttpError(message: string): void { | ||||
|  | ||||
|     }; | ||||
| } | ||||
							
								
								
									
										9
									
								
								demo/assets/script/Net/NetTaskBase.ts.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								demo/assets/script/Net/NetTaskBase.ts.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| { | ||||
|   "ver": "4.0.24", | ||||
|   "importer": "typescript", | ||||
|   "imported": true, | ||||
|   "uuid": "8c6e5935-bded-41fb-9df8-21dd76f82186", | ||||
|   "files": [], | ||||
|   "subMetas": {}, | ||||
|   "userData": {} | ||||
| } | ||||
							
								
								
									
										28
									
								
								demo/assets/script/Net/header.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								demo/assets/script/Net/header.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| /** | ||||
|  * @Author: Gongxh | ||||
|  * @Date: 2024-12-28 | ||||
|  * @Description:  | ||||
|  */ | ||||
|  | ||||
| export interface INetResponse { | ||||
|     responseStatus: number; | ||||
|     packet: any; | ||||
| } | ||||
|  | ||||
| export interface IServerInfo { | ||||
|     /** 名称 */ | ||||
|     name: string, | ||||
|     /** http地址 */ | ||||
|     url: string, | ||||
|     /** 应用id */ | ||||
|     appid: string, | ||||
|     /** 密钥 */ | ||||
|     secret: string, | ||||
| } | ||||
|  | ||||
| export const ServerConfig: IServerInfo = { | ||||
|     name: "dev-gblnn", | ||||
|     url: "", | ||||
|     appid: "", | ||||
|     secret: "", | ||||
| } | ||||
							
								
								
									
										9
									
								
								demo/assets/script/Net/header.ts.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								demo/assets/script/Net/header.ts.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| { | ||||
|   "ver": "4.0.24", | ||||
|   "importer": "typescript", | ||||
|   "imported": true, | ||||
|   "uuid": "cad69694-c4c3-4d7d-b490-26e5ebd1141e", | ||||
|   "files": [], | ||||
|   "subMetas": {}, | ||||
|   "userData": {} | ||||
| } | ||||
							
								
								
									
										9
									
								
								demo/assets/script/Net/task.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								demo/assets/script/Net/task.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| { | ||||
|   "ver": "1.2.0", | ||||
|   "importer": "directory", | ||||
|   "imported": true, | ||||
|   "uuid": "35d378d9-a2a5-407b-afec-9c7e55e408cf", | ||||
|   "files": [], | ||||
|   "subMetas": {}, | ||||
|   "userData": {} | ||||
| } | ||||
							
								
								
									
										44
									
								
								demo/assets/script/Net/task/NetAuth.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								demo/assets/script/Net/task/NetAuth.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| /** | ||||
|  * @Author: Gongxh | ||||
|  * @Date: 2024-05-20 | ||||
|  * @Description: 新版鉴权接口 | ||||
|  */ | ||||
| import { kunpo } from "../../header"; | ||||
| import { INetResponse } from "../header"; | ||||
| import { NetHelper } from "../NetHelper"; | ||||
| import { NetTaskBase } from "../NetTaskBase"; | ||||
|  | ||||
| interface IAuthResponse extends INetResponse { | ||||
|     token: string; | ||||
|     uid: number;    // 用户id | ||||
|     sdkId: string; | ||||
|     channel: number; | ||||
|     /** 注册渠道 */ | ||||
|     regChannel: number; | ||||
| } | ||||
|  | ||||
| /** 账户认证 */ | ||||
| export class NetAuth extends NetTaskBase { | ||||
|     name: string = "NetSocketAuth"; | ||||
|     url: string = '/api/game/auth'; | ||||
|     constructor() { | ||||
|         super() | ||||
|     } | ||||
|  | ||||
|     public start(): void { | ||||
|         let data = { | ||||
|             channel: 25, | ||||
|             token: kunpo.Platform.deviceId, | ||||
|             openid: kunpo.Platform.deviceId, | ||||
|             sdkChannel: 25, | ||||
|             distinctId: '' | ||||
|         } | ||||
|         NetHelper.send(this.url, data, this); | ||||
|     } | ||||
|  | ||||
|     protected onTaskComplete(data: IAuthResponse): void { | ||||
|  | ||||
|     } | ||||
| } | ||||
|  | ||||
|  | ||||
							
								
								
									
										9
									
								
								demo/assets/script/Net/task/NetAuth.ts.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								demo/assets/script/Net/task/NetAuth.ts.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| { | ||||
|   "ver": "4.0.24", | ||||
|   "importer": "typescript", | ||||
|   "imported": true, | ||||
|   "uuid": "10bbcb28-912a-4ba5-9bfd-a21668ee8763", | ||||
|   "files": [], | ||||
|   "subMetas": {}, | ||||
|   "userData": {} | ||||
| } | ||||
		Reference in New Issue
	
	Block a user