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

'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const productPrcs = require('./product-process');

/**
 * 将商品转化成以 product_skn 为键名的对象
 * @param {*} goodsArray
 */
const _goodsArrayToObj = (goodsArray) => {
    return _.keyBy(goodsArray, value => {
        return value.product_skn;
    });
};

/**
 * [将首字符为//的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(item => url.includes(item)) &&
        blackDomains.every(item => !url.includes(item))) {
        url = url.replace('http://', '//');
    }

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

        filters.forEach(item => {
            url = url.subString(0, url.lastIndexOf(item));
        });
        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]
 * @return {[array | false]}
 */
const formatArticle = (articleData, showTag, isApp, showAuthor, uid) => {
    // 资讯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, //eslint-disable-line
        title: articleData.title,
        text: articleData.intro,
        publishTime: articleData.publish_time,
        publishTimeLong: articleData.publish_time_long,
        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 colparam = articleData.colparam || {
            urlpath: '',
            param: ''
        };
        let originUrl = helpers.urlFormat(colparam.urlpath || '/author/index', null, 'guang'); // 跳转回的链接
        // 根据用户是否登录做处理的链接
        let collectUrl = 'javascript:;'; // eslint-disable-line  

        if (!uid) {
            let playUrlEncode = `${originUrl}${colparam.param}`.replace(/\//g, '\\\/');

            collectUrl = `${originUrl}?openby:yohobuy={"action":"go.weblogin","params":{"jumpurl":{"url":"${playUrlEncode}","param":{"from":"app"}},"requesturl":{"url":"","param":{}},"priority":"N"}}`; //eslint-disable-line
        }
        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}"}}`; //eslint-disable-line
    }

    // 判断是否显示作者信息
    if (showAuthor && articleData.author) {
        if (!isApp) {
            articleData.author.url = getFilterUrl(articleData.author.url);
        } else {
            // 编辑人员 app跳转url处理 20160601
            let isLogin = uid ? true : false;

            articleData.author.url = `${helpers.https(articleData.author.url)}&openby:yohobuy={"action":"go.h5","params":{"param":{},"share":"","id":${articleData.author.author_id},"type":0,"islogin":"${isLogin}","url":"${articleData.author.url}"}}`; //eslint-disable-line
        }

        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;
            default:
                break;
        }
    }

    // 170503 新添加商品列表模块
    if (articleData && articleData.product_list) {
        result.productList = [];

        _.forEach(articleData.product_list, value => {
            result.productList.push(_.assign(value, {
                price: parseFloat(value.sales_price).toFixed(2),
                href: `//m.yohobuy.com/product/${value.product_skn}.html`
            }));
        });
    }

    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] || '';
};

/**
 * 逛详情页数据处理
 */
