mm_expand 2.1.2 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +9 -0
- package/lib/base.js +45 -27
- package/lib/errors.js +160 -0
- package/lib/event.js +67 -45
- package/lib/eventer.js +39 -13
- package/lib/file.js +29 -24
- package/lib/lang.js +52 -68
- package/lib/logger.js +35 -18
- package/lib/req.js +268 -0
- package/lib/ret.js +338 -0
- package/lib/timer.js +1 -4
- package/lib/validator.js +35 -46
- package/package.json +3 -2
package/lib/req.js
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
const { Base } = require('./base');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file 请求管理类
|
|
5
|
+
* @class Req
|
|
6
|
+
*/
|
|
7
|
+
class Req extends Base {
|
|
8
|
+
static config = {
|
|
9
|
+
/**
|
|
10
|
+
* 作用域
|
|
11
|
+
* @type {string}
|
|
12
|
+
* @default "sys"
|
|
13
|
+
*/
|
|
14
|
+
scope: "sys",
|
|
15
|
+
/**
|
|
16
|
+
* 响应格式
|
|
17
|
+
* @type {string} "json-rpc" | "json-rpc3" | "restful"
|
|
18
|
+
* @default "json-rpc"
|
|
19
|
+
*/
|
|
20
|
+
format: "json-rpc"
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* 构造函数
|
|
24
|
+
* @constructor
|
|
25
|
+
* @param {Object} config 配置项
|
|
26
|
+
*/
|
|
27
|
+
constructor(config) {
|
|
28
|
+
super(config);
|
|
29
|
+
// 方法集合
|
|
30
|
+
this._methods = {};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 生成新的ID
|
|
36
|
+
* @returns {string} 新的ID
|
|
37
|
+
*/
|
|
38
|
+
Req.prototype.genId = function () {
|
|
39
|
+
return this._config.scope + Date.now();
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 发送请求
|
|
44
|
+
* @param {string} method 方法名
|
|
45
|
+
* @param {...any} args 参数
|
|
46
|
+
* @returns {Object} 发送的JSON格式
|
|
47
|
+
*/
|
|
48
|
+
Req.prototype.send = function (method, ...args) {
|
|
49
|
+
if (this._methods[method]) {
|
|
50
|
+
return this._methods[method](...args);
|
|
51
|
+
}
|
|
52
|
+
else if (this._config.format === "json-rpc") {
|
|
53
|
+
return this._sendJsonRPC(method, args);
|
|
54
|
+
}
|
|
55
|
+
else if (this._config.format === "json-rpc3") {
|
|
56
|
+
return this._sendJsonRPC3(method, ...args);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
return this._sendRESTful(method, ...args);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 发送JSON-RPC 2.0请求
|
|
65
|
+
* @param {string} method 方法名
|
|
66
|
+
* @param {Array} params 参数数组
|
|
67
|
+
* @param {string} [id] 请求ID
|
|
68
|
+
* @returns {Object} JSON-RPC 2.0格式
|
|
69
|
+
*/
|
|
70
|
+
Req.prototype._sendJsonRPC = function (method, params, id) {
|
|
71
|
+
return {
|
|
72
|
+
id: id || this.genId(),
|
|
73
|
+
method,
|
|
74
|
+
params
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 发送JSON-RPC 3.0请求
|
|
80
|
+
* @param {string} method 方法名
|
|
81
|
+
* @param {Object} [query] 查询参数
|
|
82
|
+
* @param {Object} [body] 请求体
|
|
83
|
+
* @param {Array} [meta] 元数据数组
|
|
84
|
+
* @param {string} [id] 请求ID
|
|
85
|
+
* @returns {Object} JSON-RPC 3.0格式
|
|
86
|
+
*/
|
|
87
|
+
Req.prototype._sendJsonRPC3 = function (method, query, body, meta, id) {
|
|
88
|
+
return {
|
|
89
|
+
id: id || this.genId(),
|
|
90
|
+
method,
|
|
91
|
+
params: {
|
|
92
|
+
query: query || {},
|
|
93
|
+
body: body || {},
|
|
94
|
+
meta: meta || []
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 发送RESTful请求
|
|
101
|
+
* @param {string} method 方法名
|
|
102
|
+
* @param {Object} [query] 查询参数
|
|
103
|
+
* @param {Object} [body] 请求体
|
|
104
|
+
* @param {Array} [meta] 元数据数组
|
|
105
|
+
* @param {string} [id] 请求ID
|
|
106
|
+
* @returns {Object} RESTful格式
|
|
107
|
+
*/
|
|
108
|
+
Req.prototype._sendRESTful = function (method, query, body, meta, id) {
|
|
109
|
+
return {
|
|
110
|
+
id: id || this.genId(),
|
|
111
|
+
method,
|
|
112
|
+
query: query || {},
|
|
113
|
+
body: body || {},
|
|
114
|
+
meta: meta || []
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 解析请求
|
|
120
|
+
* @param {Object} req 请求对象
|
|
121
|
+
* @returns {Object} 解析后的对象
|
|
122
|
+
*/
|
|
123
|
+
Req.prototype.parse = function (req) {
|
|
124
|
+
const id = req.id;
|
|
125
|
+
const method = req.method;
|
|
126
|
+
let params = [];
|
|
127
|
+
|
|
128
|
+
if (req.params) {
|
|
129
|
+
if (Array.isArray(req.params)) {
|
|
130
|
+
params = req.params;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
if (typeof req.params === "object") {
|
|
134
|
+
let ctx = Object.assign({}, req.params);
|
|
135
|
+
if (ctx.query || ctx.body) {
|
|
136
|
+
params.push(ctx.query || null);
|
|
137
|
+
params.push(ctx.body || null);
|
|
138
|
+
delete ctx.query;
|
|
139
|
+
delete ctx.body;
|
|
140
|
+
for (const key in ctx) {
|
|
141
|
+
params.push(ctx[key]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
params = [
|
|
146
|
+
req.params
|
|
147
|
+
];
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
params = [req.params];
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
else if (req.query || req.body) {
|
|
156
|
+
params.push(req.query || null);
|
|
157
|
+
params.push(req.body || null);
|
|
158
|
+
if (req.meta) {
|
|
159
|
+
params.push(req.meta || null);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (this._config.format === "json-rpc") {
|
|
163
|
+
if (req.params) {
|
|
164
|
+
params = req.params;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else if (this._config.format === "json-rpc3") {
|
|
168
|
+
var pm = {};
|
|
169
|
+
pm.query = params.length > 0 ? params[0] || null : null;
|
|
170
|
+
pm.body = params.length > 1 ? params[1] || null : null;
|
|
171
|
+
pm.meta = params.length > 2 ? params[2] || null : null;
|
|
172
|
+
params = pm;
|
|
173
|
+
}
|
|
174
|
+
else if (this._config.format === "restful") {
|
|
175
|
+
var ret = {
|
|
176
|
+
id,
|
|
177
|
+
method,
|
|
178
|
+
query: params.length > 0 ? params[0] || null : null,
|
|
179
|
+
body: params.length > 1 ? params[1] || null : null
|
|
180
|
+
}
|
|
181
|
+
if (params.length > 2) {
|
|
182
|
+
ret.meta = params[2];
|
|
183
|
+
}
|
|
184
|
+
return ret;
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
id,
|
|
188
|
+
method,
|
|
189
|
+
params
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 设置自定义方法
|
|
195
|
+
* @param {string} method 方法名
|
|
196
|
+
* @param {Function} func 处理方法
|
|
197
|
+
*/
|
|
198
|
+
Req.prototype.setMethod = function (method, func) {
|
|
199
|
+
if (typeof func !== "function") {
|
|
200
|
+
throw new Error("func必须是一个函数类型");
|
|
201
|
+
}
|
|
202
|
+
this._methods[method] = func;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 删除自定义方法
|
|
207
|
+
* @param {string} method 方法名
|
|
208
|
+
*/
|
|
209
|
+
Req.prototype.delMethod = function (method) {
|
|
210
|
+
delete this._methods[method];
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* 获取自定义方法
|
|
215
|
+
* @param {string} method 方法名
|
|
216
|
+
* @returns {Function} 处理方法
|
|
217
|
+
*/
|
|
218
|
+
Req.prototype.getMethod = function (method) {
|
|
219
|
+
return this._methods[method];
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 获取所有自定义方法
|
|
224
|
+
* @returns {Object} 所有自定义方法
|
|
225
|
+
*/
|
|
226
|
+
Req.prototype.getMethods = function () {
|
|
227
|
+
return this._methods;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 运行自定义方法
|
|
232
|
+
* @param {string} method 方法名
|
|
233
|
+
* @param {Array} params 参数数组
|
|
234
|
+
* @returns {Object} 处理结果
|
|
235
|
+
*/
|
|
236
|
+
Req.prototype.run = function (method, params) {
|
|
237
|
+
const func = this._methods[method];
|
|
238
|
+
if (func) {
|
|
239
|
+
return func.apply(this, params);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
throw new Error("方法不存在");
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 设置请求格式
|
|
248
|
+
* @param {string} format 请求格式
|
|
249
|
+
*/
|
|
250
|
+
Req.prototype.setFormat = function (format) {
|
|
251
|
+
switch (format?.toLowerCase()) {
|
|
252
|
+
case "json":
|
|
253
|
+
case "json-rpc":
|
|
254
|
+
this._config.format = "json-rpc";
|
|
255
|
+
break;
|
|
256
|
+
case "json3":
|
|
257
|
+
case "json-rpc3":
|
|
258
|
+
this._config.format = "json-rpc3";
|
|
259
|
+
break;
|
|
260
|
+
default:
|
|
261
|
+
this._config.format = "restful";
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
module.exports = {
|
|
267
|
+
Req
|
|
268
|
+
};
|
package/lib/ret.js
ADDED
|
@@ -0,0 +1,338 @@
|
|
|
1
|
+
const { Base } = require('./base');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @file 响应管理类
|
|
5
|
+
* @class Ret
|
|
6
|
+
*/
|
|
7
|
+
class Ret extends Base {
|
|
8
|
+
static config = {
|
|
9
|
+
/**
|
|
10
|
+
* 响应格式
|
|
11
|
+
* @type {string} "json-rpc" | "restful"
|
|
12
|
+
* @default "json-rpc"
|
|
13
|
+
*/
|
|
14
|
+
format: "json-rpc",
|
|
15
|
+
/**
|
|
16
|
+
* 默认错误码
|
|
17
|
+
* @type {number}
|
|
18
|
+
* @default 10000
|
|
19
|
+
*/
|
|
20
|
+
error_code: 10000,
|
|
21
|
+
/**
|
|
22
|
+
* 默认错误提示
|
|
23
|
+
* @type {string}
|
|
24
|
+
* @default "错误"
|
|
25
|
+
*/
|
|
26
|
+
error_message: "错误",
|
|
27
|
+
/**
|
|
28
|
+
* 默认成功码
|
|
29
|
+
* @type {number}
|
|
30
|
+
* @default 0
|
|
31
|
+
*/
|
|
32
|
+
success_code: 0,
|
|
33
|
+
/**
|
|
34
|
+
* 默认成功提示
|
|
35
|
+
* @type {string}
|
|
36
|
+
* @default "成功"
|
|
37
|
+
*/
|
|
38
|
+
success_message: "成功",
|
|
39
|
+
/**
|
|
40
|
+
* 是否自定义错误提示
|
|
41
|
+
* @type {boolean}
|
|
42
|
+
* @default false
|
|
43
|
+
*/
|
|
44
|
+
message_diy: false
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* 构造函数
|
|
48
|
+
* @param {Object} config 配置项
|
|
49
|
+
* @constructor
|
|
50
|
+
*/
|
|
51
|
+
constructor(config) {
|
|
52
|
+
super(config);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 获取错误提示
|
|
58
|
+
* @param {number} code 错误码
|
|
59
|
+
* @returns {string} 错误提示
|
|
60
|
+
*/
|
|
61
|
+
Ret.prototype._getMessage = function (code) {
|
|
62
|
+
var message;
|
|
63
|
+
if ($.error) {
|
|
64
|
+
message = $.error.get(code);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!message) {
|
|
68
|
+
if (code === this.config.success_code) {
|
|
69
|
+
message = this.config.success_message;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
message = this.config.error_message;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return message;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 生成JSON-RPC 2.0响应格式
|
|
80
|
+
* @param {Object} result 返回的结果
|
|
81
|
+
* @param {Object} error 返回的错误信息
|
|
82
|
+
* @param {Object.code} error.code 返回的错误码
|
|
83
|
+
* @param {Object.message} error.message 返回的错误提示
|
|
84
|
+
* @param {string} id 消息ID
|
|
85
|
+
* @param {boolean} diy 是否自定义错误提示
|
|
86
|
+
* @returns {Object} JSON-RPC 2.0响应格式
|
|
87
|
+
*/
|
|
88
|
+
Ret.prototype._toJsonRPC = function (result, error, id, diy = true) {
|
|
89
|
+
const ret = {
|
|
90
|
+
};
|
|
91
|
+
if (id) {
|
|
92
|
+
ret.id = id;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (result) {
|
|
96
|
+
ret.result = result;
|
|
97
|
+
}
|
|
98
|
+
else if (error) {
|
|
99
|
+
if (!error.code) {
|
|
100
|
+
error.code = this.config.error_code;
|
|
101
|
+
}
|
|
102
|
+
diy = diy || this.config.message_diy;
|
|
103
|
+
if (!diy) {
|
|
104
|
+
error.message = this._getMessage(error.code) || error.message;
|
|
105
|
+
}
|
|
106
|
+
else if (!error.message) {
|
|
107
|
+
error.message = this._getMessage(error.code);
|
|
108
|
+
}
|
|
109
|
+
ret.error = error;
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
ret.error = {
|
|
113
|
+
code: this.config.error_code,
|
|
114
|
+
message: this._getMessage(this.config.error_code)
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return ret;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 生成RESTful响应格式
|
|
122
|
+
* @param {*} result 返回的结果
|
|
123
|
+
* @param {Object} error 返回的错误信息
|
|
124
|
+
* @param {Object.code} error.code 返回的错误码
|
|
125
|
+
* @param {Object.message} error.message 返回的错误提示
|
|
126
|
+
* @param {string} id 消息ID
|
|
127
|
+
* @param {boolean} diy 是否自定义错误提示
|
|
128
|
+
* @returns {Object} RESTful响应格式
|
|
129
|
+
*/
|
|
130
|
+
Ret.prototype._toRESTful = function (result, error, id, diy = true) {
|
|
131
|
+
var code;
|
|
132
|
+
var msg;
|
|
133
|
+
if (result) {
|
|
134
|
+
code = this.config.success_code;
|
|
135
|
+
msg = this._getMessage(this.config.success_code);
|
|
136
|
+
}
|
|
137
|
+
else if (error) {
|
|
138
|
+
code = error.code || this.config.error_code;
|
|
139
|
+
diy = diy || this.config.message_diy;
|
|
140
|
+
if (!diy) {
|
|
141
|
+
msg = this._getMessage(code) || error.message || error.msg;
|
|
142
|
+
}
|
|
143
|
+
else if (!error.message && !error.msg) {
|
|
144
|
+
msg = this._getMessage(code);
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
msg = error.message || error.msg || this._getMessage(code);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
code = this.config.error_code;
|
|
152
|
+
msg = this._getMessage(code);
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
id,
|
|
156
|
+
code,
|
|
157
|
+
msg,
|
|
158
|
+
data: result
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 生成响应体
|
|
164
|
+
* @param {Object} result 返回的结果
|
|
165
|
+
* @param {Object} error 返回的错误信息
|
|
166
|
+
* @param {string} id 消息ID
|
|
167
|
+
* @param {boolean} diy 是否自定义错误提示
|
|
168
|
+
* @returns {Object} 响应格式
|
|
169
|
+
*/
|
|
170
|
+
Ret.prototype.body = function (result, error, id, diy = true) {
|
|
171
|
+
if (this.config.format === "json-rpc") {
|
|
172
|
+
return this._toJsonRPC(result, error, id, diy);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
return this._toRESTful(result, error, id, diy);
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* 生成错误响应
|
|
181
|
+
* @param {number} code 错误码
|
|
182
|
+
* @param {string} message 错误信息
|
|
183
|
+
* @param {string} id 消息ID
|
|
184
|
+
* @param {boolean} diy 是否自定义错误提示
|
|
185
|
+
* @returns {Object} 错误响应格式
|
|
186
|
+
*/
|
|
187
|
+
Ret.prototype.error = function (code, message, id, diy = true) {
|
|
188
|
+
const error = {
|
|
189
|
+
code,
|
|
190
|
+
message
|
|
191
|
+
};
|
|
192
|
+
return this.body(null, error, id, diy);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* 生成列表响应
|
|
197
|
+
* @param {Array} list 返回列表
|
|
198
|
+
* @param {number} count 查询结果数
|
|
199
|
+
* @param {string} id 消息ID
|
|
200
|
+
* @param {boolean} diy 是否自定义错误提示
|
|
201
|
+
* @returns {Object} 列表响应格式
|
|
202
|
+
*/
|
|
203
|
+
Ret.prototype.list = function (list, count, id, diy = true) {
|
|
204
|
+
if (!list) {
|
|
205
|
+
list = [];
|
|
206
|
+
}
|
|
207
|
+
const result = {
|
|
208
|
+
list: list
|
|
209
|
+
};
|
|
210
|
+
if (count !== undefined) {
|
|
211
|
+
result.count = count;
|
|
212
|
+
}
|
|
213
|
+
return this.body(result, null, id, diy);
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 生成对象响应
|
|
218
|
+
* @param {Object} obj 返回对象
|
|
219
|
+
* @param {string} id 消息ID
|
|
220
|
+
* @returns {Object} 对象响应格式
|
|
221
|
+
*/
|
|
222
|
+
Ret.prototype.obj = function (obj, id) {
|
|
223
|
+
return this.body(obj, null, id);
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* 生成布尔型响应
|
|
228
|
+
* @param {boolean} bl 布尔结果
|
|
229
|
+
* @param {string} tip 提示信息
|
|
230
|
+
* @param {string} id 消息ID
|
|
231
|
+
* @returns {Object} 布尔响应格式
|
|
232
|
+
*/
|
|
233
|
+
Ret.prototype.bl = function (bl, tip, id) {
|
|
234
|
+
if (!tip) {
|
|
235
|
+
tip = bl ? this.config.success_message : this.config.error_message;
|
|
236
|
+
}
|
|
237
|
+
return this.body({
|
|
238
|
+
bl,
|
|
239
|
+
tip
|
|
240
|
+
}, null, id);
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* 添加数据项到响应结果
|
|
245
|
+
* @param {Object} res 响应对象
|
|
246
|
+
* @param {string} key 数据项键
|
|
247
|
+
* @param {*} value 数据项值
|
|
248
|
+
*/
|
|
249
|
+
Ret.prototype.add = function (res, key, value) {
|
|
250
|
+
if (this.config.format === "json-rpc") {
|
|
251
|
+
if (!res.result) {
|
|
252
|
+
res.result = {};
|
|
253
|
+
}
|
|
254
|
+
res.result[key] = value;
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
if (!res.data) {
|
|
258
|
+
res.data = {};
|
|
259
|
+
}
|
|
260
|
+
res.data[key] = value;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* 解析响应
|
|
266
|
+
* @param {Object} res 响应对象
|
|
267
|
+
* @returns {Object} 解析后的格式
|
|
268
|
+
*/
|
|
269
|
+
Ret.prototype.parse = function (res) {
|
|
270
|
+
let error;
|
|
271
|
+
let result;
|
|
272
|
+
|
|
273
|
+
if (res.error) {
|
|
274
|
+
error = res.error;
|
|
275
|
+
}
|
|
276
|
+
else if (res.result) {
|
|
277
|
+
result = res.result;
|
|
278
|
+
} else if (res.data) {
|
|
279
|
+
result = res.data;
|
|
280
|
+
}
|
|
281
|
+
else if (res.msg || res.message) {
|
|
282
|
+
error = {
|
|
283
|
+
code: res.code || this.config.error_code,
|
|
284
|
+
message: res.msg || res.message || this.config.error_message
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const ret = {};
|
|
289
|
+
if (this.config.format === "json-rpc") {
|
|
290
|
+
if (error) {
|
|
291
|
+
ret.error = error;
|
|
292
|
+
}
|
|
293
|
+
else if (result) {
|
|
294
|
+
ret.result = result;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
if (error) {
|
|
299
|
+
ret.code = error.code || this.config.error_code;
|
|
300
|
+
ret.msg = error.message || this.config.error_message;
|
|
301
|
+
ret.data = null;
|
|
302
|
+
}
|
|
303
|
+
else if (result) {
|
|
304
|
+
ret.code = this.config.success_code;
|
|
305
|
+
ret.msg = this._getMessage(this.config.success_code);
|
|
306
|
+
ret.data = result;
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
ret.code = this.config.success_code;
|
|
310
|
+
ret.msg = this._getMessage(this.config.success_code);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return ret;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* 设置响应格式
|
|
318
|
+
* @param {string} format 响应格式
|
|
319
|
+
*/
|
|
320
|
+
Ret.prototype.setFormat = function (format) {
|
|
321
|
+
switch (format?.toLowerCase()) {
|
|
322
|
+
case "json":
|
|
323
|
+
case "json-rpc":
|
|
324
|
+
this.config.format = "json-rpc";
|
|
325
|
+
break;
|
|
326
|
+
case "json3":
|
|
327
|
+
case "json-rpc3":
|
|
328
|
+
this.config.format = "json-rpc3";
|
|
329
|
+
break;
|
|
330
|
+
default:
|
|
331
|
+
this.config.format = "restful";
|
|
332
|
+
break;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
module.exports = {
|
|
337
|
+
Ret
|
|
338
|
+
};
|