188 lines
4.3 KiB
JavaScript
188 lines
4.3 KiB
JavaScript
"use strict";
|
|
cc._RF.push(module, 'a926fAFDjpGVpYs25nO3OZn', 'nx.web.gate');
|
|
// Scripts/nx/web/nx.web.gate.js
|
|
|
|
"use strict";
|
|
|
|
/*******************************************************************************
|
|
*
|
|
* Nx WEB通信
|
|
*
|
|
*
|
|
*
|
|
* 2021.12.10
|
|
******************************************************************************/
|
|
|
|
var ERR = require("error");
|
|
var NxObject = require("nx.object");
|
|
var NxWebManager = cc.Class({
|
|
"extends": NxObject,
|
|
name: "NxWebManager",
|
|
// 初始化
|
|
initialize: function initialize(_args) {
|
|
// USPER
|
|
if (!this._super(_args)) {
|
|
return false;
|
|
}
|
|
this.url = ""; // 服务器地址
|
|
this.token = ""; // 令牌
|
|
// this.taskDB = {}; // 任务表
|
|
|
|
// 全局服务
|
|
nx.web = this;
|
|
return true;
|
|
},
|
|
// 销毁
|
|
uninitialize: function uninitialize() {
|
|
this.clean();
|
|
nx.web = null;
|
|
|
|
// USPER
|
|
return this._super();
|
|
},
|
|
// 清理
|
|
clean: function clean() {
|
|
|
|
// for( let i in this.taskDB ) {
|
|
// nx.factory.remove( this.taskDB[i] );
|
|
// }
|
|
// this.taskDB = {};
|
|
},
|
|
// GET
|
|
get: function get(_router, _args, _cb, _crypto) {
|
|
// 判空
|
|
if (nx.dt.strEmpty(_router)) {
|
|
nx.dt.fnInvoke(_cb, nx.genError(ERR.NET.URLInvalid));
|
|
return;
|
|
}
|
|
|
|
// 请求
|
|
this._request("GET", _router, _args, _cb, _crypto);
|
|
},
|
|
// POST
|
|
post: function post(_router, _args, _cb, _crypto) {
|
|
// 判空
|
|
if (nx.dt.strEmpty(_router)) {
|
|
nx.dt.fnInvoke(_cb, nx.genError(ERR.NET.URLInvalid));
|
|
return;
|
|
}
|
|
|
|
// 请求
|
|
this._request("POST", _router, _args, _cb, _crypto);
|
|
},
|
|
// 上传
|
|
upload: function upload(_router, _args, _data, _cb) {
|
|
// 判空
|
|
if (nx.dt.strEmpty(_router)) {
|
|
nx.dt.fnInvoke(_cb, nx.genError(ERR.NET.URLInvalid));
|
|
return;
|
|
}
|
|
|
|
// 关键字
|
|
var key = this._genTaskKey("POST", _router);
|
|
// if( this.taskDB[key] ) {
|
|
// nx.warn( "[WEB]重复请求:", key );
|
|
// nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.RequestRepeat ) );
|
|
// return;
|
|
// }
|
|
|
|
// URL规范
|
|
var url = _router;
|
|
if (_router.indexOf('http') != 0) {
|
|
url = cc.path.join(this.url, _router);
|
|
}
|
|
|
|
// 创建新任务
|
|
var args = {
|
|
mgr: this,
|
|
key: key,
|
|
reqURL: url,
|
|
cb: _cb,
|
|
reqType: "POST",
|
|
reqArgs: _args,
|
|
crypto: false
|
|
};
|
|
var task = nx.factory.create("nx.web.task", args);
|
|
if (!task) {
|
|
nx.dt.fnInvoke(_cb, nx.genError(ERR.NET.CreateTaskFailed));
|
|
return;
|
|
}
|
|
|
|
// this.taskDB[key] = task;
|
|
task.upload(_data);
|
|
},
|
|
// 请求
|
|
_request: function _request(_type, _router, _args, _cb, _crypto) {
|
|
_cb = _cb || nx.dt.fnEmpty;
|
|
|
|
// 方式验证
|
|
if (_type != "GET" && _type != "POST") {
|
|
nx.dt.fnInvoke(_cb, nx.genError(ERR.NET.RequestTypeInvalid));
|
|
return;
|
|
}
|
|
|
|
// 参数验证
|
|
if (nx.dt.strEmpty(_router) || !nx.dt.objGood(_args)) {
|
|
nx.dt.fnInvoke(_cb, nx.genError(ERR.NET.RequestArgsInvalid));
|
|
return;
|
|
}
|
|
|
|
// 关键字
|
|
var key = this._genTaskKey(_type, _router);
|
|
// if( this.taskDB[key] ) {
|
|
// nx.warn( "[WEB]重复请求:", key );
|
|
// nx.dt.fnInvoke( _cb, nx.genError( ERR.NET.RequestRepeat ) );
|
|
// return;
|
|
// }
|
|
|
|
// URL规范
|
|
var url = _router;
|
|
if (_router.indexOf('http') != 0) {
|
|
url = cc.path.join(this.url, _router);
|
|
}
|
|
|
|
// 创建新任务
|
|
var args = {
|
|
mgr: this,
|
|
key: key,
|
|
reqURL: url,
|
|
cb: _cb,
|
|
reqType: _type,
|
|
reqArgs: _args,
|
|
crypto: _crypto || false
|
|
};
|
|
var task = nx.factory.create("nx.web.task", args);
|
|
if (!task) {
|
|
nx.dt.fnInvoke(_cb, nx.genError(ERR.NET.CreateTaskFailed));
|
|
return;
|
|
}
|
|
|
|
// this.taskDB[key] = task;
|
|
task.send();
|
|
},
|
|
// 任务执行回调
|
|
_onTaskCallback: function _onTaskCallback(_task) {
|
|
if (!_task) {
|
|
return;
|
|
}
|
|
|
|
// 任务移除
|
|
// delete this.taskDB[_task.key];
|
|
|
|
// 监听通告
|
|
if (nx.dt.fnGood(_task.listener)) {
|
|
_task.listener(_task.ansCode, _task.ansData);
|
|
}
|
|
|
|
// 任务销毁
|
|
nx.factory.remove(_task);
|
|
},
|
|
// 生成任务关键字
|
|
_genTaskKey: function _genTaskKey(_type, _router) {
|
|
var key = cc.js.formatStr("%s-%s", _type, _router);
|
|
return key.toLowerCase();
|
|
}
|
|
});
|
|
module.exports = NxWebManager;
|
|
|
|
cc._RF.pop(); |