helpers.js 1.37 KB
/**
 * Handlebars helpers
 * bikai kai.bi@yoho.cn
 * 2016-05-10
 */
'use strict';
const querystring = require('querystring');
const _ = require('lodash');
const config = require('../config/common');

/**
 * 七牛图片路径处理
 * @param  {[tring]} url
 * @param  {[tring]} width
 * @param  {[tring]} height
 * @param  {[tring]} mode
 * @return {[tring]}
 */
exports.image = (url, width, height, mode) => {
    mode = _.isNumber(mode) ? mode : 1;
    return url.replace(/{width}/g, width).replace(/{height}/g, height).replace(/{mode}/g, mode);
};

/**
 * 站内地址格式化
 * @param  {[string]} uri 路径
 * @param  {[object]} qs 查询字符串
 * @param  {[string]} module 模块
 * @return {[string]}
 */
exports.url = (uri, qs, module) => {
    const subDomain = '.m.yohobuy.com';
    const subName = {
        default: config.siteUrl,
        guang: '//guang' + subDomain,
        list: '//list' + subDomain,
        search: '//search' + subDomain,
        huodong: '//huodong' + subDomain,
        activity: '//activity.yohobuy.com',
        index: config.siteUrl
    };
    let url;

    module = module || 'default';
    if (subName[module]) {
        url = subName[module];
    } else {
        url = '//' + module + subDomain; // 规则没匹配到就把模块当作子域名
    }

    url += uri;
    if (qs) {
        url += '?' + querystring.stringify(qs);
    }

    return url;
};