global.js 5.49 KB
/**
 * 商品促销 model
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2017/4/6
 */

'use strict';

const _ = require('lodash');

const helpers = global.yoho.helpers;

const homeService = require('./home-service');
const globalApi = require('./global-api');
const headerModel = require('../../../doraemon/models/header');
const searchHandler = require('./search-handler');
const pager = require(`${global.utils}/pager`).setPager;
const productProcess = require(`${global.utils}/product-process`);

const getGlobalProductListData = (params, yoho) => {
    return Promise.props({
        header: headerModel.requestHeaderData(yoho.channel),
        list: globalApi.getGlobalProductListAsync(Object.assign({
            physical_channel: yoho.channelNum
        }, params, {limit: params.limit ? params.limit - 1 : 59}))
    }).then(result => {
        let resData = {};

        Object.assign(resData, result.header);

        if (result.list.code === 200) {
            let totalNum = _.get(result.list, 'data.total', 0);

            // let filters = Object.assign(searchHandler.handleFilterDataAll(result[2].data, params),
            //     finalResult.list.leftContent.sort);

            // filters.checkedConditions.conditions = _.concat(filters.checkedConditions.conditions,
            //     finalResult.list.leftContent.checked);
            resData.list = {
                // filters: filters,
                // opts: searchHandler.handleOptsData(params, result[2].data.total, result[2].data.filter),
                totalCount: totalNum,
                footPager: pager(_.get(result.list, 'data.page_total', 0), params),
                goods: productProcess.processProductList(_.get(result.list, 'data.product_list', []),
                    Object.assign({showDiscount: false, isGlobal: true}, params)),
                hasNextPage: searchHandler.handleNextPage(params, totalNum),
                latestWalk: 6 // 最近浏览记录
            };
        }

        return resData;
    });
};

const getGlobalProductDetailData = (skn, channelNum, channel) => {
    return Promise.props({
        header: headerModel.requestHeaderData(channel),
        detail: globalApi.getGlobalProductDetailAsync(skn, channelNum),
        html: globalApi.getGlobalProductHtmlAsync(skn, channelNum)
    }).then(result => {
        let resData = {};
        let detailInfo, html = '';

        if (+result.detail.code === 200) {
            detailInfo = _.get(result, 'detail.data', {});

            let brandInfo = detailInfo.brand_info || {};

            resData.banner = {
                bgColor: '#000',
                homeUrl: helpers.urlFormat('/product/global/list', {brand: brandInfo.brand_id}),
                brandName: brandInfo.brand_name,
                logo: brandInfo.brand_ico
            };

            resData.pathNav = _.concat(
                homeService.getHomeChannelNav(channel),
                [
                    {name: '全球购', href: helpers.urlFormat('/product/global/list')},
                    {name: detailInfo.product_name || ''}
                ]
            );

            if (detailInfo.orign_price - detailInfo.final_price === 0) {
                _.unset(detailInfo, 'formart_orign_price');
            } else {
                detailInfo.promotion = ((detailInfo.final_price / detailInfo.orign_price) * 10).toFixed(1);

                // 只显示大于1折小于9折的折扣
                if (detailInfo.promotion <= 1.0 || detailInfo.promotion >= 9.0) {
                    detailInfo.promotion = false;
                }
            }

            if (detailInfo.market_price - detailInfo.sales_price === 0) {
                _.unset(detailInfo, 'format_market_price');
            }

            if (!_.isEmpty(detailInfo.goods_list)) {
                let colors = [];

                _.forEach(detailInfo.goods_list, (value, index) => {
                    let size = [];


                    _.forEach(value.size_list || [], subVal => {
                        size.push({
                            sku: subVal.product_sku,
                            name: subVal.size_name,
                            soldOut: subVal.storage_number * 1 <= 0
                        });
                    });


                    if (_.isEmpty(value.images_list)) {
                        value.images_list = [{
                            image_url: value.color_image
                        }];
                    }

                    if (index === 0 && !detailInfo.mainThumb) {
                        value.focus = true;
                        detailInfo.mainThumb = _.get(value, 'images_list[0].image_url', value.color_image);
                    }

                    colors.push({
                        src: value.color_image,
                        name: value.color_name,
                        title: value.color_name,
                        imgList: value.images_list,
                        focus: value.focus,
                        size: size
                    });
                });

                detailInfo.colors = colors;
            }
        }

        if (result.html) {
            let regContent = /<body[^>]*>([\s\S]*)<\/body>/.exec(result.html);

            html = regContent[1] || '';
            html = html.replace(/<script.*?>.*?<\/script>/ig, '');
        }

        Object.assign(resData, result.header, {
            goodsInfo: detailInfo,
            detailHtml: html || ''
        });

        return resData;
    });
};

module.exports = {
    getGlobalProductListData,
    getGlobalProductDetailData
};