Blame view

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

'use strict';
郭成尧 authored
8
const _ = require('lodash');
9
const helpers = global.yoho.helpers;
郭成尧 authored
10
const net = require('net');
ccbikai authored
11
郭成尧 authored
12 13 14 15 16 17 18 19 20 21 22 23 24
/**
 * 获取 IP
 * @param {*} req
 */
const _getClientIp = req => {
    let remoteIp = req.get('X-Forwarded-For') || req.get('X-Real-IP') || req.ip;

    if (remoteIp.indexOf(',') > 0) {
        let arr = remoteIp.split(',');

        remoteIp = _.trim(arr[arr.length - 1]);
    }
ccbikai(👎🏻🍜) authored
25 26 27 28
    if (_.startsWith(remoteIp, '10.66.')) {
        remoteIp = req.get('X-Real-IP');
    }
郭成尧 authored
29 30 31 32 33 34 35 36 37
    remoteIp = _.trim(remoteIp);

    if (!net.isIPv4(remoteIp)) {
        let ipv6String = remoteIp.split(':');

        remoteIp = ipv6String[ipv6String.length - 1];
    }

    return remoteIp;
郭成尧 authored
38 39
};
ccbikai authored
40 41
module.exports = () => {
    return (req, res, next) => {
郭成尧 authored
42
        let yoho = Object.assign({
ccbikai authored
43
            pageChannel: {}
郭成尧 authored
44
        }, req.yoho || {});
zhangxiaoru authored
45
ccbikai authored
46 47
        const channel = req.query.channel || req.cookies._Channel || 'boys';
郭成尧 authored
48 49
        // 当前频道设置
        yoho.channel = channel;
郭成尧 authored
50
51
        // 用于头部颜色控制
ccbikai authored
52
        yoho.pageChannel[channel] = true;
53
郭成尧 authored
54 55 56
        // IP 地址
        yoho.clientIp = _getClientIp(req);
yyq authored
57
        yoho.isMobile = /(nokia|iphone|android|ipad|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220)/i.test(req.get('User-Agent') || ''); // eslint-disable-line
58
        yoho.isWechat = /micromessenger/i.test(req.get('User-Agent') || '');
毕凯 authored
59
        yoho.isWeibo = /weibo/i.test(req.get('User-Agent') || '');
zhangxiaoru authored
60
        yoho.isqq = /MQQBrowser/i.test(req.get('User-Agent') || '');
毕凯 authored
61
ccbikai authored
62 63
        Object.assign(res.locals, yoho);
        Object.assign(req.yoho, yoho);
64
65 66 67 68 69
        // App 内请求支持跨域
        if (yoho.isApp) {
            res.set('Access-Control-Allow-Origin', '*');
        }
沈志敏 authored
70 71
        res.locals.cartUrl = helpers.urlFormat('/cart/index/index'); // 悬挂购物车
        res.locals.indexUrl = helpers.urlFormat('/?go=1'); // 悬挂首页
72
        res.locals.showHeader = true;
73
ccbikai authored
74 75 76
        next();
    };
};