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

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

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 = Object.assign(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 && result[1].data.data) {
                let keyList = result[1].data.data.helper_list;

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

                                if (item.id == params.sonId) {
                                    resu = {
                                        caption: item.caption,
                                        content: item.content
                                    };
                                }
                            });
                        }
                    });
                }
            }
        }
        return resu;
    });
};

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