guang.js 8.05 KB
'use strict';
const css = require('../css');

// const mipUtils = require('../mip-utils');
const helpers = global.yoho.helpers;
const _ = require('lodash');
const co = require('bluebird').coroutine;
const stringProcess = require(`${global.utils}/string-process`);
const guangProcess = require(`${global.utils}/guang-process`);
const mRoot = '../models';
const DetailModel = require(`${mRoot}/guang`);
const aboutModel = require('../../../doraemon/models/about');
const typeLib = require('../../../config/type-lib');
const channels = {
    boys: 1,
    girl: 2,
    kids: 3,
    lifestyle: 4
};

// const testStr = '';

/**
 * [处理品牌数据]
 * @param  {[array]} getBrand [品牌原数据]
 */
const _relatedBrand = (getBrand, isApp) => {
    let relatedBrand = getBrand;

    relatedBrand.forEach(brand => {
        brand.thumb = brand.thumb.replace('http://', '//');

        if (isApp) {
            brand.url = brand.url + '?openby:yohobuy={"action":"go.brand","params":{"brand_id":"' + brand.id + '"}}';
        }
    });

    return relatedBrand;
};

/**
 * [处理标签数据]
 * @param  {[array]} tags [标签原数据]
 * @param  {[Boolean]} isApp [是否app]
 */
const _relatedTag = (tags, isApp) => {
    let relatedTag = [];
    let tagUrl;

    tags.forEach(value => {

        tagUrl = helpers.urlFormat('/tags/index', {query: value.name}, 'guang');

        if (!isApp) {
            value.url = tagUrl;
        } else {
            if (value.url.indexOf('openby') >= 0) {
                value.url = value.url;
            } else {
                value.url = tagUrl + '&openby:yohobuy={"action":"go.h5","params":{"query":"' + value.name + '","type":0,"title":"' + value.name + '","url":"http://guang.m.yohobuy.com/tags/index","islogin":"N"}}';
            }
        }

        relatedTag.push(value);
    });

    return relatedTag;
};

/**
 * [处理相关文章数据]
 * @param  {[array]} getOtherArticle [相关文章原数据]
 * @param  {[Boolean]} isApp [是否app]
 */
const _relatedInfo = (getOtherArticle, isApp) => {
    let relatedInfo = [];
    let articleUrl;

    getOtherArticle.forEach(value => {
        articleUrl = helpers.urlFormat('/info/index', {
            id: value.id
        }, 'guang');

        if (isApp) {
            value.url = articleUrl + '&openby:yohobuy={"action":"go.h5","params":{"id":"' + value.id + '","shareparam":{"id":"' + value.id + '"},"islogin":"N","type":1,"url":"http://guang.m.yohobuy.com/info/index","param":{"id":"' + value.id + '"}}}';
        } else {
            value.url = articleUrl;
        }

        value.thumb = helpers.image(value.thumb, 279, 175);
        relatedInfo.push(value);
    });

    return relatedInfo;
};

/**
 * [处理分享内容]
 * @param  {[int]} id [资讯id]
 * @param  {[array]} getArticle [资讯内容]
 */
const _shareInfo = (id, getArticle) => {
    let shareInfo = {};

    shareInfo.shareLink = 'http:' + helpers.urlFormat('/info/index', {
        id: id
    }, 'guang');
    shareInfo.shareTitle = getArticle.article_title;
    shareInfo.shareDesc = getArticle.article_summary;
    if (getArticle.cover_image_type === 1) {
        shareInfo.shareImg = 'http:' + helpers.image(getArticle.cover_image, 640, 640);
    } else {
        shareInfo.shareImg = 'http:' + helpers.image(getArticle.cover_image, 640, 320);
    }
    return shareInfo;
};

