sale.js 10 KB
/*
 * @Author: Targaryen
 * @Date:   2016-05-19 10:20:08
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-05-25 10:04:06
 */

'use strict';
const library = '../../../library';
const API = require(`${library}/api`).API;
const TARAPI = require(`${library}/tar-api`).TARAPI;
const sign = require(`${library}/sign`);
const api = new API();
const tarApi = new TARAPI();
const _ = require('lodash');

/**
 * 处理商品列表数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
const handleSaleGoodsListData = (origin) => {
    var dest = [];


    if (!_.isEmpty(origin)) {

        _.forEach(origin, function(value) {
            let oneGoods = {};

            oneGoods.tags = value.tags;
            oneGoods.thumb = value.default_images;
            oneGoods.url = '/product/pro_' + value.product_id + '_' + value.goods_list[0].goods_id + '/' +
                value.cn_alphabet + '.html';
            oneGoods.goodsList = value.goods_list;
            oneGoods.name = value.product_name;
            oneGoods.brand = {};
            oneGoods.brand.url = value.brand_domain + '.SUB_DOMAIN'; // 待处理
            oneGoods.brand.name = value.brand_name;
            oneGoods.marketPrice = value.market_price;

            dest.push(oneGoods);
        });
    }

    return dest;
};

/**
 * 处理筛选数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
const handleSaleFilterData = (origin) => {
    var dest = {};

    dest.brand = {};
    dest.price = [];
    dest.channel = [];

    dest.opts = {};

    // 处理 品牌 筛选数据
    dest.brand.default = [];
    if (!_.isEmpty(origin.brand)) {
        _.forEach(origin.brand, function(value) {
            let brand = {};

            brand.checked = false;
            brand.href = 'CURRENT_URL/brand=' + value.brand_domain; // 待处理
            brand.name = value.brand_name;

            dest.brand.default.push(brand);
        });
    }

    // 处理 价格 筛选数据
    if (!_.isEmpty(origin.priceRange)) {
        _.forEach(origin.priceRange, function(value, key) {
            let price = {
                checked: false,
                href: 'CURRENT_URL/price=' + key, // 待处理
                name: value
            };

            dest.price.push(price);
        });
    }

    // 处理频道数据
    for (let i = 0; i < 2; i++) {
        dest.channel[i] = {};

        dest.channel[i].checked = false;
    }

    dest.channel[0].name = '男生';
    dest.channel[0].href = 'CURRENT_URL/?gender=1,3';
    dest.channel[1].name = '女生';
    dest.channel[1].href = 'CURRENT_URL/?gender=2,3';

    return dest;
};

/**
 * 处理 opts 排序数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
const handleSaleOptsData = () => {
    var dest = {};

    dest.countPerPage = 60;
    dest.start = 1;
    dest.end = 20;
    dest.sortType = [];

    for (let i = 0; i < 2; i++) {
        let opt = {};

        opt.href = 'CURRENT_URL/?order=s_p_asc';
        opt.name = '价格';
        opt.hasSortOrient = {};
        opt.hasSortOrient.active = {};
        opt.hasSortOrient.active.desc = {};

        dest.sortType.push(opt);
    }

    return dest;
};

/**
 * 处理页面左侧分类筛选数据
 * @return {[type]} [description]
 */
const handleSaleSortData = (origin) => {
    var leftContent = {};

    leftContent.allDiscount = {};
    leftContent.allDiscount.list = [];

    _.forEach(origin, function(value) {
        let category = {};

        category.name = value.sort_name;
        leftContent.allDiscount.list.push(category);
    });


    return leftContent;
};

/**
 * 处理折扣专区活动数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
const handleSaleActivityData = (origin) => {
    var dest = {};

    dest.big = [];
    dest.normal = [];
    _.forEach(origin, function(value, key) {
        let activity = {};

        activity.link = value.web_url;
        activity.img = value.cover_url;
        activity.time = value.left_time;
        activity.brand = value.logo_url;
        activity.discount = value.title;

        if (key < 3) {
            dest.big.push(activity);
        } else {
            dest.normal.push(activity);
        }
    });
    return dest;
};

/**
 * 处理首页 banner 数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
const handleSaleBannerData = (origin) => {
    var dest = {};

    dest.list = [];
    _.forEach(origin, function(value) {
        if (value.template_name === 'focus') {
            _.forEach(value.data, function(subValue) {
                let banner = {};

                banner.bannerHeight = 450;
                banner.href = subValue.url;
                banner.img = subValue.src;

                dest.list.push(banner);
            });
        }

        if (value.template_name === 'small_pic') {
            _.forEach(value.data, function(picList) {
                let smallPic = {};

                smallPic.link = picList.url;
                smallPic.icon = picList.src;
                smallPic.title = picList.title;
                smallPic.desc = picList.alt;

                dest.list.push(smallPic);
            });
        }

    });

    return dest;
};

/**
 * 处理首页 banner 小图
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
const handleSaleBannerSmallData = (origin) => {
    var dest = [];

    _.forEach(origin, function(value) {

        if (value.template_name === 'small_pic') {
            _.forEach(value.data, function(picList) {
                let smallPic = {};

                smallPic.link = picList.url;
                smallPic.icon = picList.src;
                smallPic.title = picList.title;
                smallPic.desc = picList.alt;

                dest.push(smallPic);
            });
        }

    });

    return dest;
};

/**
 * 获取商品列表 promise 对象
 * @return {[type]} [description]
 */
