index.js
3.43 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
const cheerio = require('cheerio');
const superAgent = require('superagent');
const INVALID_PARAMS = '缺少参数';
const NO_MAGAZINE_ID = '无效的杂志编号'; // 没有杂志id
const READ_MAGAZINE_URL_ERROR = '杂志内容读取失败,请重试';
const NO_HYPE_CONTAINER_ID = '杂志格式错误!'; // 找不到hype_container的id
const NO_HYPE_SCRIPT = '无效的杂志内容!'; // 找不到hype_script
const WRONG_INVIROMENT = '杂志获取失败!'; // 不在有货app也不在小程序中打开的时候提示
const headerModel = require('../../../doraemon/models/header');
/**
* 获取到杂志的url之后使用superAgent读取html内容并进行处理后输出
* @param req
* @param res
* @param next
*/
const index = function(req, res, next) {
let userAgent = req.headers['user-agent'];
let {isTest, uid, app_version, app_client_type, session_key, e_id} = req.query;
let userId = {};
let isMiniApp = /miniprogram/i.test(userAgent) || false;
let isApp = /yohobuy/i.test(userAgent) || false;
let appSessionType = app_client_type || 'h5';
function renderErroPage(result) {
return res.render('index/error', {
module: 'magazine',
page: 'error',
title: '错误提示',
localCss: true,
result: result
});
}
if (!isMiniApp && !isApp) {
if (!isTest) {
return renderErroPage({msg: WRONG_INVIROMENT});
}
}
if (!e_id) {
return renderErroPage({msg: NO_MAGAZINE_ID});
}
if (isMiniApp) {
appSessionType = 'miniapp';
} else if (isApp) {
if (/i(phone|pad|pod)/i.test(userAgent)) {
appSessionType = 'iphone';
} else {
appSessionType = 'android';
}
}
if (req.user.uid) {
console.log(req.user.uid, global.yoho);
userId = {
toString: () => {
return req.user.uid.toString();
},
appVersion: req.user.uid.appVersion || global.yoho.config.appVersion,
sessionKey: req.user.uid.sessionKey,
appSessionType: req.user.uid.appSessionType || appSessionType
};
} else {
if (!uid || !session_key) {
return res.redirect(`//m.yohobuy.com/magazine/${e_id}.html`);
}
}
let url = 'https://feature.yoho.cn/magazine/20190930/01/huangzitao/huangzitao.html';
let resourceUrl = url.substring(0, url.lastIndexOf('/') + 1);
superAgent.get(url).end(function(err, response) {
if (err) {
return res.send(READ_MAGAZINE_URL_ERROR);
}
// console.log(response.text);
let $ = cheerio.load(response.text);
let idMatch = response.text.match(/id="(.*)_hype_container/);
if (idMatch && idMatch.length >= 2) {
let name = idMatch[1];
let id = `${name}_hype_container`;
let script = $(`#${id}`).html();
let scriptSrc = script.match(/src="(.*)"/);
if (scriptSrc && scriptSrc.length >= 2) {
let src = scriptSrc[1];
let newScript = '<script type="text/javascript" charset="utf-8" src="' + resourceUrl + src + '">';
$(`#${id}`).html(newScript);
return res.send($.html());
} else {
return res.send(NO_HYPE_SCRIPT);
}
} else {
return res.send(NO_HYPE_CONTAINER_ID);
}
});
};
module.exports = {
index
}