yoho.js 1.21 KB
const url = require('url');
const _ = require('lodash');
const md5 = require('yoho-md5');

const yoho = {
    getSign(params, clientSecret) {
        let secretParams = {},
            secretStr = '';

        for (const k of Object.keys(params).sort()) {
            if (k === 'yh_sign') {
                continue;
            }
            secretParams[k] = params[k];
        }
        secretStr = _.join(_.map(secretParams, (v, k) => {
            return `${k}=${v}`;
        }), '&');
        return md5(secretStr + clientSecret);
    },

    /**
     *   refer限制
     *  @param referer string
     *      1. array: ['/login', '/signin'] 如果referer 在array中,将返回 /home
     *      2. function: 如果返回true, 返回/home
     *  @return referer
     */
    refererLimit(referer) {  // eslint-disable-line
        let result;

        try {
            result = decodeURIComponent(referer || '/home');
        } catch (e) {
            result = referer || '/home';
        }

        let urlObj = url.parse(result, false, true);

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

module.exports = yoho;