outlet.js 4.79 KB
/**
 * 频道页面 model
 * @author: Bi Kai<kai.bi@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 productProcess = require(`${utils}/product-process`);

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 getOutletResource = () => {
    const params = {
        content_code: 'c19ffa03f053f4cac3690b22c8da26b7',
        limit: 25,
        debug: 'XYZ'
    };

    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 result;
        }
    });
};

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 = () => {
    const params = {
        // client_type: 'iphone',
        // os_version: 8.4,
        // screen_size: '320x568',
        v: 7,

        // parent_id: 1107,
        debug: 'XYZ'
    };

    return api.get('operations/api/v6/category/getCategory', sign.apiSign(params)).then(result => {
        if (result && result.code === 200) {
            return convertNavData(result.data);
        } else {
            log.error('奥莱导航接口返回状态码 不是 200', result);
            return result;
        }
    });
};

const getGoodsList = () => {
    var params = {
        method: 'app.search.category',
        limit: 50,
        page: 1,
        outlets: 1
    };

    var data = {};

    return api.get('', sign.apiSign(params)).then(res => {
        if (res && res.code === 200) {
            data = productProcess.processProductList(res.data.product_list);
        }

        return data;
    });
};

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

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

const convertActicityData = (data) => {
    const formatData = [];
    var showMore = false;
    var i;

    data = data || [];
    _.forEach(data, (item) => {
        formatData.push({
            activityUrl: 'outlet/activity?id=' + item.id,
            coverUrl: item.coverUrl,
            logoUrl: item.logoUrl,
            title: item.title,
            promotionName: item.promotionName,
            startLeftTime: item.startLeftTime,
            hide: false
        });
    });


    if (formatData.length > 10) {
        for (i = 10; i < formatData.length; i++) {
            formatData[i].hide = true;
        }
        showMore = true;
    }

    return {data: formatData, showMore: showMore};
};

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

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

exports.getContent = () => {
    let floorsData = {};

    return Promise.all([getNavData(), getOutletResource(), getHomeActivity(0), getGoodsList()]).then(data => {
        floorsData.nav = data[0] || [];
        floorsData.content = data[1] || [];
        floorsData.activity = data[2] || [];
        floorsData.goods = data[3];

        return floorsData;
    });
};


exports.getActivity = (id) => {
    return getActivityDetail(id).then(res => {
        var data = {};

        data.outletActivity = res;
        data.productPool = res[0] && res[0].productPoolId || '';
        return data;
    });
};

exports.willBeEnd = () => {
    return getHomeActivity(3).then(res => {
        var data = {};

        data.activity = res;
        return data;
    });
};

exports.willBeCome = () => {
    return getHomeActivity(4).then(res => {
        var data = {};

        data.activity = res;
        return res;
    });
};