"use strict"; cc._RF.push(module, 'd0b5fM6JHFD5qb7q/b58nOw', 'nx.web.task'); // Scripts/nx/web/nx.web.task.js "use strict"; /******************************************************************************* * * Nx WEB单任务 * * * * 2021.12.10 ******************************************************************************/ var ERR = require("error"); var NxObject = require("nx.object"); var Crypto = require("cryptoHelper"); var TimeOut = 5000; var NxWebTask = cc.Class({ "extends": NxObject, name: "NxWebTask", // 初始化 initialize: function initialize(_args) { // USPER if (!this._super(_args)) { return false; } this.key = _args.key; // 唯一键值 this.manager = _args.mgr; // 管理器 this.reqType = _args.reqType; // 请求类型 this.reqURL = _args.reqURL; // 请求路径 this.reqArgs = _args.reqArgs; // 请求参数表 this.reqTime = null; // 请求时间 this.ansType = "text"; // 回复类型 this.ansTime = null; // 回复时间 this.ansCode = 0; // 回复错误码 this.ansData = null; // 回复内容 this.listener = _args.cb; // 监听回调 this.crypto = _args.crypto; // 是否需要加密 return true; }, // 销毁 uninitialize: function uninitialize() { // USPER return this._super(); }, // 发送请求 send: function send() { // 时间记录 this.reqTime = cc.sys.now(); // 发送 var xhr = this._genXHR(); var text = nx.dt.enjson(this.reqArgs); xhr.send(this.crypto ? Crypto.encrypt(text) : text); nx.debug("[WebTask][%s] ===> %s 参数:%s", this.reqType, this.reqURL, text); }, // 上传 upload: function upload(_data) { // 时间记录 this.reqTime = cc.sys.now(); // 参数序列化 var args = ""; if (nx.dt.objNEmpty(this.reqArgs)) { args = "?"; for (var key in this.reqArgs) { if (args != "?") args += "&"; args += key + "=" + this.reqArgs[key]; } } this.reqURL += args; this.reqType = "POST"; // 发送 var xhr = this._genXHR(); // xhr.send( encodeURI(_data) ); xhr.send(_data); nx.debug("[WebTask][%s] ===> %s 参数:%s", this.reqType, this.reqURL); }, // 结果回调 responsed: function responsed(_code, _params) { this.ansCode = _code; this.ansTime = cc.sys.now(); var usedms = this.ansTime - this.reqTime; // Web网路级错误 if (_code != ERR.OK) { nx.error("[WebTask]发生错误: code:%s reason:%s", _code, _params || "none"); this.ansData = {}; this.manager._onTaskCallback(this); return; } // 解密 try { nx.debug("[WebTask][%s][%dms] <=== %s 参数:%s", this.reqType, usedms, this.reqURL, _params); var data = "ok"; if (nx.dt.strNEmpty(_params)) { if (this.crypto) { _params = Crypto.decrypt(_params); } data = nx.dt.dejson(_params); } this.ansData = data; } catch (e) { nx.error("[WebTask][%s] <=== %s 参数解密失败!", this.reqType, this.reqURL); this.ansData = {}; } this.manager._onTaskCallback(this); }, // 创建XHR _genXHR: function _genXHR(_ctype) { var self = this; var xhr = new XMLHttpRequest(); xhr.open(this.reqType, this.reqURL, true); xhr.setRequestHeader("Content-Type", _ctype || "text/plain"); // xhr.setRequestHeader( "TK", encodeURI( this.manager.token || "" ) ); xhr.responseType = this.ansType; xhr.timeout = TimeOut; // 错误处理 xhr.onerror = function (_err) { self.responsed(ERR.NET.WebError, _err ? _err.message : "unknown"); }; // 超时处理 xhr.ontimeout = function () { self.responsed(ERR.TimeOut); }; // 终止处理 xhr.onabort = function () { self.responsed(ERR.NET.Abort); }; // 请求完成处理 xhr.onreadystatechange = function () { if (!xhr || xhr.readyState != 4 || xhr.status == 0) { return; } if (xhr.status < 200 || xhr.status >= 400) { self.responsed(xhr.status == 404 ? ERR.NET.URLInvalid : ERR.Unkown); return; } // Token更新 // let token = xhr.getResponseHeader( "TK" ); // if( nx.dt.strNEmpty( token ) ) { // self.manager.token = token; // } // 内容解析 var resp = xhr.responseText == "text" ? xhr.responseText : xhr.response; self.responsed(ERR.OK, resp); xhr = null; }; return xhr; } }); module.exports = NxWebTask; cc._RF.pop();