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

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

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

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

            // result.id = id;
            // result.goodsId = goodsId;

            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: 'seckill-detail',
                title: result.goodsName,
                pageFooter: true,
                localCss: true,
                appPath: appPath
            });
        }).catch(next);
    },


    /*
     *   商品基本信息 SKN 进入 pagecache重构
     */
    sknData(req, res, next) {
        let brandId = req.query.brandId;
        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();
        }

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

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

    indexData(req, res, next) {
        if (!req.xhr) {
            return next();
        }
        if (!req.body.id) {
            return next();
        }
        let uid = req.user.uid || 0;
        let shoppingKey = req.cookies._SPK || '';

        detailModel.getNewProductAsyncData(Object.assign({
            id: req.body.id,
            goodsId: req.body.goodsId,
            productSkn: req.body.productSkn,
            bundleType: req.body.bundleType,
            uid: uid,
            shoppingKey: shoppingKey,
            ua: req.get('user-agent') || ''
        }, 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') {
            req.ctx(listModel).setFavorite(req.body.id, uid, 'product').then(result => {
                return res.json(result);
            }).catch(next);
        } else {
            req.ctx(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'
        });
    },

    // 获取秒杀信息
    getSeckillData(req, res) {

        seckillDetailModel.getSeckillData({

            productskn: req.params.productskn

        }, req).then(result => {

            return res.json(result);

        });
    }
};

module.exports = newDetail;