consult-comment.js 6.02 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;

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

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

        return this.get({
            data: params,
            param: {code: 200, cache: true}
        }).then(result => {
            let data = {};

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

            return data;
        });
    }

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

        return data.map(function(value) {
            return {
                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)
            };
        });
    }

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

        return this.get({
            data: params,
            param: {code: 200, cache: true}
        }).then(result => {
            let data = {};

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

            return data;
        });
    }

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

        if (data && data.pageResponse) {
            let color, size;

            _.forEach(data.pageResponse.list, (item) => {
                if (item.goods) {
                    color = item.goods.factory_goods_name ? item.goods.factory_goods_name : item.goods.color_name;
                    size = item.goods.size_name;
                }

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

        return result;
    }

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

            return {};
        });
    }

    /**
     * 购买评价列表
     * @param  {[object]} 查询参数
     * @return {[object]}
     */
    comments(params) {
        return this.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]}
     */
    consults(params, uid) {
        return api.all([
            this.getCommonConsult(),
            this.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;
        });
    }

    upvoteConsult(params) {
        return this.get({
            data: {
                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]}
     */
    addConsult(uid, productId, content) {
        let params = {
            method: 'app.consult.add',
            product_id: productId,
            content: content,
            uid: uid
        };

        return this.post({
            data: params
        }).then(result => {
            if (!result || !result.code || result.code !== 200) {
                return false;
            }
            return result;
        });
    }
};