forked from zhongkai/wxtodo-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
62 lines (53 loc) · 1.28 KB
/
util.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
var moment = require('moment');
var config = require('./config');
var sessionTable = 'session';
var mysql = require('knex')({
client: 'mysql',
connection: {
host: config.mysql.host,
port: config.mysql.port,
user: config.mysql.user,
password: config.mysql.pass,
database: config.mysql.db,
charset: config.mysql.char
}
});
var loginCheckMiddleware = function (req, res, next) {
var skey = req.headers.skey;
req.session = null;
if(!skey) {
next();
return;
}
mysql(sessionTable).select('*').where({
skey: skey
})
.then(function (result) {
if (result.length > 0) {
var session = result[0];
var lastLoginTime = session.last_login_time;
var expireTime = config.expireTime * 1000;
if (moment(lastLoginTime, 'YYYY-MM-DD HH:mm:ss').valueOf() + expireTime > +new Date) {
req.session = session;
}
}
next();
})
.catch(function (e) {
next();
});
};
function only(obj, keys) {
obj = obj || {};
if ('string' == typeof keys) keys = keys.split(/ +/);
return keys.reduce(function (ret, key) {
if (null == obj[key]) return ret;
ret[key] = obj[key];
return ret;
}, {});
};
module.exports = {
mysql: mysql,
loginCheckMiddleware: loginCheckMiddleware,
only
};