chanjs 1.0.20 → 1.0.22

Sign up to get free protection for your applications and to get access to all the features.
package/core/chan.js CHANGED
@@ -1,9 +1,13 @@
1
1
  const express = require("express");
2
2
  const config = require("./lib/config/config.js");
3
+ const bindClass = require("./lib/extend/bindClass.js");
4
+ const Controller = require("./lib/controller/controller.js")
5
+ const Service = require("./lib/service/service.js")
3
6
  const path = require("path");
4
7
  const fs = require("fs");
5
8
  const core = require("./lib/core.js");
6
- const cors = require('cors')
9
+ const cors = require('cors');
10
+
7
11
  /**
8
12
  * @description 基于express封装的mvc框架,遵循约定优于配置原则
9
13
  */
@@ -17,6 +21,10 @@ class Chan {
17
21
  Chan.plugins ={};
18
22
  //工具
19
23
  Chan.helper ={};
24
+
25
+ //控制器基类
26
+ Chan.Controller = Controller;
27
+
20
28
  //应用实例
21
29
  this.app = express();
22
30
  this.app.config = Chan.config;
@@ -29,6 +37,7 @@ class Chan {
29
37
  this.loadCore();
30
38
  //加载实例化数据库
31
39
  this.loadKnex();
40
+
32
41
  //生命周期钩子:开始启动
33
42
  this.beforeStart();
34
43
  //解决跨域
@@ -73,9 +82,9 @@ class Chan {
73
82
  //数据库操作
74
83
  loadKnex(){
75
84
  // 连接数据库
76
- const {host,port,user,password,database,client,charset} = Chan.config.database;
85
+ const {host,port,user,password,database,client='mysql2',charset} = Chan.config.database;
77
86
  const knex = require("knex")({
78
- client: "mysql2",
87
+ client: client,
79
88
  connection: {
80
89
  host,
81
90
  port,
@@ -99,7 +108,10 @@ class Chan {
99
108
  },
100
109
  },
101
110
  });
111
+ //数据库操作实例
102
112
  Chan.knex = knex;
113
+ //数据库操作基类
114
+ Chan.Service = Service(knex);
103
115
  }
104
116
 
105
117
  //开始启动
@@ -184,7 +196,7 @@ class Chan {
184
196
  const serviceName = file.replace(".js", "");
185
197
  //模块文件夹-模块文件名-服务-方法
186
198
  Chan[modules][moduleName].service[serviceName] = {};
187
- Chan[modules][moduleName].service[serviceName] = Service;
199
+ Chan[modules][moduleName].service[serviceName] = bindClass(Service);
188
200
  }
189
201
  }
190
202
  }
@@ -206,7 +218,7 @@ class Chan {
206
218
  const controller = require(path.join(controllersDir, file));
207
219
  const controllerName = file.replace(".js", "");
208
220
  Chan[modules][moduleName].controller[controllerName] = {};
209
- Chan[modules][moduleName].controller[controllerName] = controller;
221
+ Chan[modules][moduleName].controller[controllerName] = bindClass(controller);
210
222
  }
211
223
  }
212
224
  }
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @description
3
+ * 控制器基类
4
+ */
5
+ class Controller{
6
+
7
+ constructor(){
8
+
9
+ }
10
+
11
+ download(res,file){
12
+ res.download(file)
13
+ }
14
+
15
+ success(res,data=null){
16
+ res.json({code:200,msg:'success',data:data})
17
+ }
18
+
19
+ fail(res,msg='操作失败,请稍后再试'){
20
+ res.json({code:400,msg:msg})
21
+ }
22
+
23
+ error(res,msg){
24
+ res.json({code:500,msg:msg})
25
+ }
26
+ }
27
+
28
+
29
+
30
+ module.exports = Controller;
@@ -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;
@@ -0,0 +1,32 @@
1
+ class BaseService {
2
+ constructor(knex, model='') {
3
+ this.knex = knex;
4
+ this.model = model;
5
+ }
6
+
7
+ all() {
8
+ return this.knex(this.model).select();
9
+ }
10
+
11
+ detail(id) {
12
+ return this.knex(this.model).where('id', id).select();
13
+ }
14
+
15
+ insert(params) {
16
+ return this.knex(this.model).insert(params);
17
+ }
18
+
19
+ update(id, params) {
20
+ return this.knex(this.model).where('id', id).update(params);
21
+ }
22
+
23
+ delete(id) {
24
+ return this.knex(this.model).where('id', id).del();
25
+ }
26
+ }
27
+
28
+ const Service = (knex, model='') => {
29
+ return new BaseService(knex, model);
30
+ };
31
+
32
+ module.exports = Service;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chanjs",
3
- "version": "1.0.20",
3
+ "version": "1.0.22",
4
4
  "description": "Chan.js基于express 纯js研发的轻量级mvc框架。",
