index.js 3.43 KB
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
}