Blame view

utils/index.js 974 Bytes
1 2 3 4 5 6 7 8 9 10 11 12 13
'use strict';

const url = require('url');

/**
 *   refer限制
 *  @param referer string
 *  @param blacklist [array|function] refer黑名单  TODO: 未实现
 *      1. array: ['/login', '/signin'] 如果referer 在array中,将返回 /home
 *      2. function: 如果返回true, 返回/home
 *  @return referer
 */
exports.refererLimit = (referer, blacklist) => {  // eslint-disable-line
14 15 16 17 18 19 20
    let result;

    try {
        result = decodeURIComponent(referer || '/home');
    } catch (e) {
        result = referer || '/home';
    }
21
22
    let urlObj = url.parse(result, false, true);
23 24 25 26 27 28 29 30 31 32


    if (urlObj.hostname && !/(?:yohobuy\.com$)|(?:yoho\.cn$)/.test(urlObj.hostname)) {
        result = '/home';
    }

    // TODO: blacklist;

    return result;
};
陈峰 authored
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

/**
 * 对象字段排序
 */
exports.mapSort = obj => {
    if (!obj) {
        return {};
    }
    let data = {};

    Object.keys(obj).sort().forEach(k => {
        data[k] = obj[k];
    });

    return data;
};