5
5
  "main": "core/chan.js",
6
6
  "module": "core/chan.js",
@@ -1,9 +0,0 @@
1
- class Base{
2
- constructor(config){
3
- this.config = config;
4
- }
5
-
6
- log(...args){
7
- console.log(...args);
8
- }
9
- }
@@ -1,217 +0,0 @@
1
-
2
- /**
3
- * extend controller
4
- */
5
- module.exports = {
6
- /**
7
- * body getter
8
- */
9
- get body() {
10
-
11
- },
12
- /**
13
- * body setter
14
- */
15
- set body(value) {
16
-
17
- },
18
- /**
19
- * get client ip
20
- */
21
- get ip() {
22
-
23
- },
24
- /**
25
- * get client ips
26
- */
27
- get ips() {
28
-
29
- },
30
- /**
31
- * get status code
32
- */
33
- get status() {
34
-
35
- },
36
- /**
37
- * set status code
38
- */
39
- set status(status) {
40
-
41
- },
42
- /**
43
- * get content type
44
- */
45
- get type() {
46
-
47
- },
48
- /**
49
- * set content type
50
- */
51
- set type(contentType) {
52
-
53
- },
54
- /**
55
- * get userAgent header
56
- */
57
- get userAgent() {
58
-
59
- },
60
- /**
61
- * get request method
62
- */
63
- get method() {
64
-
65
- },
66
- /**
67
- * is get method
68
- */
69
- get isGet() {
70
-
71
- },
72
- /**
73
- * is post method
74
- */
75
- get isPost() {
76
-
77
- },
78
- /**
79
- * is command line invoke
80
- */
81
- get isCli() {
82
-
83
- },
84
- /**
85
- * get or set config
86
- * @param {String} name
87
- * @param {Mix} value
88
- * @param {String} m
89
- */
90
- config(name, value, m ) {
91
-
92
- },
93
- /**
94
- * is method
95
- * @param {String} method
96
- */
97
- isMethod(method) {
98
-
99
- },
100
- /**
101
- * check if is ajax request
102
- * @param {String} method
103
- */
104
- isAjax(method) {
105
-
106
- },
107
- /**
108
- * is jsonp request
109
- * @param {String} callback
110
- */
111
- isJsonp(callbackField) {
112
-
113
- },
114
- /**
115
- * send jsonp data
116
- */
117
- jsonp(data, callbackField) {
118
-
119
- },
120
- /**
121
- * send json data
122
- */
123
- json(data) {
124
-
125
- },
126
- /**
127
- * send success data
128
- */
129
- success(data, message) {
130
-
131
- },
132
- /**
133
- * send fail data
134
- */
135
- fail(errno, errmsg, data) {
136
-
137
- },
138
- /**
139
- * set expires header
140
- * @param {Number} time
141
- */
142
- expires(time) {
143
-
144
- },
145
- /**
146
- * get query data
147
- * @param {String} name
148
- * @param {Mixed} value
149
- */
150
- get(name, value) {
151
-
152
- },
153
- /**
154
- * get query data
155
- * @param {String} name
156
- * @param {Mixed} value
157
- */
158
- query(name, value) {
159
-
160
- },
161
- /**
162
- * get or set post data
163
- * @param {String} name
164
- * @param {Mixed} value
165
- */
166
- post(name, value) {
167
-
168
- },
169
- /**
170
- * get or set file data
171
- * @param {String} name
172
- * @param {Mixed} value
173
- */
174
- file(name, value) {
175
-
176
- },
177
- /**
178
- * get or set cookies
179
- * @param {String} name
180
- * @param {String} value
181
- * @param {Object} options
182
- */
183
- cookie(name, value, options) {
184
-
185
- },
186
-
187
- /**
188
- * get referer header
189
- */
190
- referrer(onlyHost) {
191
-
192
- },
193
- /**
194
- * get referer header
195
- */
196
- referer(onlyHost) {
197
-
198
- },
199
- /**
200
- * Perform a 302 redirect to `url`.
201
- * @param {String} url
202
- * @param {String} alt
203
- */
204
- redirect(url, alt) {
205
-
206
- },
207
-
208
-
209
- /**
210
- * download
211
- * @param {String} filepath
212
- * @param {String} filename
213
- */
214
- download(filepath, filename) {
215
-
216
- }
217
- };
File without changes