guang.js
7.53 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict';
const css = require('../css');
// const mipUtils = require('../mip-utils');
const helpers = global.yoho.helpers;
const _ = require('lodash');
const co = require('bluebird').coroutine;
const stringProcess = require(`${global.utils}/string-process`);
const guangProcess = require(`${global.utils}/guang-process`);
const mRoot = '../models';
const DetailModel = require(`${mRoot}/guang`);
const typeLib = require('../../../config/type-lib');
const moment = require('moment');
const channels = {
boys: 1,
girl: 2,
kids: 3,
lifestyle: 4
};
const mipDate = (day) => {
return moment.unix(day).format('YYYY-MM-DDTHH:mm:ss');
};
// const testStr = '';
/**
* [处理品牌数据]
* @param {[array]} getBrand [品牌原数据]
*/
const _relatedBrand = (getBrand, isApp) => {
let relatedBrand = getBrand;
relatedBrand.forEach(brand => {
brand.thumb = brand.thumb.replace('http://', '//');
if (isApp) {
brand.url = brand.url + '?openby:yohobuy={"action":"go.brand","params":{"brand_id":"' + brand.id + '"}}';
}
});
return relatedBrand;
};
/**
* [处理标签数据]
* @param {[array]} tags [标签原数据]
* @param {[Boolean]} isApp [是否app]
*/
const _relatedTag = (tags, isApp) => {
let relatedTag = [];
let tagUrl;
tags.forEach(value => {
tagUrl = helpers.urlFormat('/tags/index', {query: value.name}, 'guang');
if (!isApp) {
value.url = tagUrl;
} else {
if (value.url.indexOf('openby') >= 0) {
value.url = value.url;
} else {
value.url = tagUrl + '&openby:yohobuy={"action":"go.h5","params":{"query":"' + value.name + '","type":0,"title":"' + value.name + '","url":"http://guang.m.yohobuy.com/tags/index","islogin":"N"}}';
}
}
relatedTag.push(value);
});
return relatedTag;
};
/**
* [处理相关文章数据]
* @param {[array]} getOtherArticle [相关文章原数据]
* @param {[Boolean]} isApp [是否app]
*/
const _relatedInfo = (getOtherArticle, isApp) => {
let relatedInfo = [];
let articleUrl;
getOtherArticle.forEach(value => {
articleUrl = helpers.urlFormat('/info/index', {
id: value.id
}, 'guang');
if (isApp) {
value.url = articleUrl + '&openby:yohobuy={"action":"go.h5","params":{"id":"' + value.id + '","shareparam":{"id":"' + value.id + '"},"islogin":"N","type":1,"url":"http://guang.m.yohobuy.com/info/index","param":{"id":"' + value.id + '"}}}';
} else {
value.url = articleUrl;
}
value.thumb = helpers.image(value.thumb, 279, 175);
relatedInfo.push(value);
});
return relatedInfo;
};
const detailIndex = (req, res, next) => {
let id = req.query.id || req.params[0] || req.params.id,
gender = req.query.gender ||
req.query.channel && typeLib.channels[req.query.channel] ||
req.cookies._Channel && channels[req.cookies._Channel] ||
1,
isApp = req.query.app_version || req.query.appVersion || false, // 标识是不是APP访问的
isWeixin = req.yoho.isWechat,
channel = req.query.channel || req.cookies._Channel,
isqq = req.yoho.isqq,
isWeibo = req.yoho.isWeibo,
isMip = true,
isShare;
res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.h5","params":{"id":"${id}","share":"/guang/api/v1/share/guang?id=${id}","shareparam":{"id":"${id}"},"islogin":"N","type":1,"url":"http://guang.m.yohobuy.com/info/index","param":{"id":"${id}"}}}`;
// 判断参数是否有效, 无效会跳转到错误页面
if (!stringProcess.isNumeric(id)) {
res.json({
code: 400,
message: '非法请求',
data: ''
});
return;
}
isShare = isWeixin || isqq || isWeibo ? true : false;
co(function* () {
// let mipData = mipUtils.process(testStr, 0);
let detail = yield req.ctx(DetailModel).packageData(id, isApp, isWeixin, channel, isShare);
let data = {
guangDetail: true,
guang: {}
};
data.guang.isWeixin = isWeixin;
data.guang.channel = channel;
data.guang.isShare = isShare;
if (detail.code === 400) {
return next();
}
if (!detail.getArticle) {
// TODO 跳转到逛首页
return;
}
if (isShare && detail && detail.sideNav) {
data.sideNav = detail.sideNav;
}
// 作者信息数据
if (detail && detail.getAuthor && (typeof detail.getAuthor.name !== 'undefined')) {
data.guang.author = {
avatar: detail.getAuthor.avatar.replace('http://', '//'),
name: detail.getAuthor.name,
intro: detail.getAuthor.author_desc
};
// guang双头部的问题 20160601
// 正确的URL
let url = `${detail.getAuthor.url}&openby:yohobuy={"action":"go.h5","params":{"param":{},"share":"","id":${detail.getAuthor.author_id},"type":0,"islogin":"N","url":"${detail.getAuthor.url}"}}`; // eslint-disable-line
data.guang.author.url = helpers.https(url);
}
let guang = data.guang;
let mipPublishTime = mipDate(detail.getArticle.publish_time);
let singleImages = _.filter(detail.getArticleContent, {singleImage: {template_name: 'single_image'}});
let mipPic = _.get(singleImages, '[0].singleImage.data[0].src', '');
guang.detail = {
id: _.get(detail, 'getArticle.id'),
title: detail.getArticle.article_title,
publishTime: moment.unix(detail.getArticle.publish_time).format('YYYY-MM-DD HH:mm:ss'),
pageView: detail.getArticle.pageViews,
content: []
};
if (!detail.getArticleContent) {
return next();
}
let processContents = guangProcess.processArticleDetail(detail.getArticleContent,
isApp,
gender,
isWeixin,
isqq,
isWeibo, isMip);
let goodsList = yield req.ctx(DetailModel).productInfoBySkns(processContents.allgoods);
guang.detail.content = guangProcess.pushGoodsInfo(processContents.finalDetail, goodsList, isApp);
// 相关品牌
if (detail.getBrand && detail.getBrand.length) {
guang.relatedBrand = _relatedBrand(detail.getBrand, isApp);
}
// 相关标签
if (detail.getArticle.tags && detail.getArticle.tags.length) {
guang.relatedTag = _relatedTag(detail.getArticle.tags, isApp);
}
// 相关文章
if (detail.getOtherArticle && detail.getOtherArticle.length) {
guang.relatedInfo = _relatedInfo(detail.getOtherArticle, isApp);
}
return res.render('guang/detail', Object.assign({
css: yield css('guang/detail.css'),
gender: gender,
localStyle: processContents.css,
title: detail.getArticle.article_title + '-YOHO!BUY有货',
canonical: {
currentHref: `https://m.yohobuy.com/guang/${id}.html`
},
showAnaJson: {
title: `${detail.getArticle.article_title}-YOHO!BUY有货`,
publishTime: mipPublishTime,
pic: mipPic,
des: detail.getArticle.article_summary,
url: `${req.protocol}://m.yohobuy.com${req.originalUrl}`,
}
}, data));
})().catch(next);
};
module.exports = {
detailIndex
};