mm_ret 1.4.1 → 1.4.2

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 CHANGED
@@ -3,266 +3,24 @@
3
3
  * @author <a href="http://qww.elins.cn">邱文武</a>
4
4
  * @version 1.0
5
5
  */
6
- require('mm_expand');
7
-
8
- /**
9
- * @class 错误代码
10
- */
11
- class Error {
12
- /**
13
- * @description 构造函数
14
- * @constructor
15
- */
16
- constructor() {
17
- this.type = [
18
- {
19
- code: -32700,
20
- message: "not well formed"
21
- },
22
- {
23
- code: -32701,
24
- message: "unsupported encoding"
25
- },
26
- {
27
- code: -32702,
28
- message: "invalid character for encoding"
29
- },
30
- {
31
- code: -32600,
32
- message: "invalid json-rpc. not conforming to spec"
33
- },
34
- {
35
- code: -32601,
36
- message: "requested method not found"
37
- },
38
- {
39
- code: -32602,
40
- message: "invalid method parameters"
41
- },
42
- {
43
- code: -32603,
44
- message: "internal json-rpc error"
45
- },
46
- {
47
- code: -32500,
48
- message: "application error"
49
- },
50
- {
51
- code: -32400,
52
- message: "system error"
53
- },
54
- {
55
- code: -32300,
56
- message: "transport error"
57
- },
58
- {
59
- code: 10000,
60
- message: "业务逻辑脚本错误"
61
- },
62
- {
63
- code: 30000,
64
- message: "身份验证失败"
65
- },
66
- {
67
- code: 40000,
68
- message: "数据库执行错误"
69
- },
70
- {
71
- code: 50000,
72
- message: "服务端执行错误"
73
- },
74
- {
75
- code: 70000,
76
- message: "参数不正确"
77
- }
78
- ];
79
- }
80
- }
81
-
82
- /**
83
- * @description 获取错误
84
- * @param {String|Number} keyword 关键词 或 错误码
85
- */
86
- Error.prototype.get = function(keyword) {
87
- var error = null;
88
- if (typeof(keyword) === "string") {
89
- for (var i = 0; i < this.type.length; i++) {
90
- var o = this.type[i];
91
- if (o.code === keyword) {
92
- error = o;
93
- break;
94
- }
95
- }
96
- } else {
97
- for (var i = 0; i < this.type.length; i++) {
98
- var o = this.type[i];
99
- if (o.message.has(keyword)) {
100
- error = o;
101
- break;
102
- }
103
- }
104
- }
105
- return error;
106
- };
107
-
108
- /**
109
- * @description json-rpc2.0响应函数
110
- * @class
111
- */
112
- class Ret {
113
- /**
114
- * @description 构造函数
115
- * @constructor
116
- */
117
- constructor() {
118
-
119
- }
6
+ const { Error } = require('./lib/error.js');
7
+ const { Req } = require('./lib/req.js');
8
+ const { Ret } = require('./lib/ret.js');
9
+
10
+ if (global.$) {
11
+ if (!$.error) {
12
+ $.error = new Error();
13
+ }
14
+ if (!$.req) {
15
+ $.req = new Req();
16
+ }
17
+ if (!$.ret) {
18
+ $.ret = new Ret();
19
+ }
120
20
  }
121
21
 
122
- /**
123
- * @description 取结果
124
- * @param {Object} result 返回的结果
125
- * @param {Object} error 返回的错误信息
126
- * @param {String} id 消息ID, 用于消息队列或多线程时
127
- * @return {Object} json-rpc 请求结果
128
- */
129
- Ret.prototype.body = function(result, error, id) {
130
- var ret = {};
131
- if (id) {
132
- ret.id = id;
133
- }
134
- if (result) {
135
- ret.result = result;
136
- } else {
137
- if (error) {
138
- if (!error.code) {
139
- error.code = 10000;
140
- }
141
- if (!error.message) {
142
- error.message = "业务逻辑错误";
143
- }
144
- } else {
145
- error = {
146
- code: 10000,
147
- message: "业务逻辑错误"
148
- }
149
- }
150
- ret.error = error;
151
- }
152
- return ret;
153
- };
154
-
155
- /**
156
- * @description 取结果
157
- * @param {Number} code 返回的错误码
158
- * @param {String} message 返回的错误提示
159
- * @param {String} id 消息ID, 用于消息队列或多线程时
160
- * @return {Object} json-rpc 请求结果
161
- */
162
- Ret.prototype.error = function(code, message, id) {
163
- var error = {
164
- code: code,
165
- message: message
166
- };
167
- return this.body(null, error, id);
168
- };
169
-
170
- /**
171
- * @description 取列表结果
172
- * @param {Array} list 返回列表
173
- * @param {Number} count 查询结果数
174
- * @param {String} id 消息ID, 用于消息队列或多线程时
175
- * @return {Object} json-rpc 请求结果
176
- */
177
- Ret.prototype.list = function(list, count, id) {
178
- if (!list) {
179
- list = [];
180
- }
181
- var result = {
182
- list: list
183
- };
184
- if (count !== undefined) {
185
- result.count = count;
186
- }
187
- return this.body(result, null, id);
188
- };
189
-
190
- /**
191
- * @description 取对象结果
192
- * @param {Object} obj 返回对象
193
- * @param {Object} id 消息ID, 用于消息队列或多线程时
194
- * @return {Object} json-rpc 请求结果
195
- */
196
- Ret.prototype.obj = function(obj, id) {
197
- return this.body({
198
- obj: obj
199
- }, null, id);
200
- };
201
-
202
- /**
203
- * @description 取布尔型结果
204
- * @param {Boolean} bl 取布尔结果
205
- * @param {String} tip 返回对象
206
- * @param {String} id 操作结果提示
207
- */
208
- Ret.prototype.bl = function(bl, tip, id) {
209
- if (!tip) {
210
- tip = bl ? "成功" : "失败";
211
- }
212
- return this.body({
213
- bl: bl,
214
- tip: tip
215
- }, null, id);
216
- };
217
-
218
- $.ret = new Ret();
219
-
220
- /**
221
- * @description json-rpc2.0请求函数
222
- * @class
223
- */
224
- class Req {
225
- /**
226
- * 构造函数
227
- * @constructor
228
- * @param {String} scope 作用域
229
- */
230
- constructor(scope) {
231
- // 作用域
232
- this.scope = scope ? scope : "sys";
233
-
234
- // 模板集合
235
- this.tpl = {};
236
-
237
- // 方法集合
238
- this.methods = {};
239
- }
240
- }
241
-
242
- /**
243
- * 发送请求
244
- * @param {String} method 方法
245
- * @param {Object} params 参数
246
- * @return {Object} 发送的json格式
247
- */
248
- Req.prototype.send = function(method, params) {
249
- var tpl = this.tpl[method];
250
- var pm;
251
- if(tpl){
252
- pm = Object.assign({}, tpl, params);
253
- }
254
- else {
255
- pm = params
256
- }
257
- return {
258
- id: this.scope + new Date().stamp(),
259
- method: method,
260
- params: pm
261
- };
262
- };
263
-
264
- $.req = new Req();
265
-
266
- exports.Error = Error;
267
- exports.Ret = Ret;
268
- exports.Req = Req;
22
+ module.exports = {
23
+ Error,
24
+ Ret,
25
+ Req
26
+ }
package/lib/error.js ADDED
@@ -0,0 +1,163 @@
1
+ /**
2
+ * @file 错误代码管理类
3
+ * @class Error
4
+ */
5
+ class Error {
6
+ /**
7
+ * @description 构造函数
8
+ * @constructor
9
+ * @param {Object} config 配置项
10
+ */
11
+ constructor(config) {
12
+ /**
13
+ * @description 错误码配置项
14
+ */
15
+ this._config = {
16
+ lang: "zh-CN"
17
+ };
18
+ this.setConfig(config);
19
+ /**
20
+ * @description 错误码语言包
21
+ */
22
+ this._langs = {
23
+ "zh-CN": {
24
+ "10000": "业务逻辑错误",
25
+ "0": "成功",
26
+ "‌100": "继续",
27
+ "‌101": "切换协议‌",
28
+ "‌200": "确定",
29
+ "‌201": "已创建",
30
+ "‌202": "已接受",
31
+ "‌204": "无内容",
32
+ "‌301": "已永久移动",
33
+ "‌302": "已找到",
34
+ "‌303": "见其他",
35
+ "‌304": "未修改",
36
+ "‌307": "临时重定向‌",
37
+ "‌400": "错误请求",
38
+ "‌401": "未授权",
39
+ "‌403": "禁止访问",
40
+ "‌404": "未找到",
41
+ "‌405": "方法不允许",
42
+ "‌408": "请求超时",
43
+ "‌413": "Payload Too Large",
44
+ "‌415": "不支持的媒体类型",
45
+ "‌429": "请求过多",
46
+ "‌500": "内部服务器错误",
47
+ "‌501": "未实现",
48
+ "‌502": "错误网关",
49
+ "‌503": "服务不可用",
50
+ "‌504": "网关超时",
51
+ "-32700": "格式错误",
52
+ "-32701": "不支持的编码",
53
+ "-32702": "编码中包含无效字符",
54
+ "-32600": "无效的json-rpc. 不符合规范",
55
+ "-32601": "请求的方法未找到",
56
+ "-32602": "无效的方法参数",
57
+ "-32603": "内部json-rpc错误",
58
+ "-32500": "应用错误",
59
+ "-32400": "系统错误",
60
+ "-32300": "传输错误",
61
+ "30000": "身份验证失败",
62
+ "40000": "数据库执行错误",
63
+ "50000": "服务端执行错误",
64
+ "70000": "参数不正确"
65
+ },
66
+ "en-US": {
67
+ "10000": "business logic error",
68
+ "0": "success",
69
+ "‌100": "Continue",
70
+ "‌101": "Switching Protocols‌",
71
+ "‌200": "OK",
72
+ "‌201": "Created",
73
+ "‌202": "Accepted",
74
+ "‌204": "No Content",
75
+ "‌301": "Moved Permanently",
76
+ "‌302": "Found",
77
+ "‌303": "See Other",
78
+ "‌304": "Not Modified‌",
79
+ "‌307": "Temporary Redirect‌",
80
+ "‌400": "Bad Request",
81
+ "‌401": "Unauthorized",
82
+ "‌403": "Forbidden",
83
+ "‌404": "Not Found",
84
+ "‌405": "Method Not Allowed",
85
+ "‌408": "Request Timeout",
86
+ "‌413": "Payload Too Large",
87
+ "‌415": "Unsupported Media Type",
88
+ "‌429": "Too Many Requests‌",
89
+ "‌500": "Internal Server Error",
90
+ "‌501": "Not Implemented",
91
+ "‌502": "Bad Gateway",
92
+ "‌503": "Service Unavailable",
93
+ "‌504": "Gateway Timeout",
94
+ "-32700": "not well formed",
95
+ "-32701": "unsupported encoding",
96
+ "-32702": "invalid character for encoding",
97
+ "-32600": "invalid json-rpc. not conforming to spec",
98
+ "-32601": "requested method not found",
99
+ "-32602": "invalid method parameters",
100
+ "-32603": "internal json-rpc error",
101
+ "-32500": "application error",
102
+ "-32400": "system error",
103
+ "-32300": "transport error",
104
+ "30000": "authentication failed",
105
+ "40000": "database execution error",
106
+ "50000": "server execution error",
107
+ "70000": "parameter is incorrect"
108
+ }
109
+ };
110
+ }
111
+ }
112
+
113
+ /**
114
+ * @description 设置配置项
115
+ * @param {Object} config 配置项
116
+ */
117
+ Error.prototype.setConfig = function (config) {
118
+ Object.assign(this._config, config);
119
+ }
120
+
121
+ /**
122
+ * @description 根据错误码获取错误信息
123
+ * @param {string|number} code 错误码
124
+ * @returns {string|null} 错误信息
125
+ */
126
+ Error.prototype.get = function (code) {
127
+ const lang = this._config.lang;
128
+ const lang_pack = this._langs[lang];
129
+
130
+ if (lang_pack && lang_pack[code]) {
131
+ return lang_pack[code];
132
+ }
133
+
134
+ // 如果当前语言包没有,尝试英文包
135
+ if (lang !== "en-US" && this._langs["en-US"][code]) {
136
+ return this._langs["en-US"][code];
137
+ }
138
+
139
+ return null;
140
+ };
141
+
142
+ /**
143
+ * @description 设置语言
144
+ * @param {string} lang 语言代码
145
+ */
146
+ Error.prototype.setLang = function (lang) {
147
+ this._config.lang = lang;
148
+ };
149
+
150
+ /**
151
+ * @description 获取当前语言
152
+ * @returns {string} 当前语言代码
153
+ */
154
+ Error.prototype.getLang = function () {
155
+ return this._config.lang;
156
+ };
157
+
158
+ /**
159
+ * @description 导出错误管理类
160
+ */
161
+ module.exports = {
162
+ Error
163
+ }
package/lib/req.js ADDED
@@ -0,0 +1,241 @@
1
+ /**
2
+ * @file 请求管理类
3
+ * @class Req
4
+ */
5
+ class Req {
6
+ /**
7
+ * @description 构造函数
8
+ * @constructor
9
+ * @param {Object} config 配置项
10
+ */
11
+ constructor(config) {
12
+ // 配置项
13
+ this._config = {
14
+ /**
15
+ * @description 作用域
16
+ * @type {string}
17
+ * @default "sys"
18
+ */
19
+ scope: "sys",
20
+ /**
21
+ * @description 响应格式
22
+ * @type {string} "json-rpc" | "json-rpc3" | "restful"
23
+ * @default "json-rpc"
24
+ */
25
+ format: "json-rpc"
26
+ };
27
+ this.setConfig(config);
28
+
29
+ // 方法集合
30
+ this._methods = {};
31
+ }
32
+ }
33
+
34
+ /**
35
+ * @description 设置配置项
36
+ * @param {Object} config 配置项
37
+ */
38
+ Req.prototype.setConfig = function (config) {
39
+ Object.assign(this._config, config);
40
+ this._config.format = this._config.format?.toLowerCase() || "json-rpc";
41
+ }
42
+
43
+ /**
44
+ * @description 生成新的ID
45
+ * @returns {string} 新的ID
46
+ */
47
+ Req.prototype.newId = function () {
48
+ return this._config.scope + Date.now();
49
+ }
50
+
51
+ /**
52
+ * @description 发送请求
53
+ * @param {string} method 方法名
54
+ * @param {...any} args 参数
55
+ * @returns {Object} 发送的JSON格式
56
+ */
57
+ Req.prototype.send = function (method, ...args) {
58
+ if (this._methods[method]) {
59
+ return this._methods[method](...args);
60
+ }
61
+ else if (this._config.format === "json-rpc") {
62
+ return this._sendJsonRPC(method, args);
63
+ }
64
+ else if (this._config.format === "json-rpc3") {
65
+ return this._sendJsonRPC3(method, ...args);
66
+ }
67
+ else {
68
+ return this._sendRESTful(method, ...args);
69
+ }
70
+ };
71
+
72
+ /**
73
+ * @description 发送JSON-RPC 2.0请求
74
+ * @param {string} method 方法名
75
+ * @param {Array} params 参数数组
76
+ * @param {string} [id] 请求ID
77
+ * @returns {Object} JSON-RPC 2.0格式
78
+ */
79
+ Req.prototype._sendJsonRPC = function (method, params, id) {
80
+ return {
81
+ id: id || this.newId(),
82
+ method,
83
+ params
84
+ };
85
+ }
86
+
87
+ /**
88
+ * @description 发送JSON-RPC 3.0请求
89
+ * @param {string} method 方法名
90
+ * @param {Object} [query] 查询参数
91
+ * @param {Object} [body] 请求体
92
+ * @param {Array} [meta] 元数据数组
93
+ * @param {string} [id] 请求ID
94
+ * @returns {Object} JSON-RPC 3.0格式
95
+ */
96
+ Req.prototype._sendJsonRPC3 = function (method, query, body, meta, id) {
97
+ return {
98
+ id: id || this.newId(),
99
+ method,
100
+ params: {
101
+ query: query || {},
102
+ body: body || {},
103
+ meta: meta || []
104
+ }
105
+ };
106
+ }
107
+
108
+ /**
109
+ * @description 发送RESTful请求
110
+ * @param {string} method 方法名
111
+ * @param {Object} [query] 查询参数
112
+ * @param {Object} [body] 请求体
113
+ * @param {Array} [meta] 元数据数组
114
+ * @param {string} [id] 请求ID
115
+ * @returns {Object} RESTful格式
116
+ */
117
+ Req.prototype._sendRESTful = function (method, query, body, meta, id) {
118
+ return {
119
+ id: id || this.newId(),
120
+ method,
121
+ query: query || {},
122
+ body: body || {},
123
+ meta: meta || []
124
+ };
125
+ }
126
+
127
+ /**
128
+ * @description 解析请求
129
+ * @param {Object} req 请求对象
130
+ * @returns {Object} 解析后的对象
131
+ */
132
+ Req.prototype.parse = function (req) {
133
+ const id = req.id;
134
+ const method = req.method;
135
+ let params = [];
136
+
137
+ if (req.params) {
138
+ if (Array.isArray(req.params)) {
139
+ params = req.params;
140
+ }
141
+ else {
142
+ if (typeof req.params === "object") {
143
+ let ctx = Object.assign({}, req.params);
144
+ if (ctx.query || ctx.body) {
145
+ params.push(ctx.query || null);
146
+ params.push(ctx.body || null);
147
+ delete ctx.query;
148
+ delete ctx.body;
149
+ for (const key in ctx) {
150
+ params.push(ctx[key]);
151
+ }
152
+ }
153
+ else {
154
+ params = [
155
+ req.params
156
+ ];
157
+ }
158
+ }
159
+ else {
160
+ params = [req.params];
161
+ }
162
+ }
163
+ }
164
+ else if (req.query || req.body) {
165
+ params.push(req.query || null);
166
+ params.push(req.body || null);
167
+ if (req.meta) {
168
+ params.push(req.meta || null);
169
+ }
170
+ }
171
+ if (this._config.format === "json-rpc") {
172
+ if (req.params) {
173
+ params = req.params;
174
+ }
175
+ }
176
+ else if (this._config.format === "json-rpc3") {
177
+ var pm = {};
178
+ pm.query = params.length > 0 ? params[0] || null : null;
179
+ pm.body = params.length > 1 ? params[1] || null : null;
180
+ pm.meta = params.length > 2 ? params[2] || null : null;
181
+ params = pm;
182
+ }
183
+ else if (this._config.format === "restful") {
184
+ var ret = {
185
+ id,
186
+ method,
187
+ query: params.length > 0 ? params[0] || null : null,
188
+ body: params.length > 1 ? params[1] || null : null
189
+ }
190
+ if (params.length > 2) {
191
+ ret.meta = params[2];
192
+ }
193
+ return ret;
194
+ }
195
+ return {
196
+ id,
197
+ method,
198
+ params
199
+ };
200
+ }
201
+
202
+ /**
203
+ * @description 注册自定义方法
204
+ * @param {string} method 方法名
205
+ * @param {Function} handler 处理方法
206
+ */
207
+ Req.prototype.registerMethod = function (method, handler) {
208
+ this._methods[method] = handler;
209
+ };
210
+
211
+ /**
212
+ * @description 注销自定义方法
213
+ * @param {string} method 方法名
214
+ */
215
+ Req.prototype.unregisterMethod = function (method) {
216
+ delete this._methods[method];
217
+ };
218
+
219
+ /**
220
+ * @description 设置请求格式
221
+ * @param {string} format 请求格式
222
+ */
223
+ Req.prototype.setFormat = function (format) {
224
+ switch (format?.toLowerCase()) {
225
+ case "json":
226
+ case "json-rpc":
227
+ this._config.format = "json-rpc";
228
+ break;
229
+ case "json3":
230
+ case "json-rpc3":
231
+ this._config.format = "json-rpc3";
232
+ break;
233
+ default:
234
+ this._config.format = "restful";
235
+ break;
236
+ }
237
+ }
238
+
239
+ module.exports = {
240
+ Req
241
+ };
package/lib/ret.js ADDED
@@ -0,0 +1,352 @@
1
+ require('mm_expand');
2
+ /**
3
+ * @file 响应管理类
4
+ * @class Ret
5
+ */
6
+ class Ret {
7
+ /**
8
+ * @description 构造函数
9
+ * @param {Object} config 配置项
10
+ * @constructor
11
+ */
12
+ constructor(config) {
13
+ this._config = {
14
+ /**
15
+ * @description 响应格式
16
+ * @type {string} "json-rpc" | "restful"
17
+ * @default "json-rpc"
18
+ */
19
+ format: "json-rpc",
20
+ /**
21
+ * @description 默认错误码
22
+ * @type {number}
23
+ * @default 10000
24
+ */
25
+ error_code: 10000,
26
+ /**
27
+ * @description 默认错误提示
28
+ * @type {string}
29
+ * @default "错误"
30
+ */
31
+ error_message: "错误",
32
+ /**
33
+ * @description 默认成功码
34
+ * @type {number}
35
+ * @default 0
36
+ */
37
+ success_code: 0,
38
+ /**
39
+ * @description 默认成功提示
40
+ * @type {string}
41
+ * @default "成功"
42
+ */
43
+ success_message: "成功",
44
+ /**
45
+ * @description
46
+ * @type {boolean}
47
+ * @default false
48
+ */
49
+ message_diy: false
50
+ };
51
+
52
+ this.setConfig(config);
53
+ }
54
+ }
55
+
56
+ /**
57
+ * @description 设置配置项
58
+ * @param {Object} config 配置项
59
+ */
60
+ Ret.prototype.setConfig = function (config) {
61
+ Object.assign(this._config, config);
62
+ }
63
+
64
+ /**
65
+ * @description 获取错误提示
66
+ * @param {number} code 错误码
67
+ * @returns {string} 错误提示
68
+ */
69
+ Ret.prototype._getMessage = function (code) {
70
+ var message;
71
+ if ($.error) {
72
+ message = $.error.get(code);
73
+ }
74
+
75
+ if (!message) {
76
+ if (code === this._config.success_code) {
77
+ message = this._config.success_message;
78
+ }
79
+ else {
80
+ message = this._config.error_message;
81
+ }
82
+ }
83
+ return message;
84
+ }
85
+
86
+ /**
87
+ * @description 生成JSON-RPC 2.0响应格式
88
+ * @param {Object} result 返回的结果
89
+ * @param {Object} error 返回的错误信息
90
+ * @param {Object.code} error.code 返回的错误码
91
+ * @param {Object.message} error.message 返回的错误提示
92
+ * @param {string} id 消息ID
93
+ * @param {boolean} diy 是否自定义错误提示
94
+ * @returns {Object} JSON-RPC 2.0响应格式
95
+ */
96
+ Ret.prototype._toJsonRPC = function (result, error, id, diy = false) {
97
+ const ret = {
98
+ id
99
+ };
100
+
101
+ if (result) {
102
+ ret.result = result;
103
+ }
104
+ else if (error) {
105
+ if (!error.code) {
106
+ error.code = this._config.error_code;
107
+ }
108
+ diy = diy || this._config.message_diy;
109
+ if (!diy) {
110
+ error.message = this._getMessage(error.code) || error.message;
111
+ }
112
+ else if (!error.message) {
113
+ error.message = this._getMessage(error.code);
114
+ }
115
+ ret.error = error;
116
+ }
117
+ else {
118
+ ret.error = {
119
+ code: this._config.error_code,
120
+ message: this._getMessage(this._config.error_code)
121
+ };
122
+ }
123
+ return ret;
124
+ }
125
+
126
+ /**
127
+ * @description 生成RESTful响应格式
128
+ * @param {*} result 返回的结果
129
+ * @param {Object} error 返回的错误信息
130
+ * @param {Object.code} error.code 返回的错误码
131
+ * @param {Object.message} error.message 返回的错误提示
132
+ * @param {string} id 消息ID
133
+ * @param {boolean} diy 是否自定义错误提示
134
+ * @returns {Object} RESTful响应格式
135
+ */
136
+ Ret.prototype._toRESTful = function (result, error, id, diy = false) {
137
+ var code;
138
+ var msg;
139
+ if (result) {
140
+ code = this._config.success_code;
141
+ msg = this._getMessage(this._config.success_code);
142
+ }
143
+ else if (error) {
144
+ code = error.code || this._config.error_code;
145
+ diy = diy || this._config.message_diy;
146
+ if (!diy) {
147
+ msg = this._getMessage(code) || error.message || error.msg;
148
+ }
149
+ else if (!error.message && !error.msg) {
150
+ msg = this._getMessage(code);
151
+ }
152
+ else {
153
+ msg = error.message || error.msg || this._getMessage(code);
154
+ }
155
+ }
156
+ else {
157
+ code = this._config.error_code;
158
+ msg = this._getMessage(code);
159
+ }
160
+ return {
161
+ id,
162
+ code,
163
+ msg,
164
+ data: result
165
+ };
166
+ }
167
+
168
+ /**
169
+ * @description 生成响应体
170
+ * @param {Object} result 返回的结果
171
+ * @param {Object} error 返回的错误信息
172
+ * @param {string} id 消息ID
173
+ * @param {boolean} diy 是否自定义错误提示
174
+ * @returns {Object} 响应格式
175
+ */
176
+ Ret.prototype.body = function (result, error, id, diy = false) {
177
+ if (this._config.format === "json-rpc") {
178
+ return this._toJsonRPC(result, error, id, diy);
179
+ }
180
+ else {
181
+ return this._toRESTful(result, error, id, diy);
182
+ }
183
+ };
184
+
185
+ /**
186
+ * @description 生成错误响应
187
+ * @param {number} code 错误码
188
+ * @param {string} message 错误信息
189
+ * @param {string} id 消息ID
190
+ * @param {boolean} diy 是否自定义错误提示
191
+ * @returns {Object} 错误响应格式
192
+ */
193
+ Ret.prototype.error = function (code, message, id, diy = false) {
194
+ const error = {
195
+ code,
196
+ message
197
+ };
198
+ return this.body(null, error, id, diy);
199
+ };
200
+
201
+ /**
202
+ * @description 生成列表响应
203
+ * @param {Array} list 返回列表
204
+ * @param {number} count 查询结果数
205
+ * @param {string} id 消息ID
206
+ * @param {boolean} diy 是否自定义错误提示
207
+ * @returns {Object} 列表响应格式
208
+ */
209
+ Ret.prototype.list = function (list, count, id, diy = false) {
210
+ if (!list) {
211
+ list = [];
212
+ }
213
+ const result = {
214
+ list: list
215
+ };
216
+ if (count !== undefined) {
217
+ result.count = count;
218
+ }
219
+ return this.body(result, null, id, diy);
220
+ };
221
+
222
+ /**
223
+ * @description 生成对象响应
224
+ * @param {Object} obj 返回对象
225
+ * @param {string} id 消息ID
226
+ * @returns {Object} 对象响应格式
227
+ */
228
+ Ret.prototype.obj = function (obj, id) {
229
+ return this.body(obj, null, id);
230
+ };
231
+
232
+ /**
233
+ * @description 生成布尔型响应
234
+ * @param {boolean} bl 布尔结果
235
+ * @param {string} tip 提示信息
236
+ * @param {string} id 消息ID
237
+ * @returns {Object} 布尔响应格式
238
+ */
239
+ Ret.prototype.bl = function (bl, tip, id) {
240
+ if (!tip) {
241
+ tip = bl ? this._config.success_message : this._config.error_message;
242
+ }
243
+ return this.body({
244
+ bl,
245
+ tip
246
+ }, null, id);
247
+ };
248
+
249
+ /**
250
+ * @description 添加数据项到响应结果
251
+ * @param {Object} res 响应对象
252
+ * @param {string} key 数据项键
253
+ * @param {*} value 数据项值
254
+ */
255
+ Ret.prototype.add = function (res, key, value) {
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
+ }
269
+
270
+ /**
271
+ * @description 解析响应
272
+ * @param {Object} res 响应对象
273
+ * @returns {Object} 解析后的格式
274
+ */
275
+ Ret.prototype.parse = function (res) {
276
+ let error;
277
+ let result;
278
+
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
+ }
293
+
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
+ }
321
+
322
+ /**
323
+ * @description 获取当前配置
324
+ * @returns {Object} 配置对象
325
+ */
326
+ Ret.prototype.getConfig = function () {
327
+ return this._config;
328
+ };
329
+
330
+ /**
331
+ * @description 设置响应格式
332
+ * @param {string} format 响应格式
333
+ */
334
+ Ret.prototype.setFormat = function (format) {
335
+ switch (format?.toLowerCase()) {
336
+ case "json":
337
+ case "json-rpc":
338
+ this._config.format = "json-rpc";
339
+ break;
340
+ case "json3":
341
+ case "json-rpc3":
342
+ this._config.format = "json-rpc3";
343
+ break;
344
+ default:
345
+ this._config.format = "restful";
346
+ break;
347
+ }
348
+ }
349
+
350
+ module.exports = {
351
+ Ret
352
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_ret",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "这是超级美眉http请求结果输出类函数模块,用于输出json-rpc2.0标准结果",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -30,6 +30,6 @@
30
30
  },