const detailIndex = (req, res, next) => {
    let id = req.query.id || req.params[0] || req.params.id,
        gender = req.query.gender ||
            req.query.channel && typeLib.channels[req.query.channel] ||
            req.cookies._Channel && channels[req.cookies._Channel] ||
            1,
        isApp = req.query.app_version || req.query.appVersion || false, // 标识是不是APP访问的
        isWeixin = req.yoho.isWechat,
        channel = req.query.channel || req.cookies._Channel,
        isqq = req.yoho.isqq,
        isWeibo = req.yoho.isWeibo,
        isMip = true,
        isShare;

    res.locals.appPath = `yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.h5","params":{"id":"${id}","share":"/guang/api/v1/share/guang?id=${id}","shareparam":{"id":"${id}"},"islogin":"N","type":1,"url":"http://guang.m.yohobuy.com/info/index","param":{"id":"${id}"}}}`;

    // 判断参数是否有效, 无效会跳转到错误页面
    if (!stringProcess.isNumeric(id)) {
        res.json({
            code: 400,
            message: '非法请求',
            data: ''
        });
        return;
    }

    isShare = isWeixin || isqq || isWeibo ? true : false;

    co(function* () {
        // let mipData = mipUtils.process(testStr, 0);
        let detail = yield req.ctx(DetailModel).packageData(id, isApp, isWeixin, channel, isShare);
        let data = {
            guangDetail: true,
            guang: {}
        };

        data.guang.isWeixin = isWeixin;
        data.guang.channel = channel;
        data.guang.isShare = isShare;

        if (detail.code === 400) {
            return next();
        }
        if (!detail.getArticle) {
            // TODO 跳转到逛首页
            return;
        }

        if (isShare && detail && detail.sideNav) {
            data.sideNav = detail.sideNav;
        }

        // 作者信息数据
        if (detail && detail.getAuthor && (typeof detail.getAuthor.name !== 'undefined')) {
            data.guang.author = {
                avatar: detail.getAuthor.avatar.replace('http://', '//'),
                name: detail.getAuthor.name,
                intro: detail.getAuthor.author_desc
            };

            // guang双头部的问题 20160601
            // 正确的URL
            let url = `${detail.getAuthor.url}&openby:yohobuy={"action":"go.h5","params":{"param":{},"share":"","id":${detail.getAuthor.author_id},"type":0,"islogin":"N","url":"${detail.getAuthor.url}"}}`; // eslint-disable-line

            data.guang.author.url = helpers.https(url);
        }
        let guang = data.guang;

        guang.detail = {
            id: _.get(detail, 'getArticle.id'),
            title: detail.getArticle.article_title,
            publishTime: detail.getArticle.publishTime,
            pageView: detail.getArticle.pageViews,
            content: []
        };
        if (!detail.getArticleContent) {
            return next();
        }

        let processContents = guangProcess.processArticleDetail(detail.getArticleContent,
            isApp,
            gender,
            isWeixin,
            isqq,
            isWeibo, isMip);

        let goodsList = yield req.ctx(DetailModel).productInfoBySkns(processContents.allgoods);

        guang.detail.content = guangProcess.pushGoodsInfo(processContents.finalDetail, goodsList, isApp);

        // 相关品牌
        if (detail.getBrand && detail.getBrand.length) {
            guang.relatedBrand = _relatedBrand(detail.getBrand, isApp);
        }

        // 相关标签
        if (detail.getArticle.tags && detail.getArticle.tags.length) {
            guang.relatedTag = _relatedTag(detail.getArticle.tags, isApp);
        }

        // 相关文章
        if (detail.getOtherArticle && detail.getOtherArticle.length) {
            guang.relatedInfo = _relatedInfo(detail.getOtherArticle, isApp);
        }

        // 分享参数
        if (detail.getArticle.cover_image) {
            let shareInfo = _shareInfo(id, detail.getArticle);

            Object.assign(guang, shareInfo);
            data.title = detail.getArticle.article_title + ' | Yoho!Buy有货 | 潮流购物逛不停';
            data.title_more = true;
            data.description = detail.getArticle.article_summary;
            data.description_more = true;
        }

        // 标识有微信分享
        data.hasWxShare = true;

        let resu = yield aboutModel.about(req.yoho.isApp);

        data.guang.wxFooter = resu;

        return res.render('guang/detail', Object.assign({
            css: yield css('guang/detail.css'),
            width750: true,
            title: '逛',
            gender: gender,
            wechatShare: true,
            isWeixin: isWeixin,
            isShare: isShare,
            localStyle: processContents.css
        }, data));
    })().catch(next);
};

module.exports = {
    detailIndex
};