chatQa.js 3.55 KB
/**
 * 服务与反馈
 * <jing.li@yoho.cn>
 * 2016/12/13
 */

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

const _getCenter = () => {
    return api.get('', {
        method: 'app.helper.homeCategoryFaq',
        showPlatform: 'yohobuy_ios'
    }, {code: 200});
};

const index = () => {

    return api.all([
        _getCenter()
    ]).then(result => {

        let resu = {};

        if (result && result[0] && result[0].data) {
            _.forEach(result[0].data.categorys, function(val) {

                _.forEach(val.contentItems, function(item) {
                    item.parentId = val.id;
                });

            });
            resu = result[0].data;
        }
        return resu;
    });
};

const keySearch = (params) => {
    return api.get('', {
        method: 'app.helper.search',
        keyword: params.keyword
    }, {code: 200}).then(result => {
        return result;
    });
};

const qaDetail = (params) => {
    return api.all([
        _getCenter(),
        keySearch(params)
    ]).then(result => {

        let resu = {};

        // 根据子id,父id,关键字,匹配问题详情
        if (params.keyword) {
            // 从搜索列表进入,有关键字
            if (result && result[1] && result[1].data) {
                let keyList = result[1].data.helper_list;

                _.forEach(keyList, function(val) {
                    if (parseInt(val.id, 10) === parseInt(params.sonId, 10)) {
                        resu = {
                            caption: val.caption,
                            content: helpers.httpContent(val.content)
                        };
                    }
                });
            }
        } else {
            // 常见问题没有parentId
            if (result && result[0] && result[0].data) {
                if (!params.parentId) {
                    _.forEach(result[0].data.faqs, function(val) {
                        if (parseInt(val.id, 10) === parseInt(params.sonId, 10)) {
                            resu = {
                                caption: val.caption,
                                content: helpers.httpContent(val.content)
                            };
                        }
                    });
                } else {
                    _.forEach(result[0].data.categorys, function(val) {
                        if (parseInt(val.id, 10) === parseInt(params.parentId, 10)) {
                            _.forEach(val.contentItems, function(item) {

                                if (parseInt(item.id, 10) === parseInt(params.sonId, 10)) {
                                    resu = {
                                        caption: item.caption,
                                        content: helpers.httpContent(item.content)
                                    };
                                }
                            });
                        }
                    });
                }
            }
        }

        return resu;
    });
};

/**
 * 通过 ID 获取帮助详情
 */
const getDetailById = (id) => {
    return api.get('', {
        method: 'app.helper.detail',
        id: id
    });
};

const qaSearch = () => {

    return api.all([
        _getCenter()
    ]).then(result => {

        let resu = {
            hotSearch: []
        };

        if (result && result[0] && result[0].data) {

            resu.hotSearch = result[0].data.faqs;
        }
        return resu;
    });
};

module.exports = {
    index,
    qaSearch,
    qaDetail,
    getDetailById,
    keySearch
};