guang.js 7.53 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 typeLib = require('../../../config/type-lib');
const moment = require('moment');
const channels = {
    boys: 1,
    girl: 2,
    kids: 3,
    lifestyle: 4
};

const mipDate = (day) => {
    return moment.unix(day).format('YYYY-MM-DDTHH:mm:ss');
};

// 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;
};

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;
        let mipPublishTime = mipDate(detail.getArticle.publish_time);
        let singleImages = _.filter(detail.getArticleContent, {singleImage: {template_name: 'single_image'}});
        let mipPic = _.get(singleImages, '[0].singleImage.data[0].src', '');

        guang.detail = {
            id: _.get(detail, 'getArticle.id'),
            title: detail.getArticle.article_title,
            publishTime: moment.unix(detail.getArticle.publish_time).format('YYYY-MM-DD HH:mm:ss'),
            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);
        }

        return res.render('guang/detail', Object.assign({
            css: yield css('guang/detail.css'),
            gender: gender,
            localStyle: processContents.css,
            title: detail.getArticle.article_title + '-YOHO!BUY有货',
            canonical: {
                currentHref: `https://m.yohobuy.com/guang/${id}.html`
            },
            showAnaJson: {
                title: `${detail.getArticle.article_title}-YOHO!BUY有货`,
                publishTime: mipPublishTime,
                pic: mipPic,
                des: detail.getArticle.article_summary,
                url: `${req.protocol}://m.yohobuy.com${req.originalUrl}`,
            }
        }, data));
    })().catch(next);
};

module.exports = {
    detailIndex
};