jj.js 0.8.8 → 0.9.0
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/CHANGELOG.md +13 -0
- package/LICENSE +21 -21
- package/README.md +22 -551
- package/jj.js +14 -8
- package/jsconfig.json +9 -0
- package/jsdoc.conf.js +17 -0
- package/lib/app.js +53 -48
- package/lib/cache.js +118 -65
- package/lib/config.js +98 -94
- package/lib/context.js +18 -10
- package/lib/controller.js +34 -8
- package/lib/cookie.js +82 -40
- package/lib/ctx.js +9 -0
- package/lib/db.js +352 -11
- package/lib/loader.js +10 -4
- package/lib/logger.js +116 -47
- package/lib/middleware.js +58 -26
- package/lib/model.js +54 -1
- package/lib/pagination.js +221 -182
- package/lib/response.js +62 -8
- package/lib/router.js +3 -3
- package/lib/tpl/exception.js +64 -64
- package/lib/tpl/jump.js +40 -40
- package/lib/types.js +286 -0
- package/lib/upload.js +82 -5
- package/lib/url.js +19 -0
- package/lib/utils/date.js +50 -39
- package/lib/utils/error.js +8 -4
- package/lib/utils/fs.js +77 -77
- package/lib/utils/md5.js +9 -7
- package/lib/utils/str.js +20 -10
- package/lib/utils/utils.js +16 -10
- package/lib/view.js +79 -12
- package/package.json +8 -6
package/lib/loader.js
CHANGED
|
@@ -5,12 +5,17 @@ const {isFileSync, isDirSync} = require('./utils/fs');
|
|
|
5
5
|
const nodeType = {};
|
|
6
6
|
|
|
7
7
|
function loader(dir='./', ...args) {
|
|
8
|
-
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
dir = pt.isAbsolute(dir) ? dir : pt.join(pt.dirname(module.parent.filename), dir);
|
|
10
|
+
const dirPath = pt.join(dir, './');
|
|
11
|
+
const dirType = isFileSync(dir + '.js') ? 'file' : 'dir';
|
|
12
|
+
const node = dirType == 'file' ? require(dir) : {};
|
|
9
13
|
const info = new Map();
|
|
10
14
|
info.set(node, {
|
|
11
|
-
path:
|
|
12
|
-
nodeType:
|
|
13
|
-
|
|
15
|
+
path: dirPath,
|
|
16
|
+
nodeType: dirType,
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
isClass: isClass(node)
|
|
14
19
|
});
|
|
15
20
|
return creatLoader(node);
|
|
16
21
|
|
|
@@ -51,6 +56,7 @@ function loader(dir='./', ...args) {
|
|
|
51
56
|
info.set(_node, {
|
|
52
57
|
path: _nodePath,
|
|
53
58
|
nodeType: nodeType[_nodePath],
|
|
59
|
+
// @ts-ignore
|
|
54
60
|
isClass: isClass(_node)
|
|
55
61
|
});
|
|
56
62
|
node[prop] = creatLoader(_node);
|
package/lib/logger.js
CHANGED
|
@@ -1,48 +1,117 @@
|
|
|
1
|
-
const {app: cfg_app, log: cfg_log} = require('./config');
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
{
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
-
static
|
|
42
|
-
this.
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
1
|
+
const {app: cfg_app, log: cfg_log} = require('./config');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {import('./types').LogHandle} LogHandle
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
class Logger
|
|
8
|
+
{
|
|
9
|
+
/**
|
|
10
|
+
* Creat a new `Logger` class
|
|
11
|
+
* @public
|
|
12
|
+
* @static
|
|
13
|
+
* @param {LogHandle} [handle]
|
|
14
|
+
*/
|
|
15
|
+
constructor(handle) {
|
|
16
|
+
if(new.target) {
|
|
17
|
+
// @ts-ignore
|
|
18
|
+
class ClildLogger extends this.constructor {}
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
ClildLogger.setHandle(handle);
|
|
21
|
+
return ClildLogger;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 输出error日志
|
|
27
|
+
* @public
|
|
28
|
+
* @static
|
|
29
|
+
* @param {...any} args
|
|
30
|
+
*/
|
|
31
|
+
static error(...args) {
|
|
32
|
+
this.log('error', ...args);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 输出warning日志
|
|
37
|
+
* @public
|
|
38
|
+
* @static
|
|
39
|
+
* @param {...any} args
|
|
40
|
+
*/
|
|
41
|
+
static warning(...args) {
|
|
42
|
+
this.log('warning', ...args);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 输出info日志
|
|
47
|
+
* @public
|
|
48
|
+
* @static
|
|
49
|
+
* @param {...any} args
|
|
50
|
+
*/
|
|
51
|
+
static info(...args) {
|
|
52
|
+
this.log('info', ...args);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 输出debug日志
|
|
57
|
+
* @public
|
|
58
|
+
* @static
|
|
59
|
+
* @param {...any} args
|
|
60
|
+
*/
|
|
61
|
+
static debug(...args) {
|
|
62
|
+
this.log('debug', ...args);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 输出sql日志
|
|
67
|
+
* @public
|
|
68
|
+
* @static
|
|
69
|
+
* @param {...any} args
|
|
70
|
+
*/
|
|
71
|
+
static sql(...args) {
|
|
72
|
+
this.log('sql', ...args);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 输出http日志
|
|
77
|
+
* @public
|
|
78
|
+
* @static
|
|
79
|
+
* @param {...any} args
|
|
80
|
+
*/
|
|
81
|
+
static http(...args) {
|
|
82
|
+
this.log('http', ...args);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 输出error日志
|
|
87
|
+
* @public
|
|
88
|
+
* @static
|
|
89
|
+
* @param {string} level - 日志级别、标识
|
|
90
|
+
* @param {...any} args - 日志内容
|
|
91
|
+
*/
|
|
92
|
+
static log(level='info', ...args) {
|
|
93
|
+
(cfg_app.app_debug || ~cfg_log.log_level.indexOf(level)) && this.handle(level, ...args);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 设置日志handle
|
|
98
|
+
* @public
|
|
99
|
+
* @static
|
|
100
|
+
* @param {LogHandle} handle
|
|
101
|
+
* @returns {Logger}
|
|
102
|
+
*/
|
|
103
|
+
static setHandle(handle) {
|
|
104
|
+
/**
|
|
105
|
+
* @type {LogHandle}
|
|
106
|
+
*/
|
|
107
|
+
this.handle = typeof handle == 'function' ? handle : cfg_log.log_handle;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 设置日志handle
|
|
114
|
+
*/
|
|
115
|
+
Logger.setHandle(cfg_log.log_handle);
|
|
116
|
+
|
|
48
117
|
module.exports = Logger;
|
package/lib/middleware.js
CHANGED
|
@@ -1,27 +1,59 @@
|
|
|
1
|
-
const Context = require('./context');
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
1
|
+
const Context = require('./context');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @extends Context
|
|
5
|
+
*/
|
|
6
|
+
class Middleware extends Context
|
|
7
|
+
{
|
|
8
|
+
/**
|
|
9
|
+
* Initialize a new `Middleware`
|
|
10
|
+
* @public
|
|
11
|
+
* @param {import('./types').Context} ctx
|
|
12
|
+
* @param {import('./types').Middleware} [next]
|
|
13
|
+
*/
|
|
14
|
+
constructor(ctx, next) {
|
|
15
|
+
super(ctx);
|
|
16
|
+
this.$next = next;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 直接输出内容
|
|
21
|
+
* @public
|
|
22
|
+
* @param {*} data
|
|
23
|
+
*/
|
|
24
|
+
$show(data) {
|
|
25
|
+
this.$response.show(data);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 跳转重定向
|
|
30
|
+
* @public
|
|
31
|
+
* @param {(string|object)} url
|
|
32
|
+
* @param {number} [status]
|
|
33
|
+
*/
|
|
34
|
+
$redirect(url, status) {
|
|
35
|
+
this.$response.redirect(url, status);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 输出成功提示(html|json)
|
|
40
|
+
* @public
|
|
41
|
+
* @param {(string|object)} [msg]
|
|
42
|
+
* @param {(string|object)} [url]
|
|
43
|
+
*/
|
|
44
|
+
$success(msg, url) {
|
|
45
|
+
this.$response.success(msg, url);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 输出错误提示(html|json)
|
|
50
|
+
* @public
|
|
51
|
+
* @param {(string|object)} [msg]
|
|
52
|
+
* @param {(string|object)} [url]
|
|
53
|
+
*/
|
|
54
|
+
$error(msg, url) {
|
|
55
|
+
this.$response.error(msg, url);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
27
59
|
module.exports = Middleware;
|
package/lib/model.js
CHANGED
|
@@ -1,25 +1,55 @@
|
|
|
1
1
|
const Context = require('./context');
|
|
2
2
|
const {toLine} = require('./utils/str');
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @typedef {import('./types').OkPacket} OkPacket
|
|
6
|
+
* @typedef {import('./types').RowData} RowData
|
|
7
|
+
* @typedef {import('./types').ListData} ListData
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @extends Context
|
|
12
|
+
*/
|
|
4
13
|
class Model extends Context
|
|
5
14
|
{
|
|
15
|
+
/**
|
|
16
|
+
* Initialize a new `Model`
|
|
17
|
+
* @public
|
|
18
|
+
* @param {import('./types').Context} ctx
|
|
19
|
+
*/
|
|
6
20
|
constructor(ctx) {
|
|
7
21
|
super(ctx);
|
|
8
22
|
this.connection = null;
|
|
9
23
|
this.table = toLine(this.constructor.name);
|
|
10
24
|
this.pk = 'id';
|
|
25
|
+
this._db;
|
|
11
26
|
}
|
|
12
27
|
|
|
28
|
+
/**
|
|
29
|
+
* 插入一条数据
|
|
30
|
+
* @public
|
|
31
|
+
* @param {object} data - 待插入数据
|
|
32
|
+
* @returns {Promise<OkPacket>}
|
|
33
|
+
*/
|
|
13
34
|
async add(data) {
|
|
35
|
+
// @ts-ignore
|
|
14
36
|
return await this.db.allowField().insert(data);
|
|
15
37
|
}
|
|
16
38
|
|
|
39
|
+
/**
|
|
40
|
+
* 更新或插入一条数据
|
|
41
|
+
* @public
|
|
42
|
+
* @param {object} data - 更新或插入数据
|
|
43
|
+
* @param {object} [condition] - 更新条件,不为空或data包含主键则更新数据,否则插入数据
|
|
44
|
+
* @returns {Promise<OkPacket>}
|
|
45
|
+
*/
|
|
17
46
|
async save(data, condition = {}) {
|
|
18
47
|
if(data[this.pk] || Object.keys(condition).length) {
|
|
19
48
|
if(data[this.pk]) {
|
|
20
49
|
condition[this.pk] = data[this.pk];
|
|
21
50
|
delete data[this.pk];
|
|
22
51
|
}
|
|
52
|
+
// @ts-ignore
|
|
23
53
|
return await this.db.allowField().update(data, condition);
|
|
24
54
|
}
|
|
25
55
|
|
|
@@ -27,19 +57,42 @@ class Model extends Context
|
|
|
27
57
|
return await this.add(data);
|
|
28
58
|
}
|
|
29
59
|
|
|
60
|
+
/**
|
|
61
|
+
* 删除数据
|
|
62
|
+
* @public
|
|
63
|
+
* @param {object} condition - 删除条件
|
|
64
|
+
* @returns {Promise<OkPacket>}
|
|
65
|
+
*/
|
|
30
66
|
async del(condition) {
|
|
67
|
+
// @ts-ignore
|
|
31
68
|
return await this.db.delete(condition);
|
|
32
69
|
}
|
|
33
70
|
|
|
71
|
+
/**
|
|
72
|
+
* 查询一条数据
|
|
73
|
+
* @public
|
|
74
|
+
* @param {object} condition - 查询条件
|
|
75
|
+
* @returns {Promise<(RowData|null)>}
|
|
76
|
+
*/
|
|
34
77
|
async get(condition) {
|
|
35
78
|
return await this.db.find(condition);
|
|
36
79
|
}
|
|
37
80
|
|
|
81
|
+
/**
|
|
82
|
+
* 查询多条数据
|
|
83
|
+
* @public
|
|
84
|
+
* @param {object} condition - 查询条件
|
|
85
|
+
* @returns {Promise<(ListData)>}
|
|
86
|
+
*/
|
|
38
87
|
async all(condition) {
|
|
88
|
+
// @ts-ignore
|
|
39
89
|
return await this.db.select(condition);
|
|
40
90
|
}
|
|
41
91
|
|
|
42
|
-
|
|
92
|
+
/**
|
|
93
|
+
* 数据库实例
|
|
94
|
+
* @type {typeof import('./db').prototype}
|
|
95
|
+
*/
|
|
43
96
|
get db() {
|
|
44
97
|
if(!this._db){
|
|
45
98
|
this._db = new (require('./db'))(this.ctx);
|