Authored by 邱骏

优化

@@ -22,12 +22,8 @@ const indexModel = require('../models/index'); @@ -22,12 +22,8 @@ const indexModel = require('../models/index');
22 * @param next 22 * @param next
23 */ 23 */
24 const index = async function(req, res, next) { 24 const index = async function(req, res, next) {
25 -  
26 - let userAgent = req.get('User-Agent');  
27 - let {uid, app_version, app_client_type, session_key, e_id} = req.query;  
28 - let isMiniApp = /miniprogram/i.test(userAgent) || false;  
29 - let isApp = /yohobuy/i.test(userAgent) || false;  
30 - let appSessionType = app_client_type || 'h5'; 25 + let {e_id} = req.query;
  26 + let isApp = req.yoho.isApp;
31 27
32 /** 28 /**
33 * 用于渲染错误结果页面 29 * 用于渲染错误结果页面
@@ -49,7 +45,7 @@ const index = async function(req, res, next) { @@ -49,7 +45,7 @@ const index = async function(req, res, next) {
49 } 45 }
50 46
51 // 如果不在小程序或app中打开则返回 47 // 如果不在小程序或app中打开则返回
52 - if (!isMiniApp && !isApp) { 48 + if (!isApp) {
53 return renderErroPage({msg: WRONG_INVIROMENT}); 49 return renderErroPage({msg: WRONG_INVIROMENT});
54 } 50 }
55 51
@@ -58,52 +54,29 @@ const index = async function(req, res, next) { @@ -58,52 +54,29 @@ const index = async function(req, res, next) {
58 return renderErroPage({msg: NO_MAGAZINE_ID}); 54 return renderErroPage({msg: NO_MAGAZINE_ID});
59 } 55 }
60 56
61 - if (isMiniApp) {  
62 - appSessionType = 'miniapp';  
63 - } else if (isApp) {  
64 - if (/i(phone|pad|pod)/i.test(userAgent)) {  
65 - appSessionType = 'iphone';  
66 - } else {  
67 - appSessionType = 'android';  
68 - }  
69 - }  
70 -  
71 - let user_id = {};  
72 -  
73 // 查看session中是否有登录信息 57 // 查看session中是否有登录信息
74 - if (req.user.uid) {  
75 - user_id = {  
76 - toString: () => {  
77 - return req.user.uid.toString();  
78 - },  
79 - appVersion: req.user.uid.appVersion || global.yoho.config.appVersion,  
80 - sessionKey: req.user.uid.sessionKey,  
81 - appSessionType: req.user.uid.appSessionType || appSessionType  
82 - };  
83 - } else { // session中没有都情况下查看url参数中有没有登录信息  
84 - if (!uid || !session_key) {  
85 - return res.redirect(`//m.yohobuy.com/magazine/${e_id}.html`);  
86 - } else {  
87 - let intUid = parseInt(uid, 10);  
88 -  
89 - user_id = {  
90 - toString: () => {  
91 - return intUid ? intUid : 0;  
92 - },  
93 - appVersion: app_version || global.yoho.config.appVersion,  
94 - sessionKey: session_key,  
95 - appSessionType: app_client_type || appSessionType  
96 - };  
97 - } 58 +
  59 + if (!req.user.uid || !req.user.uid.toString() || !req.user.uid.session_key) {
  60 + return res.redirect(`//m.yohobuy.com/magazine/${e_id}.html`);
98 } 61 }
99 62
100 - let magazineInfo = await req.ctx(indexModel).getMagazineInfo(user_id, e_id).catch(next); 63 + // 通过接口获取到杂志对应的实际的url,并进行处理
  64 + let magazineInfo = await req.ctx(indexModel).getMagazineInfo(req.user.uid, e_id).catch(next);
  65 +
  66 + console.log(magazineInfo);
101 67
102 if (magazineInfo && magazineInfo.code === 200 && magazineInfo.data) { 68 if (magazineInfo && magazineInfo.code === 200 && magazineInfo.data) {
103 - let code = magazineInfo.data.code; // 4000:获取成功 4001: 未购买该电子刊 4002:未获取到电子刊 4003:电子刊已删除 69 + let code = magazineInfo.data.code;
  70 +
  71 + // 4000:获取成功 4001: 未购买该电子刊 4002:未获取到电子刊 4003:电子刊已删除
104 72
105 if (code === 4000) { 73 if (code === 4000) {
106 let url = magazineInfo.data.link; 74 let url = magazineInfo.data.link;
  75 +
  76 + if (!url) {
  77 + return renderErroPage({msg: '杂志地址获取失败'});
  78 + }
  79 +
107 let resourceUrl = url.substring(0, url.lastIndexOf('/') + 1); 80 let resourceUrl = url.substring(0, url.lastIndexOf('/') + 1);
108 81
109 superAgent.get(url).end(function(err, response) { 82 superAgent.get(url).end(function(err, response) {
@@ -114,6 +87,7 @@ const index = async function(req, res, next) { @@ -114,6 +87,7 @@ const index = async function(req, res, next) {
114 let $ = cheerio.load(response.text); 87 let $ = cheerio.load(response.text);
115 let idMatch = response.text.match(/id="(.*)_hype_container"/); 88 let idMatch = response.text.match(/id="(.*)_hype_container"/);
116 89
  90 + // HYPE生成的html有其固定格式的div id,此处获取的也是这个hype创建时的名称,此名称对应插入的js
117 if (idMatch && idMatch.length >= 2) { 91 if (idMatch && idMatch.length >= 2) {
118 let name = idMatch[1]; 92 let name = idMatch[1];
119 let id = `${name}_hype_container`; 93 let id = `${name}_hype_container`;
@@ -121,6 +95,7 @@ const index = async function(req, res, next) { @@ -121,6 +95,7 @@ const index = async function(req, res, next) {
121 95
122 let scriptSrc = script.match(/src="(.*)"/); 96 let scriptSrc = script.match(/src="(.*)"/);
123 97
  98 + // 获取到对应div里面的script的src,并替换成带有域名的地址,可以让所有图片地址指向js对应的域名
124 if (scriptSrc && scriptSrc.length >= 2) { 99 if (scriptSrc && scriptSrc.length >= 2) {
125 let src = scriptSrc[1]; 100 let src = scriptSrc[1];
126 let newScript = '<script type="text/javascript" charset="utf-8" src="' + 101 let newScript = '<script type="text/javascript" charset="utf-8" src="' +
@@ -128,7 +103,7 @@ const index = async function(req, res, next) { @@ -128,7 +103,7 @@ const index = async function(req, res, next) {
128 103
129 $(`#${id}`).html(newScript); 104 $(`#${id}`).html(newScript);
130 105
131 - return res.send($.html()); 106 + return res.send($.html()); // 输出修改过的html
132 } else { 107 } else {
133 return renderErroPage({msg: NO_HYPE_SCRIPT}); 108 return renderErroPage({msg: NO_HYPE_SCRIPT});
134 } 109 }
@@ -13,7 +13,8 @@ class indexModel extends global.yoho.BaseModel { @@ -13,7 +13,8 @@ class indexModel extends global.yoho.BaseModel {
13 return this.get({ 13 return this.get({
14 data: { 14 data: {
15 method: 'app.eBook.queryLinkByEIdAndUserId', 15 method: 'app.eBook.queryLinkByEIdAndUserId',
16 - user_id: uid, 16 + uid,
  17 + user_id: uid.toString(),
17 e_id: eid, 18 e_id: eid,
18 type: 1 // 传type的情况下才能拿到杂志url 19 type: 1 // 传type的情况下才能拿到杂志url
19 } 20 }