Blame view

doraemon/middleware/set-yoho-data.js 1.26 KB
ccbikai authored
1 2 3 4 5 6 7
/**
 * 设置 YOHO 数据
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/6/16
 */

'use strict';
8
const helpers = global.yoho.helpers;
ccbikai authored
9 10 11 12 13 14

module.exports = () => {
    return (req, res, next) => {
        let yoho = {
            pageChannel: {}
        };
zhangxiaoru authored
15
ccbikai(👎🏻🍜) authored
16
        let ua = (req.get('User-Agent') || '').toLowerCase();
zhangxiaoru authored
17
ccbikai authored
18 19
        const channel = req.query.channel || req.cookies._Channel || 'boys';
20
        // 用于头部颜色控制
ccbikai authored
21
        yoho.pageChannel[channel] = true;
22 23 24 25 26

        // 当前频道设置
        yoho.channel = channel;

        // 判断请求是否来自app
ccbikai authored
27
        yoho.isApp = req.query.app_version || req.query.appVersion;
28
        yoho.isWechat = /micromessenger/i.test(req.get('User-Agent') || '');
zhangxiaoru authored
29
        yoho.isWeibo = ua.indexOf('weibo') !== -1;
zhangxiaoru authored
30
        yoho.isqq = /MQQBrowser/i.test(req.get('User-Agent') || '');
ccbikai authored
31 32 33

        Object.assign(res.locals, yoho);
        Object.assign(req.yoho, yoho);
34
35 36 37 38 39
        // App 内请求支持跨域
        if (yoho.isApp) {
            res.set('Access-Control-Allow-Origin', '*');
        }
沈志敏 authored
40 41
        res.locals.cartUrl = helpers.urlFormat('/cart/index/index'); // 悬挂购物车
        res.locals.indexUrl = helpers.urlFormat('/?go=1'); // 悬挂首页
42
        res.locals.showHeader = true;
43
ccbikai authored
44 45 46
        next();
    };
};