-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
127 lines (117 loc) · 3.35 KB
/
routes.js
1
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* 路由功能
*/
var fs=require('fs');
var util = require('./services/util');
var obj = require('./services/obj');
var settings = require('./settings');
var user = require('./models/user');
module.exports = function(app) {
// simple logger
var logger=function(req, res, next){
console.log('%s %s', req.method, req.url);
next();
};
//全局参数,用于认引擎加入
var resbefore=function(req,res,next){
if (req.session.user) {
//req.session.user.name = 'mrlong';
res.locals.user = req.session.user;
res.locals.path = "'" + req.path + "'";
return next();
}
else{
var cookie = req.cookies[settings.cookieSecret];
if (!cookie) {
return next();
};
var auth_token = util.decrypt(cookie, settings.sessionSecret);
var auth = auth_token.split('\t');
var user_id = auth[0];
user.getUserById(user_id,function(err,user){
if(!err && user){
user.logincount++;
user.lastlogin = Date.now();
user.save();
req.session.user = user;
//req.session.user.name = 'mrlong'
res.locals.user = user;
return next();
}
else{
return next(err);
}
});
};
//res.locals.user = new obj({name:'mrlong'});
};
//认证
var auths = function(req,res,next) {
//debuger;
if (req.session.user) {
return next();
}
else {
return res.send(util.errBox("你没有登录,请登录!",'/login'));
}
};
//路由加载
fs.readdir(app.get("controllers"), function(err, files){
if(!err){
files.forEach(function(item) {
var tmpPath = app.get("controllers") + '/' + item;
debugger;
if (fs.statSync(tmpPath).isFile() && tmpPath.indexOf('.cjs')>0){
loadcontrollers(tmpPath);
} //isFile()
//二级目录
else{
fs.readdir(tmpPath,function(err,files){
if(!err){
files.forEach(function(item) {
var tmpPath2 = tmpPath + '/' + item;
if (fs.statSync(tmpPath2).isFile()&& tmpPath2.indexOf('.cjs')>0){
loadcontrollers(tmpPath2);
}
//三级目录
else{
fs.readdir(tmpPath2,function(err,files){
if(!err){
files.forEach(function(item){
var tmpPath3 = tmpPath2 + '/' + item;
if(fs.statSync(tmpPath3).isFile()&& tmpPath3.indexOf('.cjs')>0){
loadcontrollers(tmpPath3);
};
});
};
});
};
//end 三级目录
})
}
});
}//end 二级目录
});
}
});
//end 路由加载
//错误
app.use(function (req, res) {
res.render("404");
});
function loadcontrollers(filepath){
var a = require(filepath);
new a(app).route.forEach(function(val){
var mid=[logger,resbefore];
if (typeof(val.auth)=='function'){
mid[mid.length]=val.auth;
}
else if(val.auth===true){
mid[mid.length]=auths;
};
if(val.get){app.get(val.url,mid,val.get);};
if(val.post){app.post(val.url,mid,val.post);};
});
};
//end loadcontrollers
};