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

'use strict';

const _ = require('lodash');
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', {});

            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, '');
        }

// console.log(result.detail.code);
        Object.assign(resData, result.header, {
            goodsInfo: detailInfo,
            detailHtml: html || ''
        });

        return resData;
    });
};

module.exports = {
    getGlobalProductListData,
    getGlobalProductDetailData
};