bundle.js 1.53 KB
/**
 * Created by targaryen on 2016/11/28.
 */
'use strict';
const utils = '../../../utils';
const _ = require('lodash');
const api = global.yoho.API;
const helpers = global.yoho.helpers;
const productProcess = require(`${utils}/product-process`);

/**
 * 从接口获取套装数据
 * @private
 */
const getBundleBySkn = (productSkn) => {
    return api.get('', {
        method: 'app.query.bundleSkn',
        product_skn: productSkn
    }, {cache: false}).then(result => {
        return result;
    });
};

/**
 * 套装详情页数据
 * @param params
 * @returns {*}
 */
const detail = (params) => {
    if (!params.skn) {
        return Promise.resolve({});
    }
    let bundleIndex = (params.index || 1);

    --bundleIndex;
    return getBundleBySkn(params.skn).then(result => {
        if (_.has(result, `data[${bundleIndex}]`)) {
            return {
                bundleDatas: _.map(result.data, (bundle, index) => {
                    return {
                        selected: index === bundleIndex,
                        title: _.get(bundle, 'bundleInfo.bundleName') || '优惠套装',
                        href: helpers.urlFormat('/product/bundle/detail', {skn: params.skn, index: ++index}),
                    };
                }),
                bundleInfo: _.get(result, `data[${bundleIndex}].bundleInfo`, {}),
                productList: productProcess.processProductList(_.get(result, `data[${bundleIndex}].productList`, []))
            };
        }
        return {};
    });
};

module.exports = {
    getBundleBySkn,
    detail
};