31
31
  "homepage": "https://gitee.com/qiuwenwu91/mm_ret#readme",
32
32
  "dependencies": {
33
- "mm_expand": "^1.8.3"
33
+ "mm_expand": "^1.9.6"
34
34
  }
35
35
  }
package/test.js CHANGED
@@ -1,36 +1,93 @@
1
- require('./index.js');
1
+ const { Ret, Req } = require('./index.js');
2
2
 
3
3
  /* 调用示例 */
4
- async function test() {
4
+ async function retTest(out) {
5
+ if (!out) {
6
+ out = $.ret;
7
+ }
5
8
  var body;
6
- body = $.ret.error(10000, '错误');
7
- console.log(JSON.stringify(body));
8
-
9
+ body = $.ret.error(10000, "这是一个错误", null, true);
10
+ $.log.debug("封装:", JSON.stringify(body));
11
+ $.log.debug("解析结果:", out.parse(body));
12
+
9
13
  body = $.ret.body("这是一个结果");
10
- console.log(JSON.stringify(body));
11
-
12
- body = $.ret.obj({ "name": "张三", age: 18});
13
- console.log(JSON.stringify(body));
14
-
15
- body = $.ret.list([{ "name": "张三", age: 18}, { "name": "李四", age: 24}]);
16
- console.log(JSON.stringify(body));
17
-
14
+ $.log.debug("封装:", JSON.stringify(body));
15
+ $.log.debug("解析结果:", out.parse(body));
16
+
17
+ body = $.ret.obj({ "name": "张三", age: 18 });
18
+ $.log.debug("封装:", JSON.stringify(body));
19
+ $.log.debug("解析结果:", out.parse(body));
20
+
21
+ body = $.ret.list([{ "name": "张三", age: 18 }, { "name": "李四", age: 24 }]);
22
+ $.log.debug("封装:", JSON.stringify(body));
23
+ $.log.debug("解析结果:", out.parse(body));
24
+
18
25
  body = $.ret.bl(true, "修改成功");
19
- console.log(JSON.stringify(body));
26
+ $.log.debug("封装:", JSON.stringify(body));
27
+ $.log.debug("解析结果:", out.parse(body));
20
28
  }
21
29
 
22
- test();
23
-
24
- async function test2() {
25
- var body = $.req.send('test', "你好吗");
26
- console.log(body);
27
-
28
- $.req.tpl.message = {
29
- "to_user": "",
30
- "from_user": "",
31
- "content": ""
32
- };
33
- body = $.req.send('message', { content: "你好吗?", "media": { "title": "牛" } });
34
- console.log(body);
30
+ function retDemo() {
31
+ $.log.debug("=== 测试JSON-RPC风格 进出 === ");
32
+ retTest();
33
+
34
+ $.log.debug("=== 测试RESTful风格 进出 === ");
35
+ $.ret.setFormat("restful");
36
+ retTest();
37
+
38
+ $.log.debug("=== 测试RESTful风格 进,JSON-RPC风格 出 === ");
39
+ retTest(new Ret());
40
+
41
+ $.log.debug("=== 测试JSON-RPC风格 进,RESTful风格 === ");
42
+ $.ret.setFormat("json-rpc");
43
+ retTest(new Ret({ format: "restful" }));
35
44
  }
36
- test2();
45
+
46
+ retDemo();
47
+
48
+ /* 调用示例 */
49
+ async function reqTest(out) {
50
+ if (!out) {
51
+ out = $.req;
52
+ }
53
+ var body;
54
+ body = $.req.send("test_method", "param1", "param2", "param3");
55
+ $.log.debug("封装:", JSON.stringify(body));
56
+ $.log.debug("解析结果:", out.parse(body));
57
+
58
+ body = $.req.send("set", {
59
+ user_id: 1
60
+ }, {
61
+ "name": "张三",
62
+ "age": 18
63
+ }, {
64
+ "token": "xxxxx"
65
+ });
66
+ $.log.debug("封装:", JSON.stringify(body));
67
+ $.log.debug("解析结果:", out.parse(body));
68
+ }
69
+
70
+ function reqDemo() {
71
+ $.log.debug("=== 测试JSON-RPC风格请求 进出 === ");
72
+ reqTest();
73
+
74
+ $.log.debug("=== 测试JSON-RPC3风格请求 进出 === ");
75
+ $.req.setFormat("json-rpc3");
76
+ reqTest();
77
+
78
+ $.log.debug("=== 测试RESTful风格请求 进出 === ");
79
+ $.req.setFormat("restful");
80
+ reqTest();
81
+
82
+ $.log.debug("=== 测试RESTful风格请求 进,JSON-RPC风格请求 出 === ");
83
+ reqTest(new Req());
84
+
85
+ $.log.debug("=== 测试RESTful风格请求 进,JSON-RPC3风格请求 出 === ");
86
+ reqTest(new Req({ format: "json-rpc3" }));
87
+
88
+ $.log.debug("=== 测试JSON-RPC风格请求 进,RESTful风格请求 出 === ");
89
+ $.req.setFormat("json-rpc");
90
+ reqTest(new Req({ format: "restful" }));
91
+ }
92
+
93
+ reqDemo();