display.js 1.14 KB
/**
 * 渲染布局
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/7/4
 */

'use strict';
const _ = require('lodash');
const header = require('../models/header');

module.exports = () => {
    return (req, res, next) => {
        res.display = (path, data) => {
            let isHead = true;

            // 判断是否隐藏公共布局
            if (_.isBoolean(data.layout) && !data.layout) {
                isHead = false;
            }

            // 判断是否显示默认头部
            if (_.isBoolean(data.defaultHeader) && !data.defaultHeader) {
                _.unset(data, 'defaultHeader');
                isHead = false;
            }

            if (!isHead) {
                res.render(path, data);
            } else {
                header.requestHeaderData('boys').then(result => {
                    Object.assign(data, result);
                    // 登录状态
                    if (req.user.uid) {
                       data.pageHeader.userName = req.user.username; 
                    }

                    res.render(path, data);
                }).catch(next);
            }
        };

        next();
    };
};