guang-process.js 10.8 KB
/**
 * 逛处理类
 */

'use strict';
const helpers = global.yoho.helpers;

/**
 * [将首字符为//的url转换为http://]
 * @param  {[string]} url [需要转换的url]
 * @return {[string]}     [description]
 */
const transHttpsUrl = (url) => {
    return url.replace(/^\/\//g, 'http://');
};

/**
 * [过滤app查询字符串]
 * @param  {[string]} url [url地址]
 * @return {[strng]}
 */
const getFilterUrl = (url) => {
    url = url.replace('.m.yohobuy.com', global.yoho.config.subDomains.host).replace('www.yohobuy.com', global.yoho.config.siteUrl);

    const whiteDomains = ['m.yohobuy.com', 'cdn.yoho.cn/myohobuy'];
    const blackDomains = ['sale.m.yohobuy.com', 'cuxiao.m.yohobuy.com', 'activity.m.yohobuy.com', 'huodong.m.yohobuy.com', '/home/orders/pay'];

    if (whiteDomains.every(_ => url.includes(_)) &&
        blackDomains.every(_ => !url.includes(_))) {
        url = url.replace('http://', '//');
    }

    if (url.includes('feature.yoho.cn')) {
        url = transHttpsUrl(url);
    }
    if (url.includes('openby:yohobuy=')) {
        let filters = ['openby:yohobuy=', '&', '?'];

        filters.forEach(_ => {
            url = url.subString(0, url.lastIndexOf(_));
        });
        return url.replace(/(^\s*)|(\s*$)/g, '');
    } else {
        return url;
    }
};

/**
 * [格式化资讯文章]
 * @param  {[array]}  articleData [需要格式化的资讯数据]]
 * @param  {[Boolean]}  showTag [是否显示左上角标签]]
 * @param  {Boolean} isApp [是否显示分享,在APP客户端里嵌入需要传url链接]
 * @param  {[Boolean]}  showAuthor  [控制是否显示作者信息]
 * @param  {[int]}  uid         [当前登录的用户ID]
 * @param  {[string]}  reqQueryString         [查询字符串]
 * @return {[array | false]}
 */
const formatArticle = (articleData, showTag, isApp, showAuthor, uid, reqQueryString) => {
    // 资讯ID不存在,则不显示
    if (!articleData.id) {
        return false;
    }
    let result = {
        id: articleData.id,
        showTags: showTag,
        img: articleData.src ? helpers.image(articleData.src, 640, 640) : '',
        url: isApp ? `${helpers.https(articleData.url)}&openby:yohobuy={"action":"go.h5","params":{"param":{"id":"${articleData.id}"},"shareparam":{"id":"${articleData.id}"},"share":"/guang/api/v1/share/guang","id":${articleData.id},"type":1,"url":"http:${helpers.urlFormat('/info/index', null, 'guang')}","islogin":"N"}}` : articleData.url,
        title: articleData.title,
        text: articleData.intro,
        publishTime: articleData.publish_time,
        pageView: articleData.views_num
    };

    if (result.url.includes('feature.yoho.cn') ||
        result.url.includes('cdn.yoho.cn')) {
        result.url = transHttpsUrl(result.url);
    }

    // 收藏
    if (isApp) {
        result.collect = [];
        result.collect.isCollected = articleData.isFavor === true;
        let originUrl = helpers.urlFormat('/author/index', null, 'guang') + reqQueryString; // 跳转回的链接
        let collectUrl = 'javascript:;'; // 根据用户是否登录做处理的链接

        if (!uid) {
            let playUrlEncode = originUrl.replace(/\//g, '\\/');

            collectUrl = `${originUrl}?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"${playUrlEncode}","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}`;
        }
        result.collect.url = collectUrl;
    } else { // 点赞
        result.like = {};
        result.like.count = articleData.praise_num;
        result.like.isLiked = articleData.isPraise === 'Y';
    }

    if (isApp && articleData.share.url) {

        // 分享链接
        result.share = `${articleData.share.url}?openby:yohobuy={"action":"go.share","params":{"title":"${articleData.title}","content":"${articleData.intro}","url":"${articleData.share.url}","pic":"https:${result.img}"}}`;
    }

    // 判断是否显示作者信息
    if (showAuthor && articleData.author) {
        if (!isApp) {
            articleData.author.url = getFilterUrl(articleData.author.url);
        }

        // 编辑人员 app跳转url处理 20160601
        let isLogin = uid ? true : false;

        articleData.author.url = `${helpers.https(articleData.author.url)}&openby:yohobuy={"action":"go.h5","params":{"param":{"id":"${articleData.author.author_id}"},"share":"","id":${articleData.author.author_id},"type":0,"islogin":"${isLogin}","url":"https:${helpers.urlFormat('/author/index', {uid: uid}, 'guang')} "}}&uid=${uid}`;
        result.author = articleData.author;
        if (result.author.avatar) {
            result.author.avatar = result.author.avatar.replace('http://', '//');
        }
    }

    // 模板中需要的标签标识
    if (showTag && articleData.category_id) {
        switch (articleData.category_id) {
            case '1': // 话题
                result.isTopic = true;
                break;
            case '2': // 搭配
                result.isCollocation = true;
                break;
            case '3': // 潮人
                result.isFashionMan = true;
                break;
            case '4': // 潮品
                result.isFashionGood = true;
                break;
            case '5': // 小贴士
                result.isTip = true;
                break;
            case '19': // 专题
                result.isSpecialTopic = true;
                break;
        }
    }
    return result;
};

/**
 * [获取商品的ICON]
 * @param  {[int]} type [类型]
 * @return {[type]}
 */
const getProductIcon = (type) => {
    const icons = {
        1: 'cloth',
        3: 'pants',
        4: 'dress',
        6: 'shoe',
        7: 'bag',
        10: 'lamp',
        241: 'headset',
        8: 'watch',
        360: 'swim-suit',
        308: 'under'
    };

    return icons[type] || '';
};

/**
 * [根据性别获取]
 * @param  {[type]} gender [description]
 * @return {[type]}        [description]
 */
const _getGenderByCookie = (gender) => {
    switch (gender) {
        case 1: // 男
            return '1,3';
        case 2: // 女
            return '2,3';
        default: // 其它
            return '1,2,3';
    }
};

/**
 * [根据性别来决定  默认图片获取字段   如果是 2、3]
 *
 * 则优先从cover2 --》 cover1 -- 》 images_url
 * 否则优先从cover1 --》 cover2 -- 》 images_url
 *
 * @param  {[array]} images [images]
 * @param  {[Boolean]} gender [性别]
 * @return {[type]}
 */
const _procProductImg = (images, gender) => {
    let imgUrl = images.images_url || '';
    let cover1 = images.cover_1 || '';
    let cover2 = images.cover_2 || '';

    gender = _getGenderByCookie(gender);
    if (gender === '2,3') {
        return cover2 || (cover1 || imgUrl);
    } else {
        return cover1 || (cover2 || imgUrl);
    }
};

/**
 * 格式化商品信息
 *
 * @param array productData 需要格式化的商品数据
 * @param bool showTags 控制是否显示标签
 * @param bool showNew 控制是否显示NEW图标
 * @param bool showSale 控制是否显示SALE图标
 * @param int width 图片的宽度
 * @param int height 图片的高度
 * @param bool isApp 判断是不是APP访问
 * @param bool showPoint 商品价格是否显示小数位,默认显示
 * @param int gender 性别
 * @return array | false
 */
const formatProduct = (productData, showTags, showNew, showSale, width, height, isApp, showPoint, gender) => {
    // 商品信息有问题,则不显示
    if (!productData.product_skn || !productData.goods_list[0]) {
        return false;
    }
    productData.sales_price = productData.sales_price || '';
    productData.market_price = productData.market_price || '';

    // 市场价和售价一样,则不显示市场价
    if (parseInt(productData.market_price) === parseInt(productData.sales_price)) {
        productData.market_price = false;
    }

    // 判别默认的商品是否将默认的图片URL赋值到skn
    let flag = false;

    // 如果设置了默认图片,就取默认的图片
    for (let oneGoods of productData.goods_list) {
        //  此skc是默认的,则将图片赋值给skn
        if (oneGoods.is_default === 'Y') {
            productData.default_images = _procProductImg(oneGoods, gender);
            flag = true;
            break;
        }
    }

    // 如果还未赋值,则取第一个skc产品的默认图片
    if (!flag) {
        productData.default_images = _procProductImg(productData.goods_list[0], gender);
    }

    let result = {
        id: productData.product_skn,
        product_id: productData.product_id,
        thumb: helpers.image(productData.default_images, width, height),
        name: productData.product_name,
        price: productData.market_price || false,
        salePrice: productData.sales_price
    };

    if (showPoint) {
        if (result.price) {
            result.price += '.00';
        }
        if (result.salePrice) {
            result.salePrice += '.00';
        }
    }

    // TODO student price
    result.studentPrice = parseInt(productData.sales_price * 100 * 0.9) / 100;
    result.is_soon_sold_out = (productData.is_soon_sold_out === 'Y');
    let url = helpers.urlFormat(`/product/pro_${productData.product_id}_${productData.goods_list[0].goods_id}/${productData.cn_alphabet}.html`);

    result.url = url.replace('http://', '//');

    // APP访问需要加附加的参数
    // 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
    if (isApp) {
        result.url += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":${productData.product_skn}}}`;
    }
    if (showTags) {
        result.tags = {
            is_new: showNew && productData.is_new === 'Y', // 新品
            is_discount: showSale && productData.is_discount === 'Y',  // 在售
            is_limited: productData.is_limited && productData.is_limited === 'Y',  // 限量
            is_yohood: productData.is_yohood && productData.is_yohood === 'Y', // YOHOOD
            midYear: productData.midear && productData.midear === 'Y', // 年中
            yearEnd: productData.yearnd && productData.yearnd === 'Y', // 年末
            is_advance: productData.is_advance && productData.is_advance === 'Y' // 再到着
        };

        // 打折与即将售完组合显示打折
        if (result.is_soon_sold_out && result.tags.is_discount) {
            result.tags.is_new = false;
        } else if (result.tags.is_discount &&
            (result.tags.is_new || result.tags.is_limited || result.tags.is_yohood || result.tags.is_advance)// 打折与其它组合则隐藏打折
        ) {
            result.tags.is_discount = false;
        } else if (result.tags.is_yohood && result.tags.is_new) { // YOHOOD和新品组合显示YOHOOD
            result.tags.is_new = false;
        }
    }

    return result;
};


module.exports = {
    formatArticle,
    transHttpsUrl,
    getFilterUrl,
    formatProduct,
    getProductIcon
};