Authored by 邱骏

优化

... ... @@ -22,12 +22,8 @@ const indexModel = require('../models/index');
* @param next
*/
const index = async function(req, res, next) {
let userAgent = req.get('User-Agent');
let {uid, app_version, app_client_type, session_key, e_id} = req.query;
let isMiniApp = /miniprogram/i.test(userAgent) || false;
let isApp = /yohobuy/i.test(userAgent) || false;
let appSessionType = app_client_type || 'h5';
let {e_id} = req.query;
let isApp = req.yoho.isApp;
/**
* 用于渲染错误结果页面
... ... @@ -49,7 +45,7 @@ const index = async function(req, res, next) {
}
// 如果不在小程序或app中打开则返回
if (!isMiniApp && !isApp) {
if (!isApp) {
return renderErroPage({msg: WRONG_INVIROMENT});
}
... ... @@ -58,52 +54,29 @@ const index = async function(req, res, next) {
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';
}
}
let user_id = {};
// 查看session中是否有登录信息
if (req.user.uid) {
user_id = {
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 { // session中没有都情况下查看url参数中有没有登录信息
if (!uid || !session_key) {
if (!req.user.uid || !req.user.uid.toString() || !req.user.uid.session_key) {
return res.redirect(`//m.yohobuy.com/magazine/${e_id}.html`);
} else {
let intUid = parseInt(uid, 10);
user_id = {
toString: () => {
return intUid ? intUid : 0;
},
appVersion: app_version || global.yoho.config.appVersion,
sessionKey: session_key,
appSessionType: app_client_type || appSessionType
};
}
}
let magazineInfo = await req.ctx(indexModel).getMagazineInfo(user_id, e_id).catch(next);
// 通过接口获取到杂志对应的实际的url,并进行处理
let magazineInfo = await req.ctx(indexModel).getMagazineInfo(req.user.uid, e_id).catch(next);
console.log(magazineInfo);
if (magazineInfo && magazineInfo.code === 200 && magazineInfo.data) {
let code = magazineInfo.data.code; // 4000:获取成功 4001: 未购买该电子刊 4002:未获取到电子刊 4003:电子刊已删除
let code = magazineInfo.data.code;
// 4000:获取成功 4001: 未购买该电子刊 4002:未获取到电子刊 4003:电子刊已删除
if (code === 4000) {
let url = magazineInfo.data.link;
if (!url) {
return renderErroPage({msg: '杂志地址获取失败'});
}
let resourceUrl = url.substring(0, url.lastIndexOf('/') + 1);
superAgent.get(url).end(function(err, response) {
... ... @@ -114,6 +87,7 @@ const index = async function(req, res, next) {
let $ = cheerio.load(response.text);
let idMatch = response.text.match(/id="(.*)_hype_container"/);
// HYPE生成的html有其固定格式的div id,此处获取的也是这个hype创建时的名称,此名称对应插入的js
if (idMatch && idMatch.length >= 2) {
let name = idMatch[1];
let id = `${name}_hype_container`;
... ... @@ -121,6 +95,7 @@ const index = async function(req, res, next) {
let scriptSrc = script.match(/src="(.*)"/);
// 获取到对应div里面的script的src,并替换成带有域名的地址,可以让所有图片地址指向js对应的域名
if (scriptSrc && scriptSrc.length >= 2) {
let src = scriptSrc[1];
let newScript = '<script type="text/javascript" charset="utf-8" src="' +
... ... @@ -128,7 +103,7 @@ const index = async function(req, res, next) {
$(`#${id}`).html(newScript);
return res.send($.html());
return res.send($.html()); // 输出修改过的html
} else {
return renderErroPage({msg: NO_HYPE_SCRIPT});
}
... ...
... ... @@ -13,7 +13,8 @@ class indexModel extends global.yoho.BaseModel {
return this.get({
data: {
method: 'app.eBook.queryLinkByEIdAndUserId',
user_id: uid,
uid,
user_id: uid.toString(),
e_id: eid,
type: 1 // 传type的情况下才能拿到杂志url
}
... ...