comment.js 1.35 KB
/**
 *   comment controller
 *   @author 陈轩 <xuan.chen@yoho.cn>
 */

'use strict';


const cookie = global.yoho.cookie;
const logger = global.yoho.logger;
const commentModel = require('../models/comment');
const _ = require('lodash');


// comment page
exports.index = (req, res, next) => {
    let uid = cookie.getUid(req);
    let isComment = req.query.isComment;
    let page = req.query.page || 1;

    // 转string值为bool值
    isComment = isComment === 'Y';

    commentModel.getCommentList(uid, isComment, page /* , limit=10*/)
        .then(data => {
            let localData = _.merge(data, {
                module: 'home',
                page: 'comment'
            });

            res.render('comment', localData);
        })
        .catch(next);
};


exports.saveComment = (req, res, next) => {
    // only ajax
    if (!req.xhr) {
        return;
    }

    // get post args
    let data = {
        uid: cookie.getUid(req),
        productSkn: req.body.productSkn,
        productId: req.body.productId,
        content: req.body.content,
        goodsId: req.body.goodsId,
        orderId: req.body.orderId,
        erpSkuId: req.body.erpSkuId
    };

    commentModel.saveShareOrder(data)
        .then(result => {
            res.json(result);
        })
        .catch(error => {
            logger.error(`home--comment: ${error}`);
        });
};