rss.js
4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
* 逛rss订阅models
* @author: chenfeng<feng.chen@yoho.cn>
* @date: 2016/09/13
*/
'use strict';
const serviceAPI = global.yoho.ServiceAPI;
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]} gender [性别参数(1,3表示男, 2,3表示女, 默认为所有)]
* @return {[array]}
*/
const getRssArticle = (gender) => {
let result = [],
article = {},
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.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.name = 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);
});
}
});
};
/**
* [构建信息内容]
* @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/>';
}
});
return result;
}
});
};
module.exports = {
getRssArticle
};