new-detail.js 7.15 KB
/**
 * 新版商品详情页面
 */
'use strict';
const _ = require('lodash');

const detailModel = require('../models/detail');
const newDetailModel = require('../models/new-detail');
const listModel = require('../models/list');
const headerModel = require('../../../doraemon/models/header'); // 头部model
const qs = require('querystring');
const tdk = require('../../../utils/getTDK');
const helpers = global.yoho.helpers;

const newDetail = {
    index(req, res, next) {
        let id;
        let goodsId;
        let productSkn;

        if (req.query.id) {
            id = req.query.id;
        } else if (req.params[0] && req.params[1]) {
            id = req.params[0];
            goodsId = req.params[1];
        } else if (req.params[0]) {
            productSkn = req.params[0];
        } else {
            return next();
        }

        let headerData = headerModel.setNav({
            navTitle: '商品详情'
        });

        tdk('skn', productSkn, req).then(ret => {
            if (ret[0]) {
                req.tdk = {
                    title: ret[1],
                    keywords: ret[2],
                    description: ret[3]
                };
            }

            return req.ctx(newDetailModel).getProductData({
                id: id,
                goodsId: goodsId,
                productSkn: productSkn,
                ua: req.get('user-agent') || ''
            });
        }).then((result) => {
            if (_.isEmpty(result)) {
                return next();
            }

            let appParams = {product_skn: result.productSkn};
            let appPath = 'yohobuy://yohobuy.com/goapp?openby:yohobuy={"action":"go.productDetail","params":' +
                JSON.stringify(appParams) +
                '}';

            res.render('detail/new/detail', {
                pageHeader: headerData,
                result: result,
                page: 'new-detail',
                title: result.goodsName + result.sortName + '正品 | YOHO!BUY 有货',
                keywords: result.brandName + result.sortName + ',' + result.brandName + '官网专卖店,' +
                result.brandName + '官方授权店,' + result.brandName + '正品,' + result.brandName + '打折,' +
                result.brandName + '折扣店,' +
                result.brandName + '真品,' + result.brandName + '代购',
                description: `YOHO!BUY 有货-${result.brandName}官方授权店,${result.goodsName}图片、报价、介绍。` +
        `YOHO!BUY 有货${result.brandName}官网专卖店提供${result.brandName}正品、${result.brandName}真品、 ${result.brandName}打折、${result.brandName}代购等。`, // eslint-disable-line
                pageFooter: true,
                localCss: true,
                appPath: appPath,
                isMarsApp: req.yoho.isMarsApp // 判断mars
            });
        }).catch(next);
    },

    // 301到新路由
    indexRedirect(req, res, next) {
        let param = qs.stringify(req.query);

        if (param) {
            param = '?' + param;
        }
        return new Promise((resolve, reject) => {
            if (req.params[0] && req.params[1]) {
                req.ctx(newDetailModel).getProductData({
                    id: req.params[0],
                    ua: req.get('user-agent') || ''
                }).then(result => {
                    if (_.isEmpty(result)) {
                        return reject();
                    }
                    resolve(result.productSkn);
                }).catch(next);
            } else if (req.params[0]) {
                resolve(req.params[0]);
            } else {
                reject();
            }
        }).then(skn => {
            return res.redirect(301, `/product/${skn}.html${param}`);
        }, () => {
            return next();
        });

    },


    /*
     *   商品基本信息 SKN 进入 pagecache重构
     */
    sknData(req, res, next) {
        let brandId = req.query.brandId;
        let shopId = req.query.shopId;
        let productId = req.query.productId;
        let skn = req.query.skn;
        let bundleType = req.query.bundleType;
        let gid = req.query.gid;

        if ([brandId, productId].some(val => !val)) {
            return next();
        }

        req.ctx(newDetailModel).querySknData({
            skn,
            brandId,
            shopId,
            productId,
            bundleType,
            gid
        }).then(result => {
            if (_.isEmpty(result)) {
                return next();
            }

            res.json(result);
        });
    },

    indexData(req, res, next) {
        // for guang
        let allowOrigin = _.get(req, 'headers.origin', null) ?
            req.headers.origin : req.protocol + '://guang.' + req.headers.host;

        res.setHeader('Access-Control-Allow-Origin', allowOrigin);
        res.setHeader('Access-Control-Allow-Credentials', 'true');

        if (!req.query.id && !req.query.productSkn) {
            return next();
        }

        let uid = req.user.uid || 0;
        let shoppingKey = req.cookies._SPK || '';
        let closeBuyNowButton = _.get(req.app.locals.wap, 'close.buyNowButton', false);

        req.ctx(detailModel).getNewProductAsyncData(Object.assign({
            id: req.query.id,
            goodsId: req.query.goodsId,
            productSkn: req.query.productSkn,
            bundleType: req.query.bundleType,
            uid: uid,
            shoppingKey: shoppingKey,
            ua: req.get('user-agent') || '',
            from: req.query.from,
            closeBuyNowButton: closeBuyNowButton
        }, req.__User__)).then((result) => {
            if (_.isEmpty(result)) {
                return res.json({
                    code: 400,
                    message: '数据错误'
                });
            }
            result.studentPrice = req.__User__.isStudent && result && result.goodsPrice &&
                result.goodsPrice.studentPrice ? result.goodsPrice.studentPrice : false;
            return res.json(result);
        }).catch(next);
    },

    /**
     * 商品收藏/取消收藏
     */
    favoriteProduct(req, res, next) {
        let allowOrigin = _.get(req, 'headers.origin', null) ?
            req.headers.origin : req.protocol + '://' + req.headers.host;

        res.setHeader('Access-Control-Allow-Origin', allowOrigin);
        res.setHeader('Access-Control-Allow-Credentials', 'true');

        let uid = req.user.uid;

        if (!uid) {
            return res.json({
                code: 400,
                data: helpers.urlFormat('/signin.html', {
                    refer: req.headers.referer
                })});
        }

        if (req.body.opt === 'ok') {
            listModel.setFavorite(req.body.id, uid, 'product').then(result => {
                return res.json(result);
            }).catch(next);
        } else {
            listModel.setFavoriteCancel(req.body.id, uid, 'product').then(result => {
                return res.json(result);
            }).catch(next);
        }
    },

    /**
     * 限购商品说明页面
     */
    limitHelp(req, res) {
        res.render('detail/limit-help', {
            module: 'product',
            page: 'limit-help'
        });
    }
};

module.exports = newDetail;