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

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

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

    _getCenter() {
        return this.get({
            data: {
                method: 'app.helper.homeCategoryFaq',
                showPlatform: 'yohobuy_ios'
            },
            param: {
                code: 200
            }
        });
    }

    index() {
        return Promise.all([
            this._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;
        });
    }

    keySearch(params) {
        return this.get({
            data: {
                method: 'app.helper.search',
                keyword: params.keyword
            },
            param: {
                code: 200
            }
        });
    }

    qaDetail(params) {
        return Promise.all([
            this._getCenter(),
            this.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 获取帮助详情
     */
    getDetailById(id) {
        return this.get({
            data: {
                method: 'app.helper.detail',
                id: id
            }
        });
    }

    qaSearch() {
        return Promise.all([
            this._getCenter()
        ]).then(result => {
            let resu = {
                hotSearch: []
            };

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

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