chanjs 1.0.21 → 1.0.22
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
CHANGED
@@ -1,10 +1,13 @@
|
|
1
1
|
const express = require("express");
|
2
2
|
const config = require("./lib/config/config.js");
|
3
3
|
const bindClass = require("./lib/extend/bindClass.js");
|
4
|
+
const Controller = require("./lib/controller/controller.js")
|
5
|
+
const Service = require("./lib/service/service.js")
|
4
6
|
const path = require("path");
|
5
7
|
const fs = require("fs");
|
6
8
|
const core = require("./lib/core.js");
|
7
|
-
const cors = require('cors')
|
9
|
+
const cors = require('cors');
|
10
|
+
|
8
11
|
/**
|
9
12
|
* @description 基于express封装的mvc框架,遵循约定优于配置原则
|
10
13
|
*/
|
@@ -18,6 +21,10 @@ class Chan {
|
|
18
21
|
Chan.plugins ={};
|
19
22
|
//工具
|
20
23
|
Chan.helper ={};
|
24
|
+
|
25
|
+
//控制器基类
|
26
|
+
Chan.Controller = Controller;
|
27
|
+
|
21
28
|
//应用实例
|
22
29
|
this.app = express();
|
23
30
|
this.app.config = Chan.config;
|
@@ -30,6 +37,7 @@ class Chan {
|
|
30
37
|
this.loadCore();
|
31
38
|
//加载实例化数据库
|
32
39
|
this.loadKnex();
|
40
|
+
|
33
41
|
//生命周期钩子:开始启动
|
34
42
|
this.beforeStart();
|
35
43
|
//解决跨域
|
@@ -100,7 +108,10 @@ class Chan {
|
|
100
108
|
},
|
101
109
|
},
|
102
110
|
});
|
111
|
+
//数据库操作实例
|
103
112
|
Chan.knex = knex;
|
113
|
+
//数据库操作基类
|
114
|
+
Chan.Service = Service(knex);
|
104
115
|
}
|
105
116
|
|
106
117
|
//开始启动
|
@@ -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,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,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
|