chanjs 1.0.18 → 1.0.21
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/core/chan.js +5 -4
- package/core/lib/controller/Base.js +4 -0
- package/core/lib/core.js +1 -1
- package/core/lib/extend/bindClass.js +23 -0
- package/package.json +1 -1
package/core/chan.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
const express = require("express");
|
2
2
|
const config = require("./lib/config/config.js");
|
3
|
+
const bindClass = require("./lib/extend/bindClass.js");
|
3
4
|
const path = require("path");
|
4
5
|
const fs = require("fs");
|
5
6
|
const core = require("./lib/core.js");
|
@@ -73,9 +74,9 @@ class Chan {
|
|
73
74
|
//数据库操作
|
74
75
|
loadKnex(){
|
75
76
|
// 连接数据库
|
76
|
-
const {host,port,user,password,database,client,charset} = Chan.config.database;
|
77
|
+
const {host,port,user,password,database,client='mysql2',charset} = Chan.config.database;
|
77
78
|
const knex = require("knex")({
|
78
|
-
client:
|
79
|
+
client: client,
|
79
80
|
connection: {
|
80
81
|
host,
|
81
82
|
port,
|
@@ -184,7 +185,7 @@ class Chan {
|
|
184
185
|
const serviceName = file.replace(".js", "");
|
185
186
|
//模块文件夹-模块文件名-服务-方法
|
186
187
|
Chan[modules][moduleName].service[serviceName] = {};
|
187
|
-
Chan[modules][moduleName].service[serviceName] = Service;
|
188
|
+
Chan[modules][moduleName].service[serviceName] = bindClass(Service);
|
188
189
|
}
|
189
190
|
}
|
190
191
|
}
|
@@ -206,7 +207,7 @@ class Chan {
|
|
206
207
|
const controller = require(path.join(controllersDir, file));
|
207
208
|
const controllerName = file.replace(".js", "");
|
208
209
|
Chan[modules][moduleName].controller[controllerName] = {};
|
209
|
-
Chan[modules][moduleName].controller[controllerName] = controller;
|
210
|
+
Chan[modules][moduleName].controller[controllerName] = bindClass(controller);
|
210
211
|
}
|
211
212
|
}
|
212
213
|
}
|
package/core/lib/core.js
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
/**
|
2
|
+
* @description 实例化一个类,并将该类的所有方法绑定到一个新的对象上。
|
3
|
+
* @param {Function} className - 需要实例化的类。
|
4
|
+
*@returns {Object} 包含绑定方法的对象。
|
5
|
+
*/
|
6
|
+
function bindClass(className) {
|
7
|
+
let obj = {};
|
8
|
+
const cls = new className();
|
9
|
+
Object.getOwnPropertyNames(cls.constructor.prototype).forEach(
|
10
|
+
(methodName) => {
|
11
|
+
if (
|
12
|
+
methodName !== "constructor" &&
|
13
|
+
typeof cls[methodName] === "function"
|
14
|
+
) {
|
15
|
+
obj[methodName] = cls[methodName].bind(cls);
|
16
|
+
}
|
17
|
+
}
|
18
|
+
);
|
19
|
+
return obj;
|
20
|
+
}
|
21
|
+
|
22
|
+
|
23
|
+
module.exports = bindClass;
|