sale.js 9.22 KB
/**
 * 频道页面 model
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/05/17
 */
'use strict';
const utils = '../../../utils';
const contentCodeConfig = require('../../../config/content-code');

const logger = global.yoho.logger;
const camelCase = global.yoho.camelCase;
const resourcesProcess = require(`${utils}/resources-process`);
const productProcess = require(`${utils}/product-process`);
const processTime = require(`${utils}/time-process`);
const _ = require('lodash');
const api = global.yoho.API;
const serviceAPI = global.yoho.ServiceAPI;
const helpers = global.yoho.helpers;

/**
 * 排序转换
 */
const typeCont = {
    price: ['s_p_desc', 's_p_asc'],
    discount: ['p_d_desc', 'p_d_asc'],
    sale: ['s_n_desc', 's_n_asc'],

    // newest: ['s_t_desc,s_s_desc', 's_t_asc,s_s_asc'],
    newest: ['s_t_desc', 's_t_asc'],
    stock: ['s_s_desc', 's_s_asc'],
    all: ['d_s_desc', 'd_s_asc'],
    category: ['s_t_desc', 's_t_asc']
};

const channelType = {
    boys: '1',
    girls: '2',
    kids: '3',
    lifestyle: '4',
    all: '1,2,3,4'
};

/**
 * 频道转换
 * 产品要求,SALE的导航显示 Boy,Girl 单数形式
 */
const channelHash = {
    boys: 'Boy',
    girls: 'Girl',
    kids: 'Kid',
    lifestyle: 'Lifestyle'
};

const saleNav = (channel) => {
    return {
        channel: channelHash[channel],
        list: [
            {
                title: 'Boy',
                url: helpers.urlFormat('/product/sale', {channel: 'boys'})
            }, {
                title: 'Girl',
                url: helpers.urlFormat('/product/sale', {channel: 'girls'})
            }, {
                title: 'Kid',
                url: helpers.urlFormat('/product/sale', {channel: 'kids'})
            }, {
                title: 'Lifestyle',
                url: helpers.urlFormat('/product/sale', {channel: 'lifestyle'})
            }
        ]
    };
};

/**
 * 资源位code码
 */
const contentCode = contentCodeConfig.sale;

/**
 * 折扣专场列表页及详情页公共方法
 * @param  {[array]} list
 * @return {[array]}
 */
const _processDiscount = (list) => {
    const formatData = [];
    let flag = true;

    list = list || [];
    list = camelCase(list);

    // list为1条数据是表示详情页,flag 设为false,不需要拼接跳转链接
    if (list.length === 1) {
        flag = false;
    }

    _.forEach(list, (data) => {
        if (flag === true) {
            Object.assign(data, processTime(data.leftTime));
        }

        formatData.push(data);
    });

    return formatData;
};

/**
 * 折扣专场接口调用
 * @param  {[object]} params
 * @return {[array]}
 */
const _discount = (params) => {
    params = params || {};

    return api.get('', Object.assign({
        method: 'app.activity.get',
        sort: 2,
        plateform: 3
    }, params), {
        cache: true
    });
};

/**
 * 断码区分类数据处理
 * @param  {[array]} list
 * @return {[array]}
 */
const _processBreakingSort = (list) => {
    const formatData = {};
    const sort = [];
    const sub = [];

    list = list || [];
    list = camelCase(list);

    _.forEach(list, (data, index) => {
        const allSub = [];

        data.sub.key = index;
        sub.push(data.sub);

        _.forEach(data.sub, (dataSub) => {
            allSub.push(dataSub.sizeId);
        });

        sort.push({
            sortName: data.sortName,
            sortId: data.sortId,
            allSub: allSub
        });
    });

    formatData.sortData = sort;
    formatData.sub = sub;

    return formatData;
};

/**
 * 商品搜索接口请求
 * @param  {[object]} params
 * @return {[array]}
 */
const _searchSales = (params) => {

    let method = 'app.search.sales';

    // 排除基本筛选项默认值为0的对象
    for (let str in params) {
        if (str !== 'order' && params[str] === '0' || params[str] === null) {
            delete params[str];
        }
    }

    params.yh_channel = channelType[params.yh_channel];

    if (params.outlets) {
        method = 'app.search.category';
    }

    params = Object.assign({
        limit: '50'
    }, params);

    if (typeCont[params.type]) {
        params.order = typeCont[params.type][params.order];
    }

    return api.get('', Object.assign({
        method: method
    }, params), {
        cache: true
    });
};

