mm_api 1.1.0 → 1.1.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/examples/README.md +39 -0
- package/examples/basic.js +37 -0
- package/examples/hello_api/api.json +18 -0
- package/examples/hello_api/index.js +14 -0
- package/examples/helper.js +28 -0
- package/examples/param_api/api.json +17 -0
- package/examples/param_api/index.js +14 -0
- package/examples/param_api/param.json +19 -0
- package/examples/param_api/run.js +38 -0
- package/examples/sql_api/api.json +16 -0
- package/examples/sql_api/index.js +49 -0
- package/examples/sql_api/run.js +33 -0
- package/index.js +2 -2
- package/lib/{drive.js → api.js} +74 -131
- package/lib/manager.js +73 -42
- package/lib/oauth.js +3 -56
- package/package.json +33 -27
- package/script.tpl.js +1 -1
- package/tests/api.test.js +167 -0
- package/tests/fixtures/echo.js +14 -0
- package/tests/fixtures/error.js +13 -0
- package/tests/fixtures/hello.js +13 -0
- package/tests/fixtures/param.json +19 -0
- package/tests/helper.js +57 -0
- package/tests/index.test.js +45 -0
- package/tests/manager.test.js +184 -0
- package/tests/oauth.test.js +107 -0
package/lib/{drive.js → api.js}
RENAMED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
const fs = require("fs");
|
|
2
2
|
const path = require("path");
|
|
3
|
-
const Item = require(
|
|
4
|
-
const { string, date } = require("mm_helper");
|
|
3
|
+
const Item = require('mm_machine').Mod;
|
|
4
|
+
const { string, date, object, file } = require("mm_helper");
|
|
5
|
+
const { getRet } = require('mm_ret');
|
|
5
6
|
const Oauth = require("./oauth.js"); // 是MM自带的身份验证机制,基于Oauth2.0,可以不使用
|
|
6
7
|
|
|
7
8
|
/**
|
|
@@ -17,79 +18,14 @@ function loadOptional(id) {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
const Param = loadOptional("mm_param/lib/
|
|
21
|
-
const Sql = loadOptional("mm_sql/lib/
|
|
21
|
+
const Param = loadOptional("mm_param/lib/param.js"); // 是MM自带的参数机制,可以不使用
|
|
22
|
+
const Sql = loadOptional("mm_sql/lib/sql.js"); // 是MM自带的sql机制,可以不使用
|
|
22
23
|
|
|
23
24
|
/** param 实例缓存(模块级,替代原全局 param 模块列表) */
|
|
24
25
|
const PARAM_CACHE = new Map();
|
|
25
26
|
/** sql 实例缓存(模块级,替代原全局 sql 模块列表) */
|
|
26
27
|
const SQL_CACHE = new Map();
|
|
27
28
|
|
|
28
|
-
/**
|
|
29
|
-
* 补全文件路径(替代原全局路径补全能力,支持相对/绝对路径)
|
|
30
|
-
* @param {string} file 文件路径
|
|
31
|
-
* @param {string} dir 基准目录
|
|
32
|
-
* @returns {string} 完整路径
|
|
33
|
-
*/
|
|
34
|
-
function _resolvePath(file, dir) {
|
|
35
|
-
if (!file) {
|
|
36
|
-
return process.cwd();
|
|
37
|
-
}
|
|
38
|
-
if (path.isAbsolute(file)) {
|
|
39
|
-
return file;
|
|
40
|
-
}
|
|
41
|
-
return path.resolve(dir || process.cwd(), file);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* 生成XML节点(_toXml 的内部递归辅助)
|
|
46
|
-
* @param {string} name 节点名
|
|
47
|
-
* @param {*} value 节点值
|
|
48
|
-
* @returns {string} XML片段字符串
|
|
49
|
-
*/
|
|
50
|
-
function _xmlNode(name, value) {
|
|
51
|
-
if (value && typeof value === "object") {
|
|
52
|
-
return "<" + name + ">" + _toXml(value) + "</" + name + ">";
|
|
53
|
-
}
|
|
54
|
-
var text = value === undefined || value === null ? "" : String(value);
|
|
55
|
-
return "<" + name + ">" + text + "</" + name + ">";
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 对象转XML字符串(替代原全局 toXml 的精简等价实现)
|
|
60
|
-
* @param {object} source 被转换的对象
|
|
61
|
-
* @returns {string} xml格式字符串
|
|
62
|
-
*/
|
|
63
|
-
function _toXml(source) {
|
|
64
|
-
var parts = [];
|
|
65
|
-
for (var key in source) {
|
|
66
|
-
var val = source[key];
|
|
67
|
-
if (Array.isArray(val)) {
|
|
68
|
-
for (var i = 0; i < val.length; i++) {
|
|
69
|
-
parts.push(_xmlNode(key, val[i]));
|
|
70
|
-
}
|
|
71
|
-
} else {
|
|
72
|
-
parts.push(_xmlNode(key, val));
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
return parts.join("");
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* 构建错误响应体(替代原全局 ret.error,默认 json-rpc 形态)
|
|
80
|
-
* @param {number} code 错误码
|
|
81
|
-
* @param {string} message 错误信息
|
|
82
|
-
* @returns {object} 形如 { error: { code, message } } 的响应体
|
|
83
|
-
*/
|
|
84
|
-
function _retError(code, message) {
|
|
85
|
-
return {
|
|
86
|
-
error: {
|
|
87
|
-
code,
|
|
88
|
-
message,
|
|
89
|
-
},
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
29
|
/**
|
|
94
30
|
* 去除 JSON 文本中的注释(支持 // 与整块注释,不处理字符串内部)
|
|
95
31
|
* @param {string} text 原始文本
|
|
@@ -169,7 +105,7 @@ function _readJsonConfig(file) {
|
|
|
169
105
|
* @augments {Item}
|
|
170
106
|
* @class
|
|
171
107
|
*/
|
|
172
|
-
class
|
|
108
|
+
class Api extends Item {
|
|
173
109
|
static config = {
|
|
174
110
|
// 应用名称 例如:demo_app
|
|
175
111
|
app: "server",
|
|
@@ -223,7 +159,7 @@ class Drive extends Item {
|
|
|
223
159
|
* @class
|
|
224
160
|
*/
|
|
225
161
|
constructor(config, parent) {
|
|
226
|
-
super({ ...
|
|
162
|
+
super({ ...Api.config, ...config });
|
|
227
163
|
|
|
228
164
|
// 父级管理器引用(用于缓存、插件与默认配置回退)
|
|
229
165
|
this.parent = parent || null;
|
|
@@ -239,7 +175,7 @@ class Drive extends Item {
|
|
|
239
175
|
/**
|
|
240
176
|
* 预设配置
|
|
241
177
|
*/
|
|
242
|
-
|
|
178
|
+
Api.prototype._preset = function () {
|
|
243
179
|
this.save_dir = "./static/file/";
|
|
244
180
|
this.url_path = "/file/";
|
|
245
181
|
|
|
@@ -260,7 +196,7 @@ Drive.prototype._preset = function () {
|
|
|
260
196
|
* 获取驱动目录(方案B:来自手动配置的 dir,替代旧基类 getDir)
|
|
261
197
|
* @returns {string} 驱动目录
|
|
262
198
|
*/
|
|
263
|
-
|
|
199
|
+
Api.prototype.getDir = function () {
|
|
264
200
|
return this.config.dir || __dirname;
|
|
265
201
|
};
|
|
266
202
|
|
|
@@ -270,7 +206,7 @@ Drive.prototype.getDir = function () {
|
|
|
270
206
|
* @returns {object|null} 配置对象,文件不存在或解析失败返回 null
|
|
271
207
|
* @private
|
|
272
208
|
*/
|
|
273
|
-
|
|
209
|
+
Api.prototype._loadJsonConfig = function (file) {
|
|
274
210
|
if (!fs.existsSync(file)) {
|
|
275
211
|
this.log("warn", "配置文件不存在", file);
|
|
276
212
|
return null;
|
|
@@ -288,7 +224,7 @@ Drive.prototype._loadJsonConfig = function (file) {
|
|
|
288
224
|
* @returns {object|null} 缓存实例;未配置返回 null
|
|
289
225
|
* @private
|
|
290
226
|
*/
|
|
291
|
-
|
|
227
|
+
Api.prototype._getCache = function () {
|
|
292
228
|
var fn = this.config.get_cache;
|
|
293
229
|
if (typeof fn !== "function" && this.parent) {
|
|
294
230
|
fn = this.parent.config.get_cache;
|
|
@@ -300,7 +236,7 @@ Drive.prototype._getCache = function () {
|
|
|
300
236
|
* 设置参数对象(写入模块级缓存,替代原全局 param 模块列表)
|
|
301
237
|
* @param {object} param 参数对象
|
|
302
238
|
*/
|
|
303
|
-
|
|
239
|
+
Api.prototype.updateParam = function (param) {
|
|
304
240
|
PARAM_CACHE.set(param.config_file, param);
|
|
305
241
|
};
|
|
306
242
|
|
|
@@ -309,7 +245,7 @@ Drive.prototype.updateParam = function (param) {
|
|
|
309
245
|
* @param {string} file 文件名
|
|
310
246
|
* @returns {object} 获取的对象
|
|
311
247
|
*/
|
|
312
|
-
|
|
248
|
+
Api.prototype.getParam = function (file) {
|
|
313
249
|
return PARAM_CACHE.get(file) || null;
|
|
314
250
|
};
|
|
315
251
|
|
|
@@ -318,11 +254,11 @@ Drive.prototype.getParam = function (file) {
|
|
|
318
254
|
* @param {string} file_path 参数文件路径
|
|
319
255
|
* @returns {object|null} 参数驱动实例,未配置或加载失败返回 null
|
|
320
256
|
*/
|
|
321
|
-
|
|
257
|
+
Api.prototype._loadParam = function (file_path) {
|
|
322
258
|
if (!file_path || !Param) {
|
|
323
259
|
return null;
|
|
324
260
|
}
|
|
325
|
-
var p =
|
|
261
|
+
var p = file.fullname(file_path, this.getDir());
|
|
326
262
|
var param = this.getParam(p);
|
|
327
263
|
if (param) {
|
|
328
264
|
this.param = param;
|
|
@@ -332,6 +268,7 @@ Drive.prototype._loadParam = function (file_path) {
|
|
|
332
268
|
if (!cfg) {
|
|
333
269
|
return null;
|
|
334
270
|
}
|
|
271
|
+
cfg.name = cfg.name || this.config.name;
|
|
335
272
|
cfg.config_file = p;
|
|
336
273
|
cfg.dir = path.dirname(p);
|
|
337
274
|
this.param = new Param(cfg, this);
|
|
@@ -345,7 +282,7 @@ Drive.prototype._loadParam = function (file_path) {
|
|
|
345
282
|
* @param {string} sql.config_file 文件名
|
|
346
283
|
* @param {object} sql.sql sql语句
|
|
347
284
|
*/
|
|
348
|
-
|
|
285
|
+
Api.prototype.updateSql = function (sql) {
|
|
349
286
|
SQL_CACHE.set(sql.config_file, sql);
|
|
350
287
|
};
|
|
351
288
|
|
|
@@ -354,7 +291,7 @@ Drive.prototype.updateSql = function (sql) {
|
|
|
354
291
|
* @param {string} file 文件名
|
|
355
292
|
* @returns {object} 获取的对象
|
|
356
293
|
*/
|
|
357
|
-
|
|
294
|
+
Api.prototype.getSql = function (file) {
|
|
358
295
|
return SQL_CACHE.get(file) || null;
|
|
359
296
|
};
|
|
360
297
|
|
|
@@ -363,11 +300,11 @@ Drive.prototype.getSql = function (file) {
|
|
|
363
300
|
* @param {string} file_path sql模板文件路径
|
|
364
301
|
* @returns {object|null} sql驱动实例,未配置或加载失败返回 null
|
|
365
302
|
*/
|
|
366
|
-
|
|
303
|
+
Api.prototype._loadSql = function (file_path) {
|
|
367
304
|
if (!file_path || !Sql) {
|
|
368
305
|
return null;
|
|
369
306
|
}
|
|
370
|
-
var p =
|
|
307
|
+
var p = file.fullname(file_path, this.getDir());
|
|
371
308
|
var sql = this.getSql(p);
|
|
372
309
|
if (sql) {
|
|
373
310
|
this.sql = sql;
|
|
@@ -388,7 +325,7 @@ Drive.prototype._loadSql = function (file_path) {
|
|
|
388
325
|
/**
|
|
389
326
|
* 加载身份验证配置
|
|
390
327
|
*/
|
|
391
|
-
|
|
328
|
+
Api.prototype._loadOauth = function () {
|
|
392
329
|
var cg = this.config.oauth;
|
|
393
330
|
if (cg) {
|
|
394
331
|
this.oauth = new Oauth({
|
|
@@ -406,19 +343,19 @@ Drive.prototype._loadOauth = function () {
|
|
|
406
343
|
* @returns {string|null} 脚本文件路径,未配置返回null
|
|
407
344
|
* @private
|
|
408
345
|
*/
|
|
409
|
-
|
|
346
|
+
Api.prototype._getScriptFile = function () {
|
|
410
347
|
var main = this.config.main || "";
|
|
411
348
|
if (!main) {
|
|
412
349
|
return null;
|
|
413
350
|
}
|
|
414
|
-
return
|
|
351
|
+
return file.fullname(main, this.getDir());
|
|
415
352
|
};
|
|
416
353
|
|
|
417
354
|
/**
|
|
418
355
|
* 脚本是否已加载(方案B:替代旧基类 isLoaded 机制)
|
|
419
356
|
* @returns {boolean} 是否已加载
|
|
420
357
|
*/
|
|
421
|
-
|
|
358
|
+
Api.prototype.isLoaded = function () {
|
|
422
359
|
return this._loaded;
|
|
423
360
|
};
|
|
424
361
|
|
|
@@ -426,7 +363,7 @@ Drive.prototype.isLoaded = function () {
|
|
|
426
363
|
* 加载主脚本文件(方案B:标准 require,替代旧基类 do("load") 与热重载模式)
|
|
427
364
|
* @returns {object|null} 加载的模块对象
|
|
428
365
|
*/
|
|
429
|
-
|
|
366
|
+
Api.prototype.load = function () {
|
|
430
367
|
var file = this._getScriptFile();
|
|
431
368
|
if (!file || !fs.existsSync(file)) {
|
|
432
369
|
return null;
|
|
@@ -446,7 +383,7 @@ Drive.prototype.load = function () {
|
|
|
446
383
|
* @param {object} mod 模块对象
|
|
447
384
|
* @private
|
|
448
385
|
*/
|
|
449
|
-
|
|
386
|
+
Api.prototype._setMainMethod = function (mod) {
|
|
450
387
|
if (!mod) {
|
|
451
388
|
return;
|
|
452
389
|
}
|
|
@@ -469,7 +406,7 @@ Drive.prototype._setMainMethod = function (mod) {
|
|
|
469
406
|
* @param {string} files.file 上传的文件对象
|
|
470
407
|
* @returns {object} 返回文件路径和URL
|
|
471
408
|
*/
|
|
472
|
-
|
|
409
|
+
Api.prototype.saveFile = function (files) {
|
|
473
410
|
var file;
|
|
474
411
|
var url;
|
|
475
412
|
if (files.file) {
|
|
@@ -477,7 +414,7 @@ Drive.prototype.saveFile = function (files) {
|
|
|
477
414
|
// 安全处理文件名: 移除路径穿越字符和特殊字符
|
|
478
415
|
var name = f.name || f.originalFilename;
|
|
479
416
|
name = name.replace(/[\\/]|\.\./g, "_");
|
|
480
|
-
file =
|
|
417
|
+
file = file.fullname("./" + name, this.save_dir);
|
|
481
418
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
482
419
|
// 创建写入流
|
|
483
420
|
var read_stream = fs.createReadStream(f.filepath);
|
|
@@ -513,8 +450,8 @@ Drive.prototype.saveFile = function (files) {
|
|
|
513
450
|
* @returns {object|null} 登录态与用户信息
|
|
514
451
|
* @private
|
|
515
452
|
*/
|
|
516
|
-
|
|
517
|
-
var db1 = db.new("
|
|
453
|
+
Api.prototype._getState = async function (db, token) {
|
|
454
|
+
var db1 = db.new("sys_user_state", "user_id");
|
|
518
455
|
var state = await db1.getObj(
|
|
519
456
|
{
|
|
520
457
|
token,
|
|
@@ -528,7 +465,7 @@ Drive.prototype._getState = async function (db, token) {
|
|
|
528
465
|
}
|
|
529
466
|
var now = new Date();
|
|
530
467
|
if (date.interval(state.time_sign, now, "day") <= 30) {
|
|
531
|
-
db.table = "
|
|
468
|
+
db.table = "sys_user";
|
|
532
469
|
var user = await db.getObj({
|
|
533
470
|
user_id: state.user_id,
|
|
534
471
|
});
|
|
@@ -552,10 +489,10 @@ Drive.prototype._getState = async function (db, token) {
|
|
|
552
489
|
* @param {object} db 数据管理器
|
|
553
490
|
* @returns {object} 执行结果
|
|
554
491
|
*/
|
|
555
|
-
|
|
492
|
+
Api.prototype.getLoginState = async function (params, ctx, options) {
|
|
556
493
|
var o = await this._getUserFromSession(ctx);
|
|
557
494
|
if (!o) {
|
|
558
|
-
o = await this._getUserFromToken(ctx, options);
|
|
495
|
+
o = await this._getUserFromToken(params, ctx, options);
|
|
559
496
|
}
|
|
560
497
|
return this._processUserInfo(o);
|
|
561
498
|
};
|
|
@@ -566,7 +503,7 @@ Drive.prototype.getLoginState = async function (ctx, options) {
|
|
|
566
503
|
* @returns {object|null} 用户信息
|
|
567
504
|
* @private
|
|
568
505
|
*/
|
|
569
|
-
|
|
506
|
+
Api.prototype._getUserFromSession = function (ctx) {
|
|
570
507
|
var u = ctx.session?.user;
|
|
571
508
|
if (u) {
|
|
572
509
|
var o = { ...u };
|
|
@@ -583,7 +520,7 @@ Drive.prototype._getUserFromSession = function (ctx) {
|
|
|
583
520
|
* @returns {object|null} 用户信息
|
|
584
521
|
* @private
|
|
585
522
|
*/
|
|
586
|
-
|
|
523
|
+
Api.prototype._getUserFromToken = async function (params, ctx, options) {
|
|
587
524
|
var token = ctx.headers[this.config.token_key];
|
|
588
525
|
if (!token) {
|
|
589
526
|
return null;
|
|
@@ -607,7 +544,7 @@ Drive.prototype._getUserFromToken = async function (ctx, options) {
|
|
|
607
544
|
* @returns {object|null} 用户信息
|
|
608
545
|
* @private
|
|
609
546
|
*/
|
|
610
|
-
|
|
547
|
+
Api.prototype._processUserFromCache = async function (u, ctx, options, token) {
|
|
611
548
|
var o;
|
|
612
549
|
if (typeof u === "string") {
|
|
613
550
|
o = { ...string.toJson(u) };
|
|
@@ -628,12 +565,12 @@ Drive.prototype._processUserFromCache = async function (u, ctx, options, token)
|
|
|
628
565
|
* 从数据库中获取用户信息
|
|
629
566
|
* @param {string} token token
|
|
630
567
|
* @param {object} ctx 请求上下文
|
|
631
|
-
* @param {object}
|
|
568
|
+
* @param {object} options 数据管理器
|
|
632
569
|
* @returns {object|null} 用户信息
|
|
633
570
|
* @private
|
|
634
571
|
*/
|
|
635
|
-
|
|
636
|
-
var obj = await this._getState(
|
|
572
|
+
Api.prototype._getUserFromDb = async function (token, ctx, options) {
|
|
573
|
+
var obj = await this._getState(options, token);
|
|
637
574
|
if (obj) {
|
|
638
575
|
var o = { ...obj.user };
|
|
639
576
|
ctx.session.user = obj.user;
|
|
@@ -650,7 +587,7 @@ Drive.prototype._getUserFromDb = async function (token, ctx, options) {
|
|
|
650
587
|
* @returns {object|null} 处理后的用户信息
|
|
651
588
|
* @private
|
|
652
589
|
*/
|
|
653
|
-
|
|
590
|
+
Api.prototype._processUserInfo = function (o) {
|
|
654
591
|
if (o) {
|
|
655
592
|
o.password_pay = o.password_pay ? "******" : "";
|
|
656
593
|
o.password = o.password ? "******" : "";
|
|
@@ -663,22 +600,22 @@ Drive.prototype._processUserInfo = function (o) {
|
|
|
663
600
|
/**
|
|
664
601
|
* 获取登录态
|
|
665
602
|
* @param {object} ctx 请求上下文
|
|
666
|
-
* @param {object}
|
|
603
|
+
* @param {object} options 数据管理器
|
|
667
604
|
* @returns {object} 执行结果
|
|
668
605
|
*/
|
|
669
|
-
|
|
606
|
+
Api.prototype.before = async function (params, ctx, options) {
|
|
670
607
|
if (this.config.oauth && this.config.oauth.sign_in) {
|
|
671
|
-
|
|
608
|
+
options.user = await this.getLoginState(params, ctx, options);
|
|
672
609
|
}
|
|
673
610
|
};
|
|
674
611
|
|
|
675
612
|
/**
|
|
676
613
|
* 主要函数
|
|
677
614
|
* @param {object} ctx 请求上下文
|
|
678
|
-
* @param {object}
|
|
615
|
+
* @param {object} options 数据管理器
|
|
679
616
|
* @returns {object} 执行结果
|
|
680
617
|
*/
|
|
681
|
-
|
|
618
|
+
Api.prototype.main = async function (params, ctx, options) {
|
|
682
619
|
if (this.sql) {
|
|
683
620
|
var req = ctx.request;
|
|
684
621
|
// 获取文件
|
|
@@ -687,10 +624,10 @@ Drive.prototype.main = async function (ctx, options) {
|
|
|
687
624
|
req.body.file = fobj.file;
|
|
688
625
|
req.body.url = fobj.url;
|
|
689
626
|
}
|
|
690
|
-
var ret = await this.sql.run(req.query, req.body,
|
|
627
|
+
var ret = await this.sql.run(req.query, req.body, options);
|
|
691
628
|
return ret;
|
|
692
629
|
} else {
|
|
693
|
-
return
|
|
630
|
+
return getRet().error(10000, "接口函数未定义");
|
|
694
631
|
}
|
|
695
632
|
};
|
|
696
633
|
|
|
@@ -700,7 +637,7 @@ Drive.prototype.main = async function (ctx, options) {
|
|
|
700
637
|
* @param {object} db 数据管理器
|
|
701
638
|
* @returns {object} 执行结果
|
|
702
639
|
*/
|
|
703
|
-
|
|
640
|
+
Api.prototype.run = async function (params, ctx, options) {
|
|
704
641
|
try {
|
|
705
642
|
var body = await this.getCache(ctx);
|
|
706
643
|
if (!body) {
|
|
@@ -709,14 +646,14 @@ Drive.prototype.run = async function (ctx, options) {
|
|
|
709
646
|
if (md !== req.method && md !== "ALL") {
|
|
710
647
|
return null;
|
|
711
648
|
}
|
|
712
|
-
await this.before(ctx, options);
|
|
713
|
-
var ret = await this.check(ctx, options);
|
|
649
|
+
await this.before(params, ctx, options);
|
|
650
|
+
var ret = await this.check(params, ctx, options);
|
|
714
651
|
if (!ret) {
|
|
715
652
|
try {
|
|
716
|
-
ret = await this.main(ctx, options);
|
|
653
|
+
ret = await this.main(params, ctx, options);
|
|
717
654
|
} catch (err) {
|
|
718
655
|
this.log("error", "脚本文件错误", req.path, err);
|
|
719
|
-
ret =
|
|
656
|
+
ret = getRet().error(10000, "脚本文件错误:" + err.toString());
|
|
720
657
|
}
|
|
721
658
|
}
|
|
722
659
|
var res = ctx.response;
|
|
@@ -734,7 +671,7 @@ Drive.prototype.run = async function (ctx, options) {
|
|
|
734
671
|
* @param {object} ctx 请求上下文
|
|
735
672
|
* @returns {string} 响应内容
|
|
736
673
|
*/
|
|
737
|
-
|
|
674
|
+
Api.prototype.getCache = async function (ctx) {
|
|
738
675
|
var cg = this.config;
|
|
739
676
|
if (cg.cache) {
|
|
740
677
|
var req = ctx.request;
|
|
@@ -769,7 +706,7 @@ Drive.prototype.getCache = async function (ctx) {
|
|
|
769
706
|
* @param {object} body 正文参数
|
|
770
707
|
* @param {string} type 响应类型
|
|
771
708
|
*/
|
|
772
|
-
|
|
709
|
+
Api.prototype.setCache = async function (ctx, body, type = null) {
|
|
773
710
|
var cg = this.config;
|
|
774
711
|
if (cg.cache && ctx.method === "GET") {
|
|
775
712
|
if (cg.client_cache) {
|
|
@@ -796,7 +733,7 @@ Drive.prototype.setCache = async function (ctx, body, type = null) {
|
|
|
796
733
|
* @param {string} t 请求类型
|
|
797
734
|
* @returns {string} 处理后的响应结果
|
|
798
735
|
*/
|
|
799
|
-
|
|
736
|
+
Api.prototype.body = function (result, res, t) {
|
|
800
737
|
var ret = result;
|
|
801
738
|
var type = res.type;
|
|
802
739
|
if (!ret) {
|
|
@@ -821,7 +758,7 @@ Drive.prototype.body = function (result, res, t) {
|
|
|
821
758
|
* @returns {string} 处理后的类型
|
|
822
759
|
* @private
|
|
823
760
|
*/
|
|
824
|
-
|
|
761
|
+
Api.prototype._handleObjectType = function (res, type, t) {
|
|
825
762
|
var new_type = type;
|
|
826
763
|
if (!new_type) {
|
|
827
764
|
new_type = t.indexOf("xml") !== -1 ? t : "application/json; charset=utf-8";
|
|
@@ -839,7 +776,7 @@ Drive.prototype._handleObjectType = function (res, type, t) {
|
|
|
839
776
|
* @returns {string} 标准化后的类型
|
|
840
777
|
* @private
|
|
841
778
|
*/
|
|
842
|
-
|
|
779
|
+
Api.prototype._normType = function (type) {
|
|
843
780
|
if (type.indexOf("json") !== -1) {
|
|
844
781
|
return "application/json; charset=utf-8";
|
|
845
782
|
} else if (type.indexOf("html") !== -1) {
|
|
@@ -857,8 +794,8 @@ Drive.prototype._normType = function (type) {
|
|
|
857
794
|
* @returns {string} 转换后的字符串
|
|
858
795
|
* @private
|
|
859
796
|
*/
|
|
860
|
-
|
|
861
|
-
return type.indexOf("/xml") !== -1 ?
|
|
797
|
+
Api.prototype._convertObject = function (obj, type) {
|
|
798
|
+
return type.indexOf("/xml") !== -1 ? object.toXml(obj) : JSON.stringify(obj);
|
|
862
799
|
};
|
|
863
800
|
|
|
864
801
|
/**
|
|
@@ -868,7 +805,7 @@ Drive.prototype._convertObject = function (obj, type) {
|
|
|
868
805
|
* @param {string} str 字符串
|
|
869
806
|
* @private
|
|
870
807
|
*/
|
|
871
|
-
|
|
808
|
+
Api.prototype._handleStringType = function (res, type, str) {
|
|
872
809
|
if (!type) {
|
|
873
810
|
if (
|
|
874
811
|
(str.startsWith("{") && str.endsWith("}")) ||
|
|
@@ -892,17 +829,21 @@ Drive.prototype._handleStringType = function (res, type, str) {
|
|
|
892
829
|
* @param {string} method 方法
|
|
893
830
|
* @returns {object} 验证结果
|
|
894
831
|
*/
|
|
895
|
-
|
|
832
|
+
Api.prototype.checkParam = function (query, body, method) {
|
|
896
833
|
if (this.param) {
|
|
897
834
|
var msg = this.param.check(query, body, method);
|
|
898
835
|
if (msg) {
|
|
899
836
|
var code;
|
|
900
|
-
if (
|
|
837
|
+
if (
|
|
838
|
+
msg.indexOf("必须") !== -1 ||
|
|
839
|
+
msg.indexOf("必填") !== -1 ||
|
|
840
|
+
msg.indexOf("为空") !== -1
|
|
841
|
+
) {
|
|
901
842
|
code = 30001;
|
|
902
843
|
} else {
|
|
903
844
|
code = 30002;
|
|
904
845
|
}
|
|
905
|
-
return
|
|
846
|
+
return getRet().error(code, msg);
|
|
906
847
|
}
|
|
907
848
|
}
|
|
908
849
|
return null;
|
|
@@ -910,10 +851,12 @@ Drive.prototype.checkParam = function (query, body, method) {
|
|
|
910
851
|
|
|
911
852
|
/**
|
|
912
853
|
* 验证参数
|
|
854
|
+
* @param {object} params 请求参数
|
|
913
855
|
* @param {object} ctx 内容上下文
|
|
856
|
+
* @param {object} options 事件运行选项
|
|
914
857
|
* @returns {object} 验证结果
|
|
915
858
|
*/
|
|
916
|
-
|
|
859
|
+
Api.prototype.check = async function (params, ctx, options) {
|
|
917
860
|
var error;
|
|
918
861
|
if (this.config.check_param) {
|
|
919
862
|
var req = ctx.request;
|
|
@@ -935,7 +878,7 @@ Drive.prototype.check = async function (ctx) {
|
|
|
935
878
|
* @param {object} ctx 内容上下文
|
|
936
879
|
* @returns {object} 验证结果
|
|
937
880
|
*/
|
|
938
|
-
|
|
881
|
+
Api.prototype.checkOauth = async function (ctx) {
|
|
939
882
|
var aouth = this.oauth;
|
|
940
883
|
if (aouth) {
|
|
941
884
|
return await aouth.check(ctx);
|
|
@@ -950,7 +893,7 @@ Drive.prototype.checkOauth = async function (ctx) {
|
|
|
950
893
|
* @param {string} name 插件名称
|
|
951
894
|
* @returns {object} 返回获取到的插件
|
|
952
895
|
*/
|
|
953
|
-
|
|
896
|
+
Api.prototype.plugin = function (app, name) {
|
|
954
897
|
var l = path.sep;
|
|
955
898
|
var dir = this.config.dir || "";
|
|
956
899
|
var app_name = app || string.between(dir, "app" + l, l);
|
|
@@ -968,7 +911,7 @@ Drive.prototype.plugin = function (app, name) {
|
|
|
968
911
|
* @param {string} type 模型类型
|
|
969
912
|
* @returns {object} 返回获取到的模型
|
|
970
913
|
*/
|
|
971
|
-
|
|
914
|
+
Api.prototype.getModel = function (type) {
|
|
972
915
|
let model = { ...this.config };
|
|
973
916
|
let dir = this.getDir();
|
|
974
917
|
let l = path.sep;
|
|
@@ -982,4 +925,4 @@ Drive.prototype.getModel = function (type) {
|
|
|
982
925
|
return model;
|
|
983
926
|
};
|
|
984
927
|
|
|
985
|
-
module.exports =
|
|
928
|
+
module.exports = Api;
|