jj.js 0.7.6 → 0.8.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/lib/db.js +1 -5
- package/lib/router.js +7 -10
- package/lib/run.js +6 -1
- package/lib/view.js +34 -10
- package/package.json +1 -1
package/lib/db.js
CHANGED
|
@@ -106,7 +106,6 @@ class Db extends Context
|
|
|
106
106
|
try {
|
|
107
107
|
await fun();
|
|
108
108
|
await this.commit();
|
|
109
|
-
resolve('事务执行成功!');
|
|
110
109
|
} catch(e) {
|
|
111
110
|
await this.rollback();
|
|
112
111
|
throw e;
|
|
@@ -450,10 +449,7 @@ class Db extends Context
|
|
|
450
449
|
return result;
|
|
451
450
|
}
|
|
452
451
|
|
|
453
|
-
async pagination(page, page_size, pagination) {
|
|
454
|
-
if(typeof page == 'object' || typeof page == 'function') {
|
|
455
|
-
[page, pagination] = [pagination, page];
|
|
456
|
-
}
|
|
452
|
+
async pagination({page, page_size, pagination} = {}) {
|
|
457
453
|
if(!pagination) {
|
|
458
454
|
pagination = this.$pagination.__node.isClass ? this.$pagination : this.$.pagination;
|
|
459
455
|
}
|
package/lib/router.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const router = require('@koa/router')();
|
|
2
2
|
const {app: cfg_app, routes: cfg_routes} = require('./config');
|
|
3
3
|
const {toLine} = require('./utils/str');
|
|
4
|
-
const Logger = require('./logger');
|
|
5
4
|
const run = require('./run');
|
|
6
5
|
|
|
7
6
|
// 注册用户路由
|
|
@@ -27,21 +26,19 @@ if (cfg_routes) {
|
|
|
27
26
|
ctx.APP = paths[2] || cfg_app.default_app;
|
|
28
27
|
ctx.CONTROLLER = paths[1] || cfg_app.default_controller;
|
|
29
28
|
ctx.ACTION = paths[0] || cfg_app.default_action;
|
|
30
|
-
Logger.debug(`Router: {APP: ${ctx.APP}, CONTROLLER: ${ctx.CONTROLLER}, ACTION: ${ctx.ACTION}}`);
|
|
31
29
|
await run(ctx, next, item.type);
|
|
32
30
|
});
|
|
33
31
|
}
|
|
34
32
|
}
|
|
35
33
|
|
|
36
34
|
// 注册系统路由
|
|
37
|
-
router.all(cfg_app.app_multi ? '/:
|
|
38
|
-
ctx.APP = ctx.params.
|
|
39
|
-
ctx.CONTROLLER = ctx.params.
|
|
40
|
-
ctx.ACTION = ctx.params.
|
|
41
|
-
delete ctx.params.
|
|
42
|
-
delete ctx.params.
|
|
43
|
-
delete ctx.params.
|
|
44
|
-
Logger.debug(`Router: {APP: ${ctx.APP}, CONTROLLER: ${ctx.CONTROLLER}, ACTION: ${ctx.ACTION}}`);
|
|
35
|
+
router.all(cfg_app.app_multi ? '/:APP?/:CONTROLLER?/:ACTION?' : '/:CONTROLLER?/:ACTION?', async (ctx, next) => {
|
|
36
|
+
ctx.APP = ctx.params.APP || cfg_app.default_app;
|
|
37
|
+
ctx.CONTROLLER = ctx.params.CONTROLLER || cfg_app.default_controller;
|
|
38
|
+
ctx.ACTION = ctx.params.ACTION || cfg_app.default_action;
|
|
39
|
+
delete ctx.params.APP;
|
|
40
|
+
delete ctx.params.CONTROLLER;
|
|
41
|
+
delete ctx.params.ACTION;
|
|
45
42
|
await run(ctx, next);
|
|
46
43
|
});
|
|
47
44
|
|
package/lib/run.js
CHANGED
|
@@ -4,8 +4,13 @@ const {readFile} = require('./utils/fs');
|
|
|
4
4
|
const {toHump} = require('./utils/str');
|
|
5
5
|
const {app: cfg_app, view: cfg_view, db, page, log, cache, cookie, tpl} = require('./config');
|
|
6
6
|
const compose = require('koa-compose');
|
|
7
|
+
const Logger = require('./logger');
|
|
7
8
|
|
|
8
9
|
async function run(ctx, next, control_type=cfg_app.controller_folder) {
|
|
10
|
+
Logger.debug(ctx.request);
|
|
11
|
+
Logger.debug(`Router: {APP: '${ctx.APP}', CONTROLLER: '${ctx.CONTROLLER}', ACTION: '${ctx.ACTION}'}`);
|
|
12
|
+
Logger.debug('Params/Get/Post:', ctx.params, ctx.query, ctx.request.body);
|
|
13
|
+
|
|
9
14
|
// 设置根加载器
|
|
10
15
|
ctx.$ = loader('./', ctx);
|
|
11
16
|
ctx._ = loader(cfg_app.base_dir, ctx);
|
|
@@ -95,7 +100,7 @@ async function run(ctx, next, control_type=cfg_app.controller_folder) {
|
|
|
95
100
|
}
|
|
96
101
|
const Middleware = ctx._[APP]['middleware'][MIDDLEWARE];
|
|
97
102
|
if(!Middleware || typeof Middleware != 'function') {
|
|
98
|
-
throw new
|
|
103
|
+
throw new Error(`中间件文件:${APP}/middleware/${MIDDLEWARE}不存在!`);
|
|
99
104
|
}
|
|
100
105
|
const $middleware = new Middleware(ctx, next);
|
|
101
106
|
if(!$middleware[METHOD] || typeof $middleware[METHOD] != 'function') {
|
package/lib/view.js
CHANGED
|
@@ -14,10 +14,7 @@ class View extends Context
|
|
|
14
14
|
|
|
15
15
|
// 加载文件
|
|
16
16
|
async load(template) {
|
|
17
|
-
const file_name = this.
|
|
18
|
-
if(!isFileSync(file_name)) {
|
|
19
|
-
throw new Error(`文件不存在(${file_name})`);
|
|
20
|
-
}
|
|
17
|
+
const file_name = this.parseTplName(template);
|
|
21
18
|
return await readFile(file_name);
|
|
22
19
|
}
|
|
23
20
|
|
|
@@ -28,10 +25,7 @@ class View extends Context
|
|
|
28
25
|
|
|
29
26
|
// 渲染文件
|
|
30
27
|
async fetch(template) {
|
|
31
|
-
const file_name = this.
|
|
32
|
-
if(!isFileSync(file_name)) {
|
|
33
|
-
throw new Error(`文件不存在(${file_name})`);
|
|
34
|
-
}
|
|
28
|
+
const file_name = this.parseTplName(template);
|
|
35
29
|
return await this._engine(file_name, this._data || {});
|
|
36
30
|
}
|
|
37
31
|
|
|
@@ -52,18 +46,36 @@ class View extends Context
|
|
|
52
46
|
}
|
|
53
47
|
|
|
54
48
|
//解析模板地址
|
|
55
|
-
parsePath(template=this.ctx.ACTION) {
|
|
49
|
+
parsePath(template=this.ctx.ACTION, view_folder, view_depr) {
|
|
50
|
+
!view_folder && (view_folder = cfg_view.view_folder);
|
|
51
|
+
!view_depr && (view_depr = cfg_view.view_depr);
|
|
56
52
|
let view_file = template;
|
|
53
|
+
|
|
57
54
|
if(view_file.indexOf('/') !== 0) {
|
|
58
55
|
const temp = view_file.replace(/^\/|\/$/g, '').split('/').reverse().map(u => toLine(u));
|
|
59
56
|
if(temp.length <= 3) {
|
|
60
|
-
view_file = `${temp[2] || this.ctx.APP}/${
|
|
57
|
+
view_file = `${temp[2] || this.ctx.APP}/${view_folder}/${temp[1] || this.ctx.CONTROLLER}${view_depr}${temp[0]}`;
|
|
61
58
|
}
|
|
62
59
|
}
|
|
63
60
|
path.extname(view_file) || (view_file += cfg_view.view_ext);
|
|
64
61
|
return path.join(this.$config.app.base_dir, view_file);
|
|
65
62
|
}
|
|
66
63
|
|
|
64
|
+
//解析模板名字
|
|
65
|
+
parseTplName(template) {
|
|
66
|
+
let file_name = this.parsePath(template, this._folder, this._depr);
|
|
67
|
+
let is_file = isFileSync(file_name);
|
|
68
|
+
if(!is_file && (this._folder || this._depr)) {
|
|
69
|
+
file_name = this.parsePath(template);
|
|
70
|
+
is_file = isFileSync(file_name);
|
|
71
|
+
}
|
|
72
|
+
if(!is_file) {
|
|
73
|
+
file_name = this.parsePath(template, this._folder, this._depr);
|
|
74
|
+
throw new Error(`文件不存在(${file_name})`);
|
|
75
|
+
}
|
|
76
|
+
return file_name;
|
|
77
|
+
}
|
|
78
|
+
|
|
67
79
|
// 设置模版引擎
|
|
68
80
|
setEngine(engine) {
|
|
69
81
|
this._engine = typeof engine == 'string' ? require(engine) : engine;
|
|
@@ -86,6 +98,18 @@ class View extends Context
|
|
|
86
98
|
}
|
|
87
99
|
return this;
|
|
88
100
|
}
|
|
101
|
+
|
|
102
|
+
//设置模版目录
|
|
103
|
+
setFolder(view_folder) {
|
|
104
|
+
this._folder = view_folder;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
//设置文件分割符
|
|
109
|
+
setDepr(view_depr) {
|
|
110
|
+
this._depr = view_depr;
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
89
113
|
}
|
|
90
114
|
|
|
91
115
|
module.exports = View;
|