/
index.js
43 lines (36 loc) · 1.02 KB
/
index.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
module.exports = function () {
return function (req, res, next) {
if (req.flash) {
return next();
} else {
req.flash = _flash;
next();
}
};
};
/**
* 设置flash方法,用于redirect后获取重定向前保存的临时数据
* @Author fisher<wangjiang.fly.1989@163.com>
* @DateTime 2016-03-29T14:19:34+0800
* @param {[type]} key
* @param {[type]} msgs key对应要保存的消息内容,此值不传入时,表示取key对应的消息内容返回
* @return {Object}
*/
function _flash (key, msgs) {
if (!this.rSession) {
throw Error('req.flash() middleware require redis3x-session');
}
var flashArr = this.rSession.flash = this.rSession.flash || {};
// 设置内容
if (msgs && key) {
flashArr[key] = msgs;
// 获取内容
} else if (key) {
var re = flashArr[key];
delete flashArr[key];
return re || null;
// 清空内容
} else {
this.rSession.flash = {};
}
}