detail.js 9.6 KB
/**
 * 商品详情controller
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/5/6
 */

'use strict';

const mRoot = '../models';
const headerModel = require('../../../doraemon/models/header'); // 头部model
const detailModel = require(`${mRoot}/detail`); // 商品详情 model
const introModel = require(`${mRoot}/intro`); // 商品尺码信息 model
const preferenceModel = require(`${mRoot}/preference`); // 商品偏好 model
const detailRelated = require(`${mRoot}/consult-comment`); // 商品评论咨询 model
const _ = require('lodash');

const helpers = global.yoho.helpers;



exports.getUser = (req, res, next) => {
    let uid = req.user.uid,
        result = {
            isStudent: false
        };

    detailModel.getUserProfile(uid).then((data) => {
        result.isStudent = data.data && data.data.vip_info && data.data.vip_info.is_student ? true : false;
        result.vipLevel = (data.data && data.data.vip_info && data.data.vip_info.cur_level) || '0';
        req.__User__ = result;
        next();
    }).catch(next);
};

// /**
//  * 商品基本信息
//  * @param  {[type]} req [description]
//  * @param  {[type]} res [description]
//  * @return {[type]}     [description]
//  */
// exports.index = (req, res, next) => {
//     if (!req.params[0] || !req.params[1]) {
//         return next();
//     }
//     let uid = req.user.uid || 0;
//     let headerData = headerModel.setNav({
//         navTitle: '商品详情'
//     });

//     detailModel.getProductData({
//         id: req.params[0],
//         goodsId: req.params[1],
//         uid: uid,
//         ua: req.get('user-agent') || ''
//     }).then((result) => {
//         if (_.isEmpty(result)) {
//             return next();
//         }

//         result.studentPrice = req.__User__.isStudent && result && result.goodsPrice && result.goodsPrice.studentPrice ? result.goodsPrice.studentPrice : false;
//         res.render('detail/detail', {
//             pageHeader: headerData,
//             result: result,
//             page: 'detail',
//             title: result.goodsName,
//             pageFooter: true
//         });
//     }).catch(next);
// };

// /**
//  * 商品基本信息 SKN 进入
//  * @param  {[type]} req [description]
//  * @param  {[type]} res [description]
//  * @return {[type]}     [description]
//  */
// exports.indexSkn = (req, res, next) => {
//     if (!req.params[0]) {
//         return next();
//     }
//     let uid = req.user.uid || 0;
//     let headerData = headerModel.setNav({
//         navTitle: '商品详情'
//     });

//     detailModel.getProductData({
//         productSkn: req.params[0],
//         uid: uid,
//         ua: req.get('user-agent') || ''
//     }).then((result) => {
//         if (_.isEmpty(result)) {
//             return next();
//         }
//         result.studentPrice = req.__User__.isStudent && result && result.goodsPrice && result.goodsPrice.studentPrice ? result.goodsPrice.studentPrice : false;
//         res.render('detail/detail', {
//             pageHeader: headerData,
//             result: result,
//             page: 'detail',
//             title: result.goodsName,
//             pageFooter: true
//         });
//     }).catch(next);
// };

/**
 * 商品尺码信息详情
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.intro = (req, res, next) => {
    if (!req.params.productskn) {
        return next();
    }

    introModel.getintroIntro({
        productskn: req.params.productskn
    }, req).then(html => {
        res.send(html);
    });

    // introModel.getintroData({
    //     productskn: req.params.productskn
    // }, req).then((result) => {
    //     res.render('detail/intro', {
    //         result: result,
    //         layout: false
    //     });
    // }).catch(next);
};

/**
 * 为你优选
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.preference = (req, res, next) => {
    preferenceModel({
        productskn: req.query.productSkn
    }).then((result) => {
        res.send(result);
    }).catch(next);
};

/**
 * 购买评价页
 */
exports.comments = (req, res, next) => {
    if (!req.query.product_id) {
        return next();
    }

    let headerData = headerModel.setNav({
        navTitle: '购买评价'
    });

    detailRelated.comments(req.query).then((result) => {
        res.render('detail/comments', Object.assign({
            title: '购买评价',
            pageHeader: headerData,
            pageFooter: true
        }, result));
    }).catch(next);
};

/**
 * 购买咨询
 */
