sale.js 4.29 KB
/**
 * 频道页面 model
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/05/17
 */
'use strict';
const library = '../../../library';
const utils = '../../../utils';
const API = require(`${library}/api`).API;
const ServiceAPI = require(`${library}/api`).ServiceAPI;
const sign = require(`${library}/sign`);
const logger = require(`${library}/logger`);
const helpers = require(`${library}/helpers`);
const camelCase = require(`${library}/camel-case`);
const resourcesProcess = require(`${utils}/resources-process`);
const _ = require('lodash');
const api = new API();
const serviceAPI = new ServiceAPI();
const anHour = 3600000;
const aDay = anHour * 24;

/**
 * 折扣专场列表数据处理
 */
const processSpecial = (list) => {
    const formatData = [];

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

    _.forEach(list, (data) => {
        data.specialUrl = `/product/specialDetail?id=${data.id}`;

        if (data.leftTime < anHour) {
            data.warnColor = true;
            data.leftTime = '低于1小时';
        } else {
            if (data.leftTime > aDay) {
                data.leftTime = helpers.dateDiffFormat('剩{d}天{h}小时', data.leftTime, 'ms');
            } else {
                data.leftTime = helpers.dateDiffFormat('剩{h}小时', data.leftTime, 'ms');
            }
        }
        formatData.push(data);
    });

    return formatData;
};

/**
 * 商品搜索数据处理
 */
const processSearch = (list) => {
    // const formartData = [];

    // let goods = {};

    list = list || [];

    // _.forEach(camelCase(list.data.product_list), (data) => {

    // });
};

/**
 * 获取商品数据
 */
exports.getSearchData = (params) => {
    return api.get('', sign.apiSign(Object.assign({
        method: 'app.search.sales'
    }, params))).then((result) => {
        if (result && result.code === 200) {
            return processSearch(result);
        } else {
            logger.error('SALE 商品搜索返回 code 不是 200');
            return [];
        }
    });
};

exports.getSaleResources = () => {
    return serviceAPI.get('operations/api/v5/resource/get', sign.apiSign({
        content_code: '7c2b77093421efa8ae9302c91460db73'
    })).then((result) => {
        if (result && result.code === 200) {
            return resourcesProcess(result.data);
        } else {
            logger.error('SALE 页面资源位返回 code 不是 200');
            return [];
        }
    });
};

/**
 * 获取折扣专场专题列表数据
 * @param  {[object]} params
 * @return {[object]}
 */
exports.getSpecialData = (params) => {
    const specialData = {};

    return api.get('', sign.apiSign(Object.assign({
        method: 'app.activity.get',
        sort: 2,
        plateform: 2
    }, params))).then((result) => {
        if (result && result.code === 200) {
            specialData.data = processSpecial(result.data);
            return specialData;
        } else {
            return Promise.reject('error');
        }
    });
};


/**
 * 获取折扣专场专题详情数据
 * @param  {[object]} params
 * @return {[object]}
 */
exports.getSpecialDetailData = (params) => {
    return api.get('', sign.apiSign(Object.assign({
        method: 'app.activity.get',
        sort: 2,
        plateform: 2
    }, params))).then((result) => {
        if (result && result.code === 200) {
            return camelCase(result.data);
        } else {
            return Promise.reject('error');
        }
    });
};

/**
 * 获取断码区数据
 * @param  {[object]} params
 * @return {[object]}
 */
exports.getBreakCodeData = (params) => {
    return api.get('', sign.apiSign(Object.assign({
        method: 'app.activity.get',
        sort: 2,
        plateform: 2
    }, params))).then((result) => {
        if (result && result.code === 200) {
            return camelCase(result.data);
        } else {
            return Promise.reject('error');
        }
    });
};


/**
 * 获取会员享数据
 * @param  {[object]} params
 * @return {[object]}
 */
exports.getVipData = (params) => {
    return api.get('', sign.apiSign(Object.assign({
        method: 'app.activity.get',
        sort: 2,
        plateform: 2
    }, params))).then((result) => {
        if (result && result.code === 200) {
            return camelCase(result.data);
        } else {
            return Promise.reject('error');
        }
    });
};