product-detail.js 6.53 KB
'use strict';

const $ = require('cheerio');
const _ = require('lodash');
const mipUtils = require('../mip-utils');

class ProductDetail extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    baseInfo(params) {
        let options = {
            data: {
                method: 'app.product.data',
                product_skn: params.productSkn
            },
            param: {
                code: 200,
                cache: true
            }
        };

        return this.get(options).then(result => {
            let brandId = _.get(result, 'data.brand_id');
            let shopId = _.get(result, 'data.shop_id');
            let optionsParams = {
                data: {
                    method: 'app.product.queryShopsInfoById',
                    brand_id: brandId,
                    shop_id: shopId
                },
                param: {
                    cache: true
                }
            };

            return this.get(optionsParams).then(data => {
                if (result.data) {
                    result.data.enterStore = _.get(data, 'data');
                }
                return result;
            });
        });
    }

    intro(params) {
        let options = {
            data: {
                method: 'app.product.intro',
                product_skn: params.productSkn,
                app_version: '5.6.0'
            },
            param: {
                cache: true
            }
        };

        return this.get(options).then(result => {
            result = _.isString(result) ? result : '';
            result = $.load(result);

            result = result('#productDesc');

            return (result.html() || '');
        });
    }

    preference(params) {
        let options = {
            data: {
                method: 'app.product.preference',
                product_skn: params.productSkn
            },
            param: {
                cache: true
            }
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    promotion(params) {
        let options = {
            data: {
                method: 'app.product.promotion',
                product_skn: params.productSkn
            },
            param: {
                cache: true
            }
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    index(params) {
        return Promise.all([
            this.baseInfo(params),
            this.intro(params),
            this.preference(params),
            this.promotion(params)
        ]).then(result => {
            let resu = {
                baseInfo: {},
                intro: '',
                preference: '',
                promotion: [],
                enterStore: [],
                shoppingCount: '',
                title: '',
                mipCss: ''
            };
            let localCss = [];

            if (_.get(result, '[0].data')) {
                result[0].data.sales_price = result[0].data.sales_price.toFixed(2);
                result[0].data.market_price = result[0].data.market_price.toFixed(2);
                if (result[0].data.sales_price === result[0].data.market_price) {
                    result[0].data.market_price = 0;
                }
                resu.baseInfo = result[0].data;

                let picBuild = [];
                let build = [];
                let tags = {};

                _.forEach(result[0].data.tags, (value) => {
                    tags[value] = value;
                });

                resu.baseInfo.tags = tags;

                _.forEach(result[0].data.goods_list, (value) => {
                    _.forEach(value.images_list, (item) => {
                        picBuild.push({
                            image_url: item.image_url
                        });
                    });
                });
                resu.baseInfo.images_list = picBuild;

                _.forEach(result[0].data.enterStore, function(value) {
                    build.push({
                        storeName: value.brand_name,
                        url: `/shop/${value.brand_domain}-${value.shop_id}.html`,
                        img: value.brand_ico
                    });
                });
                resu.enterStore = build;
                let brandName = '';

                if (_.get(resu, 'baseInfo.brand_info.brand_name')) {
                    brandName = resu.baseInfo.brand_info.brand_name;
                }
                resu.title = `${resu.baseInfo.product_name}_${brandName}${resu.baseInfo.middle_sort_name}`;
            }

            if (_.get(result, '[1]')) {
                result[1] = _.toString(result[1]).replace(/http:/g, `${params.protocol}:`);
                resu.intro = mipUtils.process(result[1]).mipHtml;
                localCss.push(mipUtils.process(result[1]).css);
            }

            if (_.get(result, '[2]')) {
                result[2] = _.toString(result[2]).replace(/http:/g, `${params.protocol}:`);

                // 处理a标签
                let option = {
                    mipLink: true
                };

                let resultHtml = _.isString(result[2]) ? result[2] : '';
                let goodsContainer = $.load(resultHtml)('#shop-goods-container');
                let goodThumb = goodsContainer.find('.good-thumb');
                let nameA = goodsContainer.find('.name a');

                _.forEach(goodThumb, function(value) {
                    let href = $(value).attr('href').split('?')[0];
                    let skn = $(value).closest('.good-info').data('id');

                    if (skn) {
                        href = `//m.yohobuy.com/product/${skn}.html?union_type=100000000013130`;
                    }
                    $(value).attr('href', href);
                });

                _.forEach(nameA, function(value) {
                    let href = $(value).attr('href').split('?')[0];
                    let skn = $(value).closest('.good-info').data('id');

                    if (skn) {
                        href = `${params.protocol}://m.yohobuy.com/product/${skn}.html?union_type=100000000013130`;
                    }
                    $(value).attr('href', href);
                });

                resu.preference = mipUtils.process(goodsContainer.html(), option).mipHtml;
            }

            if (_.get(result, '[3].data')) {
                resu.promotion = result[3].data;
            }

            resu.mipCss = localCss.join('');
            return resu;
        });
    }
}

module.exports = ProductDetail;