rss.js
3.72 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
95
96
97
98
99
100
101
102
103
104
105
/**
* 逛rss订阅models
* @author: chenfeng<feng.chen@yoho.cn>
* @date: 2016/09/13
*/
'use strict';
const mRoot = '../models';
const IndexModel = require(`${mRoot}/index`);
const DetailModel = require(`${mRoot}/detail`);
const guangProcess = require(`${global.utils}/guang-process`);
const htmlProcess = require(`${global.utils}/html-process`);
const helpers = global.yoho.helpers;
class RssModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.indexModel = new IndexModel(ctx);
this.detailModel = new DetailModel(ctx);
}
/**
* [构建信息内容]
* @param {[int]} id [资讯id]
* @return {[string]}
*/
_genIntro(id) {
let result = '';
return this.detailModel.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]}
*/
getRssArticle(gender) {
let result = [],
articlePromise;
switch (gender) {
case '1,3':
articlePromise = this.indexModel.getArticleList('1,3', 0, 0, '', 1, null, null, 100, false);
break;
case '2,3':
articlePromise = this.indexModel.getArticleList('2,3', 0, 0, '', 1, null, null, 100, false);
break;
default:
articlePromise = this.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 this._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 = RssModel;