display.js 1.38 KB
/**
 * 渲染布局
 * @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();
    };
};