import GameUtil from "../../core/GameUtil"; import SKDataUtil from "../util/SKDataUtil"; import SKLogger from "../util/SKLogger"; export enum NetCode { SUCCESS = 0, STATUS = 1, TIMEOUT = 2, ERROR = 3, } // Http請求任務 export default class SKNetTask { public static baseURL : string = ""; public path : string; public params : any; public jsonBlock : (code : NetCode, result : any) => void; private url : string; private xhr : XMLHttpRequest; private status : number; constructor(path : string, params : any, jsonBlock : (code : number, result : any) => void) { this.path = path; this.params = params; this.jsonBlock = jsonBlock; } static getWithJson(path : string, params : any, jsonBlock : (code : number, result : any) => void) { let task = new SKNetTask(path, params, jsonBlock); task.load(); } static postWithJson(path : string, params : any, jsonBlock : (code : number, result : any) => void) { let task = new SKNetTask(path, params, jsonBlock); task.load("POST"); } public urlParam(url : string, name : string, value : string) { url += (url.indexOf('?') == -1) ? '?' : '&'; url += encodeURIComponent(name) + "=" + encodeURIComponent(value); return url; } public param(param : string, name : string, value : string) { param += (param.length < 1) ? "" : "&"; param += encodeURIComponent(name) + "=" + encodeURIComponent(value); return param; } // 載入 public load(method : string = "GET") { cc.log("load...", SKNetTask.baseURL) if (!SKNetTask.baseURL || SKNetTask.baseURL.length < 1) { return; } cc.log("load1...", SKNetTask.baseURL) if (this.xhr) { return; } cc.log("load2...", SKNetTask.baseURL) let xhr = new XMLHttpRequest(); this.status = -1; let self = this; xhr.onerror = (event : ProgressEvent) => { let info = `$警告:帳號[${GameUtil.account}]請求失敗:${self.url}`; SKLogger.warn(info); SKLogger.warn(this.path); if (self.status < 200 || self.status >= 300) { console.log("網絡狀態碼:" + self.status) self.jsonBlock(NetCode.STATUS, null); } else { self.jsonBlock(NetCode.ERROR, null); } } xhr.ontimeout = (event : ProgressEvent) => { if (self.status == 0) { return; } let info = `$警告:帳號[${GameUtil.account}]請求超時:${self.url}`; cc.warn(info); self.jsonBlock(NetCode.TIMEOUT, null); } xhr.onreadystatechange = () => { if (xhr.readyState == 4) { let code = NetCode.ERROR; let data = null; try { self.status = xhr.status; if (self.status >= 200 && self.status < 300) { data = SKDataUtil.jsonBy(xhr.responseText); code = NetCode.SUCCESS; } else { code = NetCode.STATUS; SKLogger.warn(`$警告:帳號[${GameUtil.account}]狀態碼[${self.status}]錯誤:${self.url}`); } } catch (error) { SKLogger.warn(`$警告:帳號[${GameUtil.account}]網絡請求錯誤:${this.url}`); } finally { self.xhr = null; self.jsonBlock(code, data); } } }; this.url = this.path; if (this.path.indexOf("/") == 0 && SKNetTask.baseURL) { this.url = SKNetTask.baseURL + this.path; } let params = ``; if (this.params) { for (let key in this.params) { params = this.param(params, key, this.params[key]); } } if (method == "GET") { if (params.length > 0) { this.url = `${this.url}?${params}`; } SKLogger.debug(`接口GET請求:${this.url}`); xhr.open(method, this.url, true); xhr.send(); cc.log("load4...", this.url) } else if (method == "POST") { SKLogger.debug(`接口POST請求:${this.url}`); xhr.open(method, this.url, true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(params); } cc.log("load5...", SKNetTask.baseURL) } // 取消請求 public cancel() { if (this.xhr == null) { return; } this.xhr.abort(); this.xhr = null; } }