chanjs 1.0.18 → 1.0.21

Sign up to get free protection for your applications and to get access to all the features.
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: "mysql2",
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
  }
@@ -6,4 +6,8 @@ class Base{
6
6
  log(...args){
7
7
  console.log(...args);
8
8
  }
9
+
10
+ success(){
11
+
12
+ }
9
13
  }
package/core/lib/core.js CHANGED
@@ -33,7 +33,7 @@ module.exports = async function (app) {
33
33
 
34
34
  //设置头信息
35
35
  app.use((req, res, next) => {
36
- res.setHeader("MVC", "Chanjs");
36
+ res.setHeader("mvc", "Chanjs");
37
37
  res.setHeader("X-Powered-By", appName);
38
38
  res.setHeader(appName, version);
39
39
  next();
@@ -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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chanjs",
3
- "version": "1.0.18",
3
+ "version": "1.0.21",
4
4
  "description": "Chan.js基于express 纯js研发的轻量级mvc框架。",
5
5
  "main": "core/chan.js",
6
6
  "module": "core/chan.js",