display.js
1.38 KB
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
/**
* 渲染布局
* @author: yyq<yanqing.yang@yoho.cn>
* @date: 2016/7/4
*/
'use strict';
const _ = require('lodash');
const header = require('../models/header');
const helpers = global.yoho.helpers;
module.exports = () => {
return (req, res, next) => {
res.display = (path, data) => {
let isHead = true;
data.siteUrl = helpers.urlFormat('');
// 判断是否隐藏公共布局
if (_.isBoolean(data.layout) && !data.layout) {
isHead = false;
}
// 判断是否显示默认头部
if (_.isBoolean(data.defaultHeader) && !data.defaultHeader) {
_.unset(data, 'defaultHeader');
isHead = false;
}
if (!isHead) {
// 登录状态
if (req.user.uid) {
data.userName = req.user.username;
}
res.render(path, data);
} else {
header.requestHeaderData(req.yoho.channel).then(result => {
Object.assign(data, result);
// 登录状态
if (req.user.uid) {
data.pageHeader.userName = req.user.username;
}
res.render(path, data);
}).catch(next);
}
};
next();
};
};