star.js 2.16 KB
/**
 * 新潮教室
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/05/30
 */
'use strict';
const library = '../../../library';
const utils = '../../../utils';
const resourcesProcess = require(`${utils}/resources-process`);
const ServiceAPI = require(`${library}/api`).ServiceAPI;
const sign = require(`${library}/sign`);
const logger = require(`${library}/logger`);
const camelCase = require(`${library}/camel-case`);
const _ = require('lodash');
const serviceAPI = new ServiceAPI();

/**
 * 获取资源位数据
 * @param  {[string]} page
 * @return {[array]}
 */
const getResources = (page) => {
    const contentCode = {
        index: '8adc27fcf5676f356602889afcfd2a8e',
        special: '89cc20483ee2cbc8a716dcfe2b6c7603'
    };

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

/**
 * 星搭配文章列表数据处理
 */
const processCollocationData = (list) => {
    const formatData = [];

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

    _.forEach(list, (data) => {
        if (data.isFavor === 'N') {
            data.isCollected = false;
        } else {
            data.isCollected = true;
        }
        formatData.push(data);
    });

    return formatData;
};

/**
 * 星潮首页
 */
exports.getIndexData = () => {
    return getResources('index');
};

/**
 * 星专题
 */
exports.getSpecialData = () => {
    return getResources('special');
};

/**
 * 星搭配
 */
exports.getCollocationListData = (params, uid) => {

    return serviceAPI.get('guang/api/v5/article/getStarClassroomArticleList', sign.apiSign(Object.assign({
        limit: '20',
        uid: uid
    }, params))).then((result) => {
        if (result && result.code === 200) {
            return processCollocationData(result.data.list.artList);
        } else {
            logger.error('获取星搭配文章列表返回 code 不是 200');
            return [];
        }
    });
};