outlet.js 5.5 KB
/**
 * 频道页面 model
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/05/09
 */
'use strict';

const library = '../../../library';
const utils = '../../../utils';

const _ = require('lodash');
const ServiceAPI = require(`${library}/api`).ServiceAPI;
const API = require(`${library}/api`).API;
const sign = require(`${library}/sign`);

const camelCase = require(`${library}/camel-case`);
const resourcesProcess = require(`${utils}/resources-process`);
const log = require(`${library}/logger`);

const serviceApi = new ServiceAPI();
const api = new API();

const dateFormate = (str) =>{
    var time = new Date(str * 1000);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();

    return y + '年' + m + '月' + d + '日' + h + '时';
};

const transDiscountToArr = (discount) => {
    return discount.replace(/(?:\d+[.\d]?)([\u4e00-\u9fa5]{1})/g, function(fullMatch, capture) {
        if (capture) {
            const arr = [];

            arr.push(fullMatch.replace(capture, ''));
            arr.push(capture);
            return arr;
        } else {
            return fullMatch;
        }
    }).split(',');
};

const getOutletResource = (channel, contentcode) => {
    const params = {
        content_code: contentcode || 'c19ffa03f053f4cac3690b22c8da26b7',
        limit: 25,
        yh_channel: channel || ''
    };

    return serviceApi.get('operations/api/v5/resource/home', sign.apiSign(params)).then(result => {
        if (result && result.code === 200) {
            return resourcesProcess(result.data.list);
        } else {
            log.error('奥莱资源位接口返回状态码 不是 200');
            return [];
        }
    });
};

const convertNavData = (list) => {
    const formatData = [];

    list = list || [];
    list = camelCase(list);
    _.forEach(list, (item) => {
        formatData.push({
            id: item.id,
            name: item.sortName,
            url: encodeURI(item.sortUrl)
        });
    });

    return {data: formatData};
};

const getNavData = (categoryId) => {
    const params = {
        v: 7,
        parent_id: categoryId
    };

    return serviceApi.get('operations/api/v6/category/getCategory', sign.apiSign(params)).then(result => {
        if (result && result.code === 200) {
            let data = convertNavData(result.data);

            data.category = categoryId;
            return data;
        } else {
            log.error('奥莱导航接口返回状态码 不是 200', result);
            return [];
        }
    });
};

const convertActicityData = (data) => {
    const formatData = [];

    let discountArr = [],
        discountNum = 0,
        discountText = 0;

    data = data || [];
    _.forEach(data, (item) => {
        discountArr = item.promotionName.split('~');
        if (discountArr.length === 1) {
            discountNum = transDiscountToArr(discountArr[0])[0];
            discountText = transDiscountToArr(discountArr[0])[1];
        } else {
            discountNum = discountArr[0] + '~' + transDiscountToArr(discountArr[1])[0];
            discountText = transDiscountToArr(discountArr[1])[1];
        }

        formatData.push({
            activityUrl: '/product/outlet/activity?id=' + item.id,
            coverUrl: item.coverUrl,
            logoUrl: item.logoUrl,
            title: item.title,
            discountNum: discountNum,
            discountText: discountText,
            leftTime: item.startLeftTime > 0 ? dateFormate(item.startTime) : item.endLeftTime,
            hide: false
        });
    });

    return formatData;
};

const getActivityDetail = (id) => {
    var params = {
        method: 'app.outlets.activityGet',
        sort: 2,
        platform: 3,
        yh_channel: 1,
        id: id,
        type: 0
    };

    return api.get('', sign.apiSign(params)).then(res => {
        if (res.code === 200) {
            return convertActicityData(res.data);
        } else {
            log.error('获取奥莱活动详情页接口返回状态码 不是 200', res);
            return {};
        }
    });
};

const getHomeActivity = (data) => {
    var params = {
        method: 'app.outlets.activityGet',
        platform: 3
    };

    return api.get('', sign.apiSign(_.assign(params, data))).then(res => {
        return convertActicityData(res.data);
    });
};

exports.getContent = (categoryId, channel, code) => {
    let params = {
        type: 0,
        yh_channel: channel
    };

    const p = [getNavData(categoryId), getOutletResource(channel, code), getHomeActivity(params)];

    return Promise.all(p).then(data => {

        return {
            nav: data[0] || [],
            content: data[1] || [],
            activity: data[2]
        };
    });
};


exports.getActivity = (id) => {
    return getActivityDetail(id).then(res => {
        return {
            activity: res,
            productPool: res[0] && res[0].productPoolId || '',
            activityTitle: res[0] && res[0].title || 'OUTLET',
            saleType: 4
        };
    });
};

/**
 * 获取即将开始或即将结束的活动列表
 * @param  {Number} type 标明是即将开始还是即将技术
 * @param  {String} categoryId 父级菜单id,用于标明当前页面是奥莱页面
 * @return {Object} 活动列表数据
 */
exports.getRecentActivity = (type, categoryId) => {
    var params = {
        type: type
    };

    return Promise.all([getNavData(categoryId), getHomeActivity(params)]).then(res => {

        return {
            nav: res[0] || [],
            activity: res[1]
        };
    });
};