consult-comment.js 5.44 KB
/**
 * 商品详情相关models
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/6/27
 */

'use strict';

const _ = require('lodash');
const api = global.yoho.API;
const helpers = global.yoho.helpers;

/**
 * 获取默认咨询列表
 */
const getCommonConsult = () => {
    let params = {
        method: 'app.consult.common'
    };

    return api.get('', params, {
        code: 200
    }).then(result => {
        let data = {};

        if (result.data) {
            data.faq = result.data;
        }

        return data;
    });
};

/**
 * 处理咨询列表数据
 * @data  {[object]} 咨询列表原始数据
 * @return {[object]}
 */
const _formatConsultsList = (data) => {
    let list = [];

    if (data.length) {
        _.forEach(data, (value) => {
            list.push({
                question: value.ask,
                time: value.ask_time,
                answer: value.answer,
                id: value.id,
                isLike: value.is_like === 'Y',
                like: _.toNumber(value.like),
                isUseful: value.is_useful === 'Y',
                useful: _.toNumber(value.useful)
            });
        });
    }

    return list;
};

/**
 * 获取咨询数据
 * @id  {[number]} 商品id
 * @page  {[number]} 页码
 * @limit  {[number]} 每页咨询数量
 * @return {[object]}
 */
const getConsults = (id, page, limit, uid) => {
    let params = {
        method: 'app.consult.li',
        product_id: id,
        page: page ? page : 1,
        limit: limit ? limit : 300,
        uid
    };

    return api.get('', params, {
        code: 200
    }).then(result => {
        let data = {};

        if (result && result.data && result.data.list) {
            Object.assign(data, {
                list: _formatConsultsList(result.data.list),
                pageTotal: result.data.page_total,
                total: result.data.total
            });
        }

        return data;
    });
};

/**
 * 处理评论数据
 */
let _processComment = (data) => {
    let result = {
        commentsNum: (data.pageResponse && data.pageResponse.totalCount) || 0,
        comments: []
    };

    if (data && data.pageResponse) {
        let descColor;

        _.forEach(data.pageResponse.list, (item) => {
            if (item.goods) {
                descColor = item.goods.factory_goods_name ? `${item.goods.factory_goods_name}/${item.goods.size_name}` : `${item.goods.color_name}/${item.goods.size_name}`;
            };

            result.comments.push({
                userName: item.userInfo ? item.userInfo.nickName : '',
                desc: descColor ? descColor : '',
                content: item.content,
                time: helpers.dateFormat('YYYY-MM-DD HH:mm:ss', new Date(item.createTime * 1000))
            });
        });
    }

    return result;
};

/**
 * 获取评论信息
 */
let getCommentInfo = (params) => {
    return api.get('', Object.assign({
        method: 'show.productShareOrderList',
        limit: '1',
        page: '1',
        filterId: '7'
    }, params), {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return _processComment(result.data);
        }

        return {};
    });
};

/**
 * 购买评价列表
 * @param  {[object]} 查询参数
 * @return {[object]}
 */
let comments = (params) => {
    return getCommentInfo({
        productId: params.product_id,
        limit: '60'
    }).then(result => {
        let data = {};

        if (result.comments && result.comments.length) {
            if (result.commentsNum) {
                _.set(data, 'navTitle', `购买评价(${result.commentsNum})`);
            }
            data.comments = result.comments;
        }

        return data;
    });
};

/**
 * 购买咨询列表
 * @params  {[object]} 查询参数
 * @return {[object]}
 */
let consults = (params, uid) => {
    return api.all([
        getCommonConsult(),
        getConsults(params.product_id, 1, 60, uid)
    ]).then(result => {
        let data = {
            link: `/product/detail/consultform?product_id=${params.product_id}`
        };

        Object.assign(data, result[0]);

        if (result[1].list && result[1].list.length) {
            if (result[1].total) {
                _.set(data, 'navTitle', `购买咨询(${result[1].total})`);
            }
            data.consults = result[1].list;
        }

        data.showReadMore = _.get(result[1], 'list.length', 0) > 2;

        return data;
    });
};

let upvoteConsult = (params) => {
    return api.get('', {
        method: params.isUpvote ? 'app.consult.like' : 'app.consult.useful',
        id: params.id,
        uid: params.uid
    });
};

/**
 * 购买咨询列表
 * @uid  {[number]} 用户id
 * @productId  {[number]} 商品id
 * @content  {[string]} 咨询内容
 * @return {[object]}
 */
let addConsult = (uid, productId, content) => {
    let params = {
        method: 'app.consult.add',
        product_id: productId,
        content: content,
        uid: uid
    };

    return api.post('', params).then(result => {
        if (!result || !result.code || result.code !== 200) {
            return false;
        }
        return result;
    });
};

module.exports = {
    getCommonConsult, // 默认咨询
    getCommentInfo, // 商品详情相关,获取评价,来自晒单
    comments, // 商品详情相关-购买评价
    consults, // 商品详情相关-购买咨询
    addConsult, // 商品详情相关-添加咨询
    upvoteConsult, // 咨询点赞
    getConsults // 获取咨询
};