mm_expand 2.3.4 → 2.3.6
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/lib/array.js +36 -36
- package/lib/base.js +15 -15
- package/lib/date.js +9 -8
- package/lib/errors.js +99 -99
- package/lib/event.js +45 -45
- package/lib/eventer.js +166 -165
- package/lib/file.js +127 -15
- package/lib/global.js +51 -53
- package/lib/lang.js +10 -10
- package/lib/logger.js +2 -2
- package/lib/number.js +10 -10
- package/lib/object.js +72 -75
- package/lib/req.js +42 -42
- package/lib/ret.js +247 -248
- package/lib/string.js +87 -93
- package/lib/timer.js +15 -15
- package/lib/validator.js +11 -11
- package/package.json +6 -6
package/lib/ret.js
CHANGED
|
@@ -5,52 +5,52 @@ const { Base } = require('./base');
|
|
|
5
5
|
* @class Ret
|
|
6
6
|
*/
|
|
7
7
|
class Ret extends Base {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
+
* @class
|
|
50
|
+
*/
|
|
51
|
+
constructor(config) {
|
|
52
|
+
super(config);
|
|
53
|
+
}
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
/**
|
|
@@ -59,125 +59,128 @@ class Ret extends Base {
|
|
|
59
59
|
* @returns {string} 错误提示
|
|
60
60
|
*/
|
|
61
61
|
Ret.prototype._getMessage = function (code) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
var message;
|
|
63
|
+
if ($.error) {
|
|
64
|
+
message = $.error.get(code);
|
|
65
|
+
}
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
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
77
|
|
|
78
78
|
/**
|
|
79
79
|
* 生成JSON-RPC 2.0响应格式
|
|
80
|
-
* @param {
|
|
81
|
-
* @param {
|
|
82
|
-
* @param {
|
|
83
|
-
* @param {
|
|
80
|
+
* @param {object} result 返回的结果
|
|
81
|
+
* @param {object} error 返回的错误信息
|
|
82
|
+
* @param {object.code} error.code 返回的错误码
|
|
83
|
+
* @param {object.message} error.message 返回的错误提示
|
|
84
84
|
* @param {string} id 消息ID
|
|
85
85
|
* @param {boolean} diy 是否自定义错误提示
|
|
86
|
-
* @returns {
|
|
86
|
+
* @returns {object} JSON-RPC 2.0响应格式
|
|
87
87
|
*/
|
|
88
|
-
Ret.prototype._toJsonRPC = function (result, error, id,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
88
|
+
Ret.prototype._toJsonRPC = function (result, error, id, _diy = true) {
|
|
89
|
+
const ret = {
|
|
90
|
+
};
|
|
91
|
+
if (id) {
|
|
92
|
+
ret.id = id;
|
|
93
|
+
}
|
|
94
94
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
+
var 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
|
+
if (this.config.fs) {
|
|
118
|
+
ret.fs = this.config.fs;
|
|
119
|
+
}
|
|
120
|
+
return ret;
|
|
121
|
+
};
|
|
119
122
|
|
|
120
123
|
/**
|
|
121
124
|
* 生成RESTful响应格式
|
|
122
125
|
* @param {*} result 返回的结果
|
|
123
|
-
* @param {
|
|
124
|
-
* @param {
|
|
125
|
-
* @param {
|
|
126
|
+
* @param {object} error 返回的错误信息
|
|
127
|
+
* @param {object.code} error.code 返回的错误码
|
|
128
|
+
* @param {object.message} error.message 返回的错误提示
|
|
126
129
|
* @param {string} id 消息ID
|
|
127
130
|
* @param {boolean} diy 是否自定义错误提示
|
|
128
|
-
* @returns {
|
|
131
|
+
* @returns {object} RESTful响应格式
|
|
129
132
|
*/
|
|
130
|
-
Ret.prototype._toRESTful = function (result, error, id,
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
133
|
+
Ret.prototype._toRESTful = function (result, error, id, _diy = true) {
|
|
134
|
+
let ret = {};
|
|
135
|
+
if (id) {
|
|
136
|
+
ret.id = id;
|
|
137
|
+
}
|
|
138
|
+
var code;
|
|
139
|
+
var msg;
|
|
140
|
+
if (result) {
|
|
141
|
+
code = this.config.success_code;
|
|
142
|
+
msg = this._getMessage(this.config.success_code);
|
|
143
|
+
}
|
|
144
|
+
else if (error) {
|
|
145
|
+
code = error.code || this.config.error_code;
|
|
146
|
+
var diy = _diy || this.config.message_diy;
|
|
147
|
+
if (!diy) {
|
|
148
|
+
msg = this._getMessage(code) || error.message || error.msg;
|
|
149
|
+
}
|
|
150
|
+
else if (!error.message && !error.msg) {
|
|
151
|
+
msg = this._getMessage(code);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
msg = error.message || error.msg || this._getMessage(code);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
code = this.config.error_code;
|
|
159
|
+
msg = this._getMessage(code);
|
|
160
|
+
}
|
|
161
|
+
ret.code = code;
|
|
162
|
+
ret.msg = msg;
|
|
163
|
+
if (result) {
|
|
164
|
+
ret.data = result;
|
|
165
|
+
}
|
|
166
|
+
return ret;
|
|
164
167
|
};
|
|
165
168
|
|
|
166
169
|
/**
|
|
167
170
|
* 生成响应体
|
|
168
|
-
* @param {
|
|
169
|
-
* @param {
|
|
171
|
+
* @param {object} result 返回的结果
|
|
172
|
+
* @param {object} error 返回的错误信息
|
|
170
173
|
* @param {string} id 消息ID
|
|
171
174
|
* @param {boolean} diy 是否自定义错误提示
|
|
172
|
-
* @returns {
|
|
175
|
+
* @returns {object} 响应格式
|
|
173
176
|
*/
|
|
174
177
|
Ret.prototype.body = function (result, error, id, diy = true) {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
178
|
+
if (this.config.format === 'json-rpc') {
|
|
179
|
+
return this._toJsonRPC(result, error, id, diy);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
return this._toRESTful(result, error, id, diy);
|
|
183
|
+
}
|
|
181
184
|
};
|
|
182
185
|
|
|
183
186
|
/**
|
|
@@ -186,14 +189,14 @@ Ret.prototype.body = function (result, error, id, diy = true) {
|
|
|
186
189
|
* @param {string} message 错误信息
|
|
187
190
|
* @param {string} id 消息ID
|
|
188
191
|
* @param {boolean} diy 是否自定义错误提示
|
|
189
|
-
* @returns {
|
|
192
|
+
* @returns {object} 错误响应格式
|
|
190
193
|
*/
|
|
191
194
|
Ret.prototype.error = function (code, message, id, diy = true) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
195
|
+
const error = {
|
|
196
|
+
code,
|
|
197
|
+
message
|
|
198
|
+
};
|
|
199
|
+
return this.body(null, error, id, diy);
|
|
197
200
|
};
|
|
198
201
|
|
|
199
202
|
/**
|
|
@@ -202,32 +205,30 @@ Ret.prototype.error = function (code, message, id, diy = true) {
|
|
|
202
205
|
* @param {number} count 查询结果数
|
|
203
206
|
* @param {string} id 消息ID
|
|
204
207
|
* @param {boolean} diy 是否自定义错误提示
|
|
205
|
-
* @returns {
|
|
208
|
+
* @returns {object} 列表响应格式
|
|
206
209
|
*/
|
|
207
210
|
Ret.prototype.list = function (list, count, id, diy = true) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
return this.body(result, null, id, diy);
|
|
211
|
+
var list_val = list || [];
|
|
212
|
+
const result = {
|
|
213
|
+
list: list_val
|
|
214
|
+
};
|
|
215
|
+
if (count !== undefined) {
|
|
216
|
+
result.count = count;
|
|
217
|
+
}
|
|
218
|
+
return this.body(result, null, id, diy);
|
|
218
219
|
};
|
|
219
220
|
|
|
220
221
|
/**
|
|
221
222
|
* 生成对象响应
|
|
222
|
-
* @param {
|
|
223
|
+
* @param {object} obj 返回对象
|
|
223
224
|
* @param {string} id 消息ID
|
|
224
|
-
* @returns {
|
|
225
|
+
* @returns {object} 对象响应格式
|
|
225
226
|
*/
|
|
226
227
|
Ret.prototype.obj = function (obj, id) {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
228
|
+
const result = {
|
|
229
|
+
obj: obj
|
|
230
|
+
};
|
|
231
|
+
return this.body(result, null, id);
|
|
231
232
|
};
|
|
232
233
|
|
|
233
234
|
/**
|
|
@@ -235,112 +236,110 @@ Ret.prototype.obj = function (obj, id) {
|
|
|
235
236
|
* @param {boolean} bl 布尔结果
|
|
236
237
|
* @param {string} tip 提示信息
|
|
237
238
|
* @param {string} id 消息ID
|
|
238
|
-
* @returns {
|
|
239
|
+
* @returns {object} 布尔响应格式
|
|
239
240
|
*/
|
|
240
|
-
Ret.prototype.bl = function (bl,
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
tip
|
|
247
|
-
}, null, id);
|
|
241
|
+
Ret.prototype.bl = function (bl, _tip, id) {
|
|
242
|
+
var tip = _tip || (bl ? this.config.success_message : this.config.error_message);
|
|
243
|
+
return this.body({
|
|
244
|
+
bl,
|
|
245
|
+
tip
|
|
246
|
+
}, null, id);
|
|
248
247
|
};
|
|
249
248
|
|
|
250
249
|
/**
|
|
251
250
|
* 添加数据项到响应结果
|
|
252
|
-
* @param {
|
|
251
|
+
* @param {object} res 响应对象
|
|
253
252
|
* @param {string} key 数据项键
|
|
254
253
|
* @param {*} value 数据项值
|
|
255
254
|
*/
|
|
256
255
|
Ret.prototype.add = function (res, key, value) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
}
|
|
256
|
+
if (this.config.format === 'json-rpc') {
|
|
257
|
+
if (!res.result) {
|
|
258
|
+
res.result = {};
|
|
259
|
+
}
|
|
260
|
+
res.result[key] = value;
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
if (!res.data) {
|
|
264
|
+
res.data = {};
|
|
265
|
+
}
|
|
266
|
+
res.data[key] = value;
|
|
267
|
+
}
|
|
268
|
+
};
|
|
270
269
|
|
|
271
270
|
/**
|
|
272
271
|
* 解析响应
|
|
273
|
-
* @param {
|
|
274
|
-
* @returns {
|
|
272
|
+
* @param {object} res 响应对象
|
|
273
|
+
* @returns {object} 解析后的格式
|
|
275
274
|
*/
|
|
276
275
|
Ret.prototype.parse = function (res) {
|
|
277
|
-
|
|
278
|
-
|
|
276
|
+
let error;
|
|
277
|
+
let result;
|
|
279
278
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
279
|
+
if (res.error) {
|
|
280
|
+
error = res.error;
|
|
281
|
+
}
|
|
282
|
+
else if (res.result) {
|
|
283
|
+
result = res.result;
|
|
284
|
+
} else if (res.data) {
|
|
285
|
+
result = res.data;
|
|
286
|
+
}
|
|
287
|
+
else if (res.msg || res.message) {
|
|
288
|
+
error = {
|
|
289
|
+
code: res.code || this.config.error_code,
|
|
290
|
+
message: res.msg || res.message || this.config.error_message
|
|
291
|
+
};
|
|
292
|
+
}
|
|
294
293
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
}
|
|
294
|
+
const ret = {};
|
|
295
|
+
if (this.config.format === 'json-rpc') {
|
|
296
|
+
if (error) {
|
|
297
|
+
ret.error = error;
|
|
298
|
+
}
|
|
299
|
+
else if (result) {
|
|
300
|
+
ret.result = result;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
if (error) {
|
|
305
|
+
ret.code = error.code || this.config.error_code;
|
|
306
|
+
ret.msg = error.message || this.config.error_message;
|
|
307
|
+
ret.data = null;
|
|
308
|
+
}
|
|
309
|
+
else if (result) {
|
|
310
|
+
ret.code = this.config.success_code;
|
|
311
|
+
ret.msg = this._getMessage(this.config.success_code);
|
|
312
|
+
ret.data = result;
|
|
313
|
+
}
|
|
314
|
+
else {
|
|
315
|
+
ret.code = this.config.success_code;
|
|
316
|
+
ret.msg = this._getMessage(this.config.success_code);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return ret;
|
|
320
|
+
};
|
|
322
321
|
|
|
323
322
|
/**
|
|
324
323
|
* 设置响应格式
|
|
325
324
|
* @param {string} format 响应格式
|
|
326
325
|
*/
|
|
327
326
|
Ret.prototype.setFormat = function (format) {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
327
|
+
switch (format?.toLowerCase()) {
|
|
328
|
+
case 'json':
|
|
329
|
+
case 'json-rpc':
|
|
330
|
+
this.config.format = 'json-rpc';
|
|
331
|
+
break;
|
|
332
|
+
case 'json3':
|
|
333
|
+
case 'json-rpc3':
|
|
334
|
+
this.config.format = 'json-rpc3';
|
|
335
|
+
break;
|
|
336
|
+
default:
|
|
337
|
+
this.config.format = 'restful';
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
return this;
|
|
342
341
|
};
|
|
343
342
|
|
|
344
343
|
module.exports = {
|
|
345
|
-
|
|
344
|
+
Ret
|
|
346
345
|
};
|