Authored by 邱骏

杂志读取

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
}
... ...
class indexModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 通过杂志id和userId获取杂志信息
* @param uid
* @param eid
* @returns {*}
*/
getMagazineInfo(uid, eid) {
return this.get({
data: {
method: 'app.eBook.queryLinkByEIdAndUserId',
user_id: uid,
e_id: eid
}
});
}
}
... ...
... ... @@ -11,11 +11,13 @@ const cRoot = './controllers';
// 杂志详情controller
const detail = require(`${cRoot}/detail`);
const magazine = require(`${cRoot}/index`);
// middlware
// Your controller here
router.get(/^\/(\d+)\.html/, detail.cover);
router.get('/index', magazine.index);
module.exports = router;
... ...
<div class="yoho-page magazine-error-page">
{{#result}}
<div class="error-tips">
{{msg}}
</div>
{{/result}}
</div>
... ...
... ... @@ -83,6 +83,7 @@
"request-promise": "^4.2.1",
"semver": "^5.5.0",
"sitemap": "^1.13.0",
"superagent": "^5.1.0",
"tencentcloud-sdk-nodejs": "^3.0.95",
"uglifyjs-webpack-plugin": "^2.1.3",
"urlencode": "^1.1.0",
... ...
html,
body {
height: 100%;
}
.main-wrap {
min-height: 100%;
}
.magazine-error-page {
position: relative;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
.error-tips {
font-size: 32px;
text-align: center;
}
}
... ...
... ... @@ -2683,7 +2683,7 @@ commoner@^0.10.1:
q "^1.1.2"
recast "^0.11.17"
component-emitter@^1.2.0, component-emitter@^1.2.1:
component-emitter@^1.2.0, component-emitter@^1.2.1, component-emitter@^1.3.0:
version "1.3.0"
resolved "http://npm.yohops.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"
integrity sha1-FuQHD7qK4ptnnyIVhT7hgasuq8A=
... ... @@ -2846,7 +2846,7 @@ cookie@0.4.0:
resolved "http://npm.yohops.com/cookie/-/cookie-0.4.0.tgz#beb437e7022b3b6d49019d088665303ebe9c14ba"
integrity sha1-vrQ35wIrO21JAZ0IhmUwPr6cFLo=
cookiejar@^2.1.0:
cookiejar@^2.1.0, cookiejar@^2.1.2:
version "2.1.2"
resolved "http://npm.yohops.com/cookiejar/-/cookiejar-2.1.2.tgz#dd8a235530752f988f9a0844f3fc589e3111125c"
integrity sha1-3YojVTB1L5iPmghE8/xYnjERElw=
... ... @@ -4363,6 +4363,11 @@ fast-safe-stringify@^2.0.4:
resolved "http://npm.yohops.com/fast-safe-stringify/-/fast-safe-stringify-2.0.6.tgz#04b26106cc56681f51a044cfc0d76cf0008ac2c2"
integrity sha1-BLJhBsxWaB9RoETPwNds8ACKwsI=
fast-safe-stringify@^2.0.6:
version "2.0.7"
resolved "http://npm.yohops.com/fast-safe-stringify/-/fast-safe-stringify-2.0.7.tgz#124aa885899261f68aedb42a7c080de9da608743"
integrity sha1-EkqohYmSYfaK7bQqfAgN6dpgh0M=
fastparse@^1.0.0, fastparse@^1.1.1:
version "1.1.2"
resolved "http://npm.yohops.com/fastparse/-/fastparse-1.1.2.tgz#91728c5a5942eced8531283c79441ee4122c35a9"
... ... @@ -4641,6 +4646,15 @@ form-data@^2.3.1:
combined-stream "^1.0.6"
mime-types "^2.1.12"
form-data@^2.3.3:
version "2.5.1"
resolved "http://npm.yohops.com/form-data/-/form-data-2.5.1.tgz#f2cbec57b5e59e23716e128fe44d4e5dd23895f4"
integrity sha1-8svsV7XlniNxbhKP5E1OXdI4lfQ=
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.6"
mime-types "^2.1.12"
form-data@~2.3.2:
version "2.3.3"
resolved "http://npm.yohops.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
... ... @@ -4650,7 +4664,7 @@ form-data@~2.3.2:
combined-stream "^1.0.6"
mime-types "^2.1.12"
formidable@^1.2.0:
formidable@^1.2.0, formidable@^1.2.1:
version "1.2.1"
resolved "http://npm.yohops.com/formidable/-/formidable-1.2.1.tgz#70fb7ca0290ee6ff961090415f4b3df3d2082659"
integrity sha1-cPt8oCkO5v+WEJBBX0s989IIJlk=
... ... @@ -7163,7 +7177,7 @@ merge2@^1.2.3:
resolved "http://npm.yohops.com/merge2/-/merge2-1.2.3.tgz#7ee99dbd69bb6481689253f018488a1b902b0ed5"
integrity sha1-fumdvWm7ZIFoklPwGEiKG5ArDtU=
methods@^1.1.1, methods@~1.1.2:
methods@^1.1.1, methods@^1.1.2, methods@~1.1.2:
version "1.1.2"
resolved "http://npm.yohops.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
integrity sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=
... ... @@ -7236,7 +7250,7 @@ mime@2.3.1:
resolved "http://npm.yohops.com/mime/-/mime-2.3.1.tgz#b1621c54d63b97c47d3cfe7f7215f7d64517c369"
integrity sha1-sWIcVNY7l8R9PP5/chX31kUXw2k=
mime@^2.0.3, mime@^2.4.2:
mime@^2.0.3, mime@^2.4.2, mime@^2.4.4:
version "2.4.4"
resolved "http://npm.yohops.com/mime/-/mime-2.4.4.tgz#bd7b91135fc6b01cde3e9bae33d659b63d8857e5"
integrity sha1-vXuRE1/GsBzePpuuM9ZZtj2IV+U=
... ... @@ -9590,6 +9604,11 @@ qs@6.7.0, qs@^6.4.0, qs@^6.5.1:
resolved "http://npm.yohops.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
integrity sha1-QdwaAV49WB8WIXdr4xr7KHapsbw=
qs@^6.7.0:
version "6.9.1"
resolved "http://npm.yohops.com/qs/-/qs-6.9.1.tgz#20082c65cb78223635ab1a9eaca8875a29bf8ec9"
integrity sha1-IAgsZct4IjY1qxqerKiHWim/jsk=
qs@~6.5.2:
version "6.5.2"
resolved "http://npm.yohops.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
... ... @@ -9766,7 +9785,7 @@ readable-stream@1.1.x, readable-stream@~1.1.9:
isarray "0.0.1"
string_decoder "~0.10.x"
readable-stream@3, readable-stream@^3.0.6, readable-stream@^3.1.1:
readable-stream@3, readable-stream@^3.0.6, readable-stream@^3.1.1, readable-stream@^3.4.0:
version "3.4.0"
resolved "http://npm.yohops.com/readable-stream/-/readable-stream-3.4.0.tgz#a51c26754658e0a3c21dbf59163bd45ba6f447fc"
integrity sha1-pRwmdUZY4KPCHb9ZFjvUW6b0R/w=
... ... @@ -11273,6 +11292,23 @@ superagent@^3.6.0:
qs "^6.5.1"
readable-stream "^2.3.5"
superagent@^5.1.0:
version "5.1.0"
resolved "http://npm.yohops.com/superagent/-/superagent-5.1.0.tgz#9ce4f38bee64d65a56166423b573222fa1b8f041"
integrity sha1-nOTzi+5k1lpWFmQjtXMiL6G48EE=
dependencies:
component-emitter "^1.3.0"
cookiejar "^2.1.2"
debug "^4.1.1"
fast-safe-stringify "^2.0.6"
form-data "^2.3.3"
formidable "^1.2.1"
methods "^1.1.2"
mime "^2.4.4"
qs "^6.7.0"
readable-stream "^3.4.0"
semver "^6.1.1"
supports-color@5.4.0:
version "5.4.0"
resolved "http://npm.yohops.com/supports-color/-/supports-color-5.4.0.tgz#1c6b337402c2137605efe19f10fec390f6faab54"
... ...