/**
 * 获取用户数据信息
 * @param  {[string]} uid
 * @return {[array]}
 */
const _getUserProfile = (uid) => {
    if (!uid) {
        return Promise.resolve({
            code: 200,
            data: {}
        });
    }
    return api.get('', {
        method: 'app.passport.profile',
        uid: uid
    }, {
        cache: true
    });
};

/**
 * 获取资源位数据
 * @param  {[string]} page
 * @return {[array]}
 */
const _getResources = (page, channel) => {
    return serviceAPI.get('operations/api/v5/resource/get', {
        content_code: contentCode[channel][page]
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return resourcesProcess(result.data);
        } else {
            logger.error('SALE content resource code no 200');
            return [];
        }
    });
};

/**
 * 获取断码区分类数据
 * @param  {[string]} yhChannel
 * @return {[object]}
 */
const _getBreakingSort = (yhChannel) => {
    return api.get('', {
        method: 'app.sale.getBreakingSort',
        yh_channel: channelType[yhChannel] || '1'
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return _processBreakingSort(result.data);
        } else {
            logger.error('api app.sale.getBreakingSort code no 200');
            return {};
        }
    });
};

/**
 * 获取商品数据
 */
const getSearchData = (params, uid) => {
    return Promise.all([
        _searchSales(params).then((result) => {
            if (result && result.code === 200) {
                return productProcess.processProductList(result.data.product_list || [], {
                    yh_channel: params.yh_channel,
                    showSale: false
                });
            } else {
                logger.error('api SALE product search code no 200');
                return [];
            }
        }),
        _getUserProfile(uid).then((result) => {
            if (result && result.code === 200) {
                return result.data.vip_info ? camelCase(result.data.vip_info) : {};
            } else {
                logger.error('api get user info code no 200');
                return {};
            }
        })
    ]);
};

/**
 * 获取筛选数据
 * @param  {[object]} params
 * @return {[array]}
 */
const getFilterData = (params) => {
    return _searchSales(params).then((result) => {
        if (result && result.code === 200) {
            return productProcess.processFilter(result.data.filter || [], {
                hideSize: params.saleType === '1',
                hideSort: params.saleType === '1'
            });
        } else {
            logger.error('SALE search product code no 200');
            return [];
        }
    });
};

/**
 * 获取sale首页数据
 * @return {[array]}
 */
const getSaleData = (channel) => {
    return _getResources('sale', channel);
};


/**
 * 获取会员享数据
 * @return {[array]}
 */
const getVipData = (channel) => {
    return _getResources('vip', channel);
};


/**
 * 获取断码区数据
 * @param  {[object]} params
 * @return {[object]}
 */
const getBreakCodeData = (params) => {
    params = params || {};
    return Promise.all([_getResources('breakCode', params.yhChannel), _getBreakingSort(params.yhChannel)])
        .then((result) => {
            return {
                content: result[0],
                nav: result[1]
            };
        });
};

/**
 * 获取折扣专场专题列表数据
 * @param  {[object]} params
 * @return {[object]}
 */
const getDiscountData = (yhChannel) => {
    return _discount({
        yh_channel: channelType[yhChannel] || '1'
    }).then((result) => {
        if (result && result.code === 200) {
            return {
                list: _processDiscount(result.data)
            };
        } else {
            logger.error('api discount list code no 200');
            return {};
        }
    });
};

/**
 * 获取折扣专场专题详情数据
 * @param  {[string]} id
 * @return {[object]}
 */
const getDiscountDetailData = (id, yhChannel) => {
    let res = {};
    let param = {
        id: id,
        yh_channel: channelType[yhChannel] || '1'
    };

    return _discount(param).then((result) => {
        if (result && result.code === 200) {
            res = _processDiscount(result.data);
            if (res[0] && res[0].coverUrl) {
                res[0].coverUrl = res[0].coverUrl.replace('/extent/{width}x{height}', '');
            }
            return {
                title: res[0] && res[0].title,
                productPool: res[0] && res[0].productPool,
                activity: {
                    coverUrl: res[0] && res[0].coverUrl,
                    leftTime: res[0] && res[0].leftTime
                }
            };
        } else {
            logger.error('discount detail code no 200');
            return {};
        }
    });
};

module.exports = {
    getSaleData,
    getBreakCodeData,
    getDiscountData,
    getDiscountDetailData,
    getVipData,
    getFilterData,
    getSearchData,
    saleNav
};