rss.js 3.25 KB
/**
 * 逛rss订阅models
 * @author: chenfeng<feng.chen@yoho.cn>
 * @date: 2016/09/13
 */
'use strict';
const mRoot = '../models';
const indexModel = require(`${mRoot}/index`);
const infoModel = require(`${mRoot}/info`);
const guangProcess = require(`${global.utils}/guang-process`);
const htmlProcess = require(`${global.utils}/html-process`);
const helpers = global.yoho.helpers;

/**
 * [构建信息内容]
 * @param  {[int]} id [资讯id]
 * @return {[string]}
 */
const _genIntro = (id) => {
    let result = '';

    return infoModel.intro(id).then((resData) => {
        if (resData.data) {
            resData.data.forEach((value) => {
                if (value.text) { // 文字
                    result += htmlProcess.removeHtml(htmlProcess.escapeToHtml(value.text.data.text)) + '<br/>';
                } else if (value.singleImage) { // 单张图
                    result += `<img src="${helpers.image(value.singleImage.data[0].src, 640, 640)}"/><br/>`;
                } else if (value.smallPic && value.smallPic.data) { // 多张小图
                    value.smallPic.data.forEach((small) => {
                        result += `<img src="${helpers.image(small.src, 315, 420)}"/>`;
                    });
                    result += '<br/>';
                }
            });

            result += `<a href="http://guang.m.yohobuy.com/info/index?id=${id}">查看原文</a>`;
            return result;
        }
    });

};

/**
 * [获取订阅的资讯文章列表]
 * @param  {[int]} gender [性别参数(1,3表示男, 2,3表示女, 默认为所有)]
 * @return {[array]}
 */
const getRssArticle = (gender) => {
    let result = [],
        articlePromise;

    switch (gender) {
        case '1,3':
            articlePromise = indexModel.getArticleList('1,3', 0, 0, '', 1, null, null, 100, false);
            break;
        case '2,3':
            articlePromise = indexModel.getArticleList('2,3', 0, 0, '', 1, null, null, 100, false);
            break;
        default:
            articlePromise = indexModel.getArticleList('1,2,3', 0, 0, '', 1, null, null, 100, false);
            break;
    }
    return articlePromise.then((article) => {
        // 内容列表
        if (article && article.data && article.data.list && article.data.list.artList) {
            let build = {};

            let artListFunc = (i, len, resolve) => {
                if (i < len) {
                    let value = article.data.list.artList[i];

                    if (typeof value.id !== 'undefined') {
                        build = guangProcess.formatArticle(value, false, false, true);
                        build.author = build.author || {};
                        build.author.name = (build.author && build.author.name) || '';
                        return _genIntro(value.id).then((intro) => {
                            build.intro = intro;
                            result.push(build);
                            artListFunc(++i, len, resolve);
                        });
                    }
                } else {
                    resolve(result);
                }
            };

            return new Promise((resolve) => {
                artListFunc(0, article.data.list.artList.length, resolve);
            });

        }
    });
};


module.exports = {
    getRssArticle
};