const processArticleDetail = (articleContent, isApp, gender, isWeixin, isqq, isWeibo) => {
    let finalDetail = [];
    let allgoods = '';

    _.forEach(articleContent, (value, index) => {

        // 文字
        if (_.get(value, 'text.data.text', false)) {
            finalDetail.push({
                text: value.text.data.text
            });
        }

        // 一张图片
        if (_.get(value, 'singleImage.data.length', false)) {
            let tagList = _.get(value, 'singleImage.data[0].tagList', []);

            if (tagList.length > 0) {
                _.forEach(tagList, (tag, tagIndex) => {

                    // 链接
                    tagList[tagIndex].href = '//m.yohobuy.com/product/' + tag.product_skn + '.html';
                    tagList[tagIndex].isApp = isApp;
                });
            }

            finalDetail.push({
                bigImage: helpers.image(_.get(value, 'singleImage.data[0].src', ''), 640, 640),
                noLazy: index <= 3,
                tagList: tagList
            });
        }

        // 两张小图
        if (_.get(value, 'smallPic.data.length', 0) > 1) {
            finalDetail.push({
                smallImage: [
                    {
                        src: helpers.image(_.get(value, 'smallPic.data[0].src', ''), 315, 420)
                    },
                    {
                        src: helpers.image(_.get(value, 'smallPic.data[1].src', ''), 315, 420)
                    }
                ],
            });
        }

        // 微信公众号?
        if (_.get(value, 'weixinPublic', false)) {
            finalDetail.push({
                weixinPublic: _.get(value, 'weixinPublic', '')
            });
        }

        // 分享?
        if (_.get(value, 'shareCode', false)) {
            let shareCode = false;

            if (isWeixin) {
                shareCode = _.get(value, 'shareCode.wechatShareImgUrl', false);
            }

            if (isqq) {
                shareCode = _.get(value, 'shareCode.qqShareImgUrl', false);
            }

            if (isWeibo) {
                shareCode = _.get(value, 'shareCode.showShareImgUrl', false);
            }

            if (shareCode) {
                finalDetail.push({
                    shareCode: [{
                        codeShare: shareCode
                    }]
                });
            }
        }

        // 相关商品 170505 更改了前端样式
        if (_.get(value, 'goods.data', false)) {
            let goodsData = _.get(value, 'goods.data', []);

            _.forEach(goodsData, item => {
                allgoods += item.id + ',';
            });

            finalDetail.push({
                relatedReco: {
                    isApp: isApp,
                    goods: goodsData,
                    moreNum: goodsData.length - 2 > 0 ? goodsData.length - 2 : 0
                }
            });
        }

        // 悬停浮动商品 170505 后面版本不支持配置此资源,重构可忽略此部分
        if (_.get(value, 'goodsGroup.data', false)) {
            let collocation = [];

            _.forEach(_.get(value, 'goodsGroup.data', []), item => {
                let cover = _.get(item, 'cover', '');

                _.forEach(_.get(item, 'list', []), item2 => {
                    allgoods += item2.id + ',';
                });

                collocation.push({
                    thumb: cover ?
                        helpers.image(cover.cover, 235, 314) : '',
                    type: cover ? getProductIcon(cover.maxSortId) : '',
                    goods: _.get(item, 'list', [])
                });
            });

            finalDetail.push({
                collocation: collocation
            });
        }

        // 更多商品链接
        if (_.get(value, 'link', false)) {
            finalDetail.push({
                moreLink: _.get(value, 'link.data[0].url', '')
            });
        }

        // 170505 新添加推荐商品
        if (_.get(value, 'recommend_products', false)) {
            let recommendProducts = _.get(value, 'recommend_products.data', []);

            _.forEach(recommendProducts, (goods, goodsKey) => {
                recommendProducts[goodsKey].price = parseFloat(goods.sales_price).toFixed(2);
                recommendProducts[goodsKey].href = isApp ? `http://m.yohobuy.com/product/${goods.product_skn}.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":"${goods.product_skn}"}}` : `//m.yohobuy.com/product/${goods.product_skn}.html`;
            });

            finalDetail.push({
                recommendProducts: _.get(value, 'recommend_products.data', [])
            });
        }
    });

    return {
        finalDetail: finalDetail,
        allgoods: allgoods
    };
};

/**
 * 重新获取商品数据
 */
const pushGoodsInfo = (finalDetail, goodsList, isApp) => {
    let goodsObj = _goodsArrayToObj(productPrcs.processProductList(goodsList, { isApp: isApp }));

    _.forEach(finalDetail, (value, key) => {
        if (value.relatedReco) {
            _.forEach(value.relatedReco.goods, (item, subKey) => {
                finalDetail[key].relatedReco.goods[subKey] = goodsObj[item.id];
            });
        }

        if (value.collocation) {
            _.forEach(value.collocation, (item, subKey) => {
                _.forEach(item.goods, (thItem, thKey) => {
                    finalDetail[key].collocation[subKey].goods[thKey] = goodsObj[thItem.id];
                });
            });
        }
    });

    return finalDetail;
};

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