star.js 7.02 KB
/**
 * 新潮教室
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/05/30
 */
'use strict';
const utils = '../../../utils';
const contentCodeConfig = require('../../../config/content-code');
const resourcesProcess = require(`${utils}/resources-process`);
const logger = global.yoho.logger;
const helpers = global.yoho.helpers;
const _ = require('lodash');
const serviceAPI = global.yoho.ServiceAPI;
const api = global.yoho.API;

const contentCode = contentCodeConfig.guang;

/**
 * 获取资源位数据
 * @param  {[string]} page
 * @return {[array]}
 */
const _getResources = (page) => {
    return serviceAPI.get('operations/api/v5/resource/get', {
        content_code: contentCode[page],
        client_type: 'iphone'
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return resourcesProcess(result.data);
        } else {
            logger.error('star class content resource return code no 200');
            return [];
        }
    });
};

/**
 * 星潮教室首页数据处理
 * @param  {[array]} dataList
 * @return {[array]}
 */
const _processIndexData = (dataList) => {
    const formatData = {
        ads: [],
        starAvatar: [],
        articles: []
    };

    let list = dataList.data || {};


    formatData.md5 = dataList.md5;

    // 首页资源位数据处理
    if (list.ads) {
        _.forEach(list.ads.data, (data) => {
            formatData.ads.push({
                src: data.src.replace('imageView/', 'imageView2/'),
                url: data.url
            });
        });
    }

    // 首页明星文章数据处理
    if (list.list) {
        _.forEach(list.list, (data, index) => {
            const avatar = {
                tags: []
            };

            if (data.ext.tags.length > 1) {
                avatar.isSwiper = true;
            }

            _.forEach(data.ext.tags, (tags, i) => {
                if (i >= 1) {
                    return;
                }

                avatar.tags.push({
                    avatarUrl: `/guang/star/detail?tag=${tags.tagName}&openby:yohobuy{"action":"go.h5","params":{"id":"","share":"","shareparam":{},"islogin":"N","type":0,"updateflag":"N","url":"http://m.yohobuy.com/guang/star/detail","param":{"tag":"${tags.tagName}"}}}`, // eslint-disable-line
                    cover: tags.cover ? (tags.cover + '?imageView2/2/w/104/h/104') : tags.cover,
                    tagName: tags.tagName
                });
            });

            if (formatData.articles.length > 30) {
                return;
            }

            formatData.articles.push(_.merge({
                noLazy: index < 2,
                id: data.id,
                url: data.url,
                title: data.title,
                articeTxt: data.intro,
                src: data.src,
                publish_time: helpers.dateFormat('MM月DD日 hh:mm', data.publish_time),
                views_num: data.views_num
            }, avatar));
        });
    }


    // 首页明星头像数据处理
    if (list.tags) {
        _.forEach(list.tags, (data) => {
            let url = `/guang/star/detail?tag=${data.tagName}&openby:yohobuy{"action":"go.h5","params":{"id":"","share":"","shareparam":{},"islogin":"N","type":0,"updateflag":"N","url":"http://m.yohobuy.com/guang/star/detail","param":{"tag":"${data.tagName}"}}}`; // eslint-disable-line

            formatData.starAvatar.push({
                // noLazy: index < 6,
                url: url,
                cover: data.cover ? (data.cover +
                    '?imageView2/2/w/180/h/180/q/90').replace('http:', '') : data.cover.replace('http:', '')
            });
        });
    }

    return formatData;
};


/**
 * 明星专题列表及星搭配数据处理
 * @param  {[array]} list
 * @param  {[boolean]} flag 明星专题列表需要转换下日期格式
 * @return {[array]}
 */
const _processGuangData = (list, flag) => {
    const formatData = [];

    list = list || [];

    _.forEach(list, (data, key) => {
        if (flag) {
            data.publish_time = helpers.dateFormat('MM月DD日 hh:mm', data.publish_time);
        }

        if (data.isFavor === 'N') {
            data.isCollected = false;
        } else {
            data.isCollected = true;
        }

        // data.src += '/q/80';

        if (key < 2) {
            data.islazy = true;
        }

        formatData.push(data);
    });

    return formatData;
};

/**
 * 星潮首页
 */
const getIndexData = () => {
    return api.get('', {
        method: 'app.starClass.index',
        code: '8adc27fcf5676f356602889afcfd2a8e',
        client_type: 'iphone'
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return _processIndexData(result);
        } else {
            logger.error('star class content resource return code no 200');
            return {};
        }
    });
};

/**
 * 明星专题
 */
const getDetailData = (params, uid) => {
    return api.get('', {
        method: 'app.starClass.lastTagArticle',
        tag: params.tag,
        page: params.page || 1,
        size: 10,
        uid: uid,
        client_type: 'iphone'
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            if (params.page > result.data.totalPage) {
                return '';
            } else {
                return _processGuangData(result.data.list, true);
            }
        } else {
            logger.error('api app.starClass.lastTagArticle code no 200');
            return [];
        }
    });
};


/**
 * 星专题
 */
const getSpecialData = () => {
    return _getResources('special').then((result) => {

        // 数据结构嵌套太深
        _.forEach(result, (data, key) => {
            _.map(data.data, (item) => {
                if (!_.isObject(item)) {
                    return;
                }

                // item.src += '/q/80';

                if (key < 4) {
                    item.islazy = true;
                }
                return item;
            });
        });

        return result;
    });
};

/**
 * 星搭配
 */
const getCollocationListData = (params, uid) => {
    return serviceAPI.get('guang/api/v5/article/getStarClassroomArticleList', Object.assign({
        limit: '20',
        uid: uid
    }, params), {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return _processGuangData(result.data.list.artList);
        } else {
            logger.error('getStarClassroomArticleList code no 200');
            return [];
        }
    });
};

const setFavorite = (params, uid) => {
    if (!uid) {
        return Promise.resolve({
            code: 400,
            message: '未登录'
        });
    }

    return api.get('', {
        method: params.type === 'del' ? 'app.sns.cancelFavorBackCount' : 'app.sns.setFavorBackCount',
        client_type: 'h5',
        article_id: params.articleId,
        uid: uid
    });
};

module.exports = {
    getIndexData,
    getSpecialData,
    getCollocationListData,
    setFavorite,
    getDetailData
};