channel.js 4.87 KB
/**
 * 频道页面 model
 * @author: Bi Kai<kai.bi@yoho.cn>
 * @date: 2016/05/09
 */
'use strict';
const _ = require('lodash');
const API = require('../../../library/api').ServiceAPI;
const sign = require('../../../library/sign');
const camelCase = require('../../../library/camel-case');

const api = new API();

const genderData = {
    boys: '1,3',
    girls: '2,3'
};

/**
 * 楼层资源的位置码
 * @type {Object}
 */
const contentCode = {
    boys: '8512bf0755cc549ac323f852c9fd945d',
    girls: '189b6686065dbd6755dd6906cf03c002',
    kids: 'b8c1bff53d4ea60f978926d538620636',
    lifestyle: '61cd852c6afcf60660196154f66a3a62'
};

/**
 * 频道底部 Banner 位置码
 * @type {Object}
 */
const bottomBannerCode = {
    boys: 'a2ec977c027d0cd9cdccb356ddf16b08',
    girls: '8c8bd1b89a22e5895f05882e0825b493'
};

/**
 * 获取二级菜单顶部颜色
 * @param  {[string]} choosed
 * @return {[string]}
 */
const getSidebarColor = (choosed) => {
    let color = false;

    if (choosed === 'girls') {
        color = '#FF88AE';
    } else if (choosed === 'kids') {
        color = '#7ad9f9';
    } else if (choosed === 'lifestyle') {
        color = '#4f4138';
    }

    return color;
};

/**
 * 处理侧边栏数据
 * @param  {[array]} list
 * @return {[array]}
 */
const processSideBar = (list, choosed) => {
    const formatData = [];
    let offset = 0; // 分割数组用到的游标

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

    _.forEach(list, (item, i) => {
        if (item.sub) {
            item.sub.unshift({
                sortName: item.sortName,
                sortNameEn: item.sortNameEn,
                back: true,
                isSelect: false,
                bgColor: getSidebarColor(choosed)
            });
        }

        // 如果有分隔符,分割数组
        if (item.separativeSign === 'Y') {
            formatData.push(list.slice(offset, i));
            offset = i;
        }
    });

    // 数组被分割剩余的部分
    formatData.push(list.slice(offset));
    return formatData;
};

/**
 * 处理楼层数据
 * @param  {[array]} list
 * @return {[array]}
 */
const processFloor = (list) => {
    const formatData = [];

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

    _.forEach(list, (floor) => {
        floor[_.camelCase(floor.templateName)] = true;

        // 特殊资源位处理
        // PLUS
        if (floor.singleNameImage && floor.data) {
            floor.data.title = {
                title: floor.data.title
            };
        }

        // 潮流时装/经典裤裙/时尚靴履/潮人配饰/潮流上装
        if (floor.recommendContentOne && floor.data) {
            if (floor.data.bigImage && floor.data.bigImage.length > 1) {
                floor.data.bigImage = {
                    bigList: floor.data.bigImage
                };
            }
        }

        formatData.push(floor);
    });

    return formatData;
};

/**
 * 获取频道页面资源位
 * @param  {[object]} gender
 * @return {[type]}
 */
const getChannelResource = (params) => {
    params.gender = params.gender || 'boys';

    params = Object.assign({
        gender: genderData[params.gender],
        content_code: contentCode[params.gender], // eslint-disable-line
        page: 1,
        limit: 30
    }, params);
    if (!params.uid) {
        params.new_device = true; // eslint-disable-line
    }

    return api.get('operations/api/v5/resource/home', sign.apiSign(params), 300).then(result => {
        if (result && result.code === 200) {
            return processFloor(result.data.list);
        } else {
            return result;
        }
    });
};

/**
 * 获取左侧边栏数据
 * @param  {[string]} choosed
 * @return {[object]}
 */
const getLeftNav = (choosed) => {
    choosed = choosed || 'all';

    return api.get('operations/api/v6/category/getCategory', sign.apiSign({}), 300).then(result => {
        if (result && result.code === 200) {
            return processSideBar(result.data, choosed);
        } else {
            return result;
        }
    });
};

/**
 * 获取频道页面数据
 * @param  {[object]} params
 * @return {[object]}
 */
exports.getChannelDate = (params) => {
    var channelData = {};

    return api.all([getChannelResource(params), getLeftNav(params.gender)]).then((data) => {
        channelData.content = data[0]; // 资源位数据
        channelData.sideNav = data[1]; // 侧边栏数据

        return channelData;
    });
};

/**
 * 获取频道页底部 bannel 数据
 * @param  {[string]} gender
 * @return {[type]}
 */
exports.getBottomBannerDate = (gender) => {
    gender = gender || 'boys';

    if (gender === 'boys' || gender === 'girls') {
        return api.get('operations/api/v5/resource/get', sign.apiSign({
            content_code: bottomBannerCode[gender] // eslint-disable-line
        }));
    }
    return Promise.resolve({
        code: 400,
        data: '',
        message: '参数错误'
    });
};