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

'use strict';

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

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;
    });
};

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;
};

const _getConsults = (id, page, limit) => {
    let params = {
        method: 'app.consult.li',
        product_id: id,
        page: page ? page : 1,
        limit: limit ? limit : 10
    };

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

        if (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 consults = (params) => {
    return api.all([
        _getCommonConsult(),
        _getConsults(params.product_id, 1, 10)
    ]).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, 'pageHeader.navTitle', `购买咨询(${result[1].total})`);
            }
            data.consults = result[1].list;
        }

        return data;
    });
};

let addConsult = (uid, productId, content) => {
    let params = {
        method: 'h5.consult.add',
        product_id: productId,
        content: content,
        uid: uid
    };

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

module.exports = {
    consults,  // 商品详情相关-购买咨询
    addConsult  // 商品详情相关-添加咨询
};