const getSaleGoodsList = (params) => {

    return api.get('', sign.apiSign({
        method: 'app.search.sales',
        p_d: _.isEmpty(params.p_d) ? null : params.p_d,
        limit: _.isEmpty(params.limit) ? null : params.limit,
        order: _.isEmpty(params.order) ? 's_t_desc' : params.order,
        page: _.isEmpty(params.page) ? 1 : params.page,
        gender: _.isEmpty(params.gender) ? null : params.gender,
        sort: _.isEmpty(params.sort) ? null : params.sort,
        brand: _.isEmpty(params.brand) ? null : params.brand,
        color: _.isEmpty(params.color) ? null : params.color,
        size: _.isEmpty(params.size) ? null : params.size,
        saleType: _.isEmpty(params.saleType) ? null : params.saleType,
        breakSize: _.isEmpty(params.breakSize) ? null : params.breakSize,
        breakSort: _.isEmpty(params.breakSort) ? null : params.breakSort,
        productPool: _.isEmpty(params.productPool) ? null : params.productPool,
        price: _.isEmpty(params.price) ? null : params.price,
        productSize: '384x511',
        yh_channel: 1
    }));
};

/**
 * 获取左侧分类信息数据 promise 对象
 * @return {[type]} [description]
 */
const getSortList = () => {
    return api.get('', sign.apiSign({
        method: 'app.sale.getBreakingSort',
        yh_channel: 1
    }));
};

/**
 * 获取折扣专区活动列表 promise 对象
 * @return {[type]} [description]
 */
const getSaleActivityList = () => {
    return tarApi.getActivity('', sign.apiSign({
        method: 'app.activity.get',
        sort: 2,
        plateform: 2
    }));
};

/**
 * 获取顶部 banner
 * @return {[type]} [description]
 */
const getSaleBannerList = () => {
    return tarApi.getBanner('', sign.apiSign({
        content_code: '7c2b77093421efa8ae9302c91460db73'
    }));
};

/**
 * 获取 Sale 首页数据
 * @return {[type]} [description]
 */
exports.getSaleIndexData = () => {

    return api.all([getSaleActivityList(), getSaleBannerList()]).then(result => {
        var finalResult = {};

        if (result[0].code === 200) {
            finalResult.brandSale = handleSaleActivityData(result[0].data);
        }

        if (result[1].code === 200) {
            finalResult.topBanner = handleSaleBannerData(result[1].data);
            finalResult.activityEnter = handleSaleBannerSmallData(result[1].data);
        }

        return finalResult;
    });
};

/**
 * 获取商品列表数据 仅 Ajax 调用时使用
 * @return {[type]} [description]
 */
exports.getSaleGoodsData = (params) => {

    return getSaleGoodsList(params).then(result => {
        let finalResult = {};

        if (result.code === 200) {
            finalResult.goods = handleSaleGoodsListData(result.data.product_list);
        }
        return finalResult;
    });
};

/**
 * 获取其他三个页面全部数据
 * @return {[type]} [description]
 */
exports.getSaleData = (params) => {

    return api.all([getSortList(), getSaleGoodsList(params)]).then(result => {
        let finalResult = {};

        if (result[0].code === 200) {
            finalResult.leftContent = handleSaleSortData(result[0].data);
        }
        if (result[1].code === 200) {

            if (!_.isEmpty(result[1].data.product_list)) { // 处理商品列表数据
                finalResult.goods = handleSaleGoodsListData(result[1].data.product_list);
            }

            if (!_.isEmpty(result[1].data.filter)) {
                finalResult.filters = handleSaleFilterData(result[1].data.filter);
            }
        }
        finalResult.opts = handleSaleOptsData();
        return finalResult;
    });
};

/**
 * 获取折扣专场数据
 * @return {[type]} [description]
 */
exports.getDiscountData = (params) => {
    return api.all([getSortList(), getSaleGoodsList(params)]).then(result => {
        let finalResult = {};

        if (result[0].code === 200) {
            finalResult.leftContent = handleSaleSortData(result[0].data);
        }
        if (result[1].code === 200) {
            finalResult.saleList = {};
            finalResult.saleList.goods = handleSaleGoodsListData(result[1].data.product_list);
        }

        return finalResult;
    });
};