exports.consults = (req, res, next) => {
    if (!req.query.product_id) {
        return next();
    }

    let headerData = headerModel.setNav({
        navTitle: '购买咨询'
    });

    detailRelated.consults(req.query).then((result) => {
        res.render('detail/consults', Object.assign({
            title: '购买咨询',
            pageHeader: headerData,
            pageFooter: true
        }, result));
    }).catch(next);
};

/**
 * 咨询点赞
 */
exports.consultUpvoteOrUseful = (req, res, next) => {
    let uid = req.user.uid || 0;

    if (!uid) {
        return res.json({
            code: 401,
            message: '用户id为空',
            data: helpers.urlFormat('/signin.html', {
                refer: helpers.urlFormat('/product/detail/consults', {
                    product_id: req.body.productId,
                    total: req.body.total
                })
            })
        });
    }

    req.body.isUpvote = /upvote/.test(req.path);
    req.body.uid = uid;

    detailRelated.upvoteConsult(req.body).then((result) => {
        res.json(result);
    }).catch(next);
};

/**
 * 咨询表单页
 */
exports.consultform = (req, res, next) => {
    if (!req.query.product_id) {
        return next();
    }

    let headerData = headerModel.setNav({
        navTitle: '我要咨询'
    });

    res.render('detail/consult-form', {
        title: '我要咨询',
        pageHeader: headerData,
        productId: req.query.product_id,
        formUrl: '/product/detail/consultsubmit',
        pageFooter: true
    });
};

/**
 * 咨询提交接口
 */
exports.consultsubmit = (req, res, next) => {
    let data = {
        code: 400,
        message: '请输入咨询内容',
        data: ''
    };

  // 判断参数是否存在
    if (!req.body.product_id || !req.body.content) {
        return res.json(data);
    }

    detailRelated.addConsult(req.user.uid, req.body.product_id, req.body.content).then((result) => {

        if (result) {
            Object.assign(data, result);
        }
        return res.json(data);
    }).catch(next);
};

/**
 * 商品基本信息 SKN 进入 pagecache重构
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.indexSkn = (req, res, next) => {
    if (!req.params[0]) {
        return next();
    }

    // let uid = req.user.uid || 0;// pagecache重构
    let headerData = headerModel.setNav({
        navTitle: '商品详情'
    });

    detailModel.getProductData({
        productSkn: req.params[0],

        // uid: uid,
        ua: req.get('user-agent') || ''
    }).then((result) => {
        if (_.isEmpty(result)) {
            return next();
        }

        // pagecache重构
        // result.studentPrice = req.__User__.isStudent && result && result.goodsPrice && result.goodsPrice.studentPrice ? result.goodsPrice.studentPrice : false;
        res.render('detail/detail', {
            pageHeader: headerData,
            result: result,
            page: 'detail',
            title: result.goodsName,
            pageFooter: true
        });
    }).catch(next);
};

/**
 * 商品基本信息 pagecache重构
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.index = (req, res, next) => {
    if (!req.params[0] || !req.params[1]) {
        return next();
    }

    // let uid = req.user.uid || 0; // pagecache重构
    let headerData = headerModel.setNav({
        navTitle: '商品详情'
    });

    detailModel.getProductData({
        id: req.params[0],
        goodsId: req.params[1],

        // uid: uid,
        ua: req.get('user-agent') || ''
    }).then((result) => {
        if (_.isEmpty(result)) {
            return next();
        }

        // pagecache重构
        // result.studentPrice = req.__User__.isStudent && result && result.goodsPrice && result.goodsPrice.studentPrice ? result.goodsPrice.studentPrice : false;
        res.render('detail/detail', {
            pageHeader: headerData,
            result: result,
            page: 'detail',
            title: result.goodsName,
            pageFooter: true
        });
    }).catch(next);
};

/**
 * 商品信息异步数据
 * @param  {[type]} req [description]
 * @param  {[type]} res [description]
 * @return {[type]}     [description]
 */
exports.indexData = (req, res, next)=> {
    if (!req.xhr) {
        return next();
    }
    if (!req.body.id) {
        return next();
    }
    let uid = req.user.uid || 0;

    detailModel.getProductAsyncData(Object.assign({
        id: req.body.id,
        goodsId: req.body.goodsId,
        productSkn: req.body.productSkn,
        uid: uid,
        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);

};