help.js 7.43 KB
/**
 * 帮助中心
 * @author: jiangmin
 * @date: 2016/07/27
 */

'use strict';
const _ = require('lodash');
const helpApi = require('./help-api');

/**
 * 根据id获取当前所在位置,拼接面包屑
 * @param id
 * @param menuData
 * @returns {*[]}
 * @private
 */
const _getNav = (id, menuData)=> {
    for (let i = 0; i < menuData.length; i++) {
        if (menuData[i].subsets) {
            for (let j = 0; j < menuData[i].subsets.length; j++) {
                if (menuData[i].subsets[j].id === id) {
                    return [
                        {
                            link: ' ',
                            name: menuData[i].categoryName
                        }, {
                            name: menuData[i].subsets[j].categoryName
                        }
                    ];
                }
            }
        } else {
            if (menuData[i].id === id) {
                return [
                    {
                        name: menuData[i].categoryName
                    }
                ];
            }
        }
    }
};

// 首页链接组装
const _processLink = (list, id) => {
    let params = '';

    list = list || [];

    _.forEach(list, d => {
        if (id) {
            params = 'id=' + id + '&contId=' + d.id;
        } else {
            params = 'id=' + d.id;
        }

        d.jumpLink = global.yoho.config.siteUrl + '/help/detail?' + params;
    });

    return list;
};

// 首页模块内容处理
const _processHomeCategoryFaq = (list) => {
    let result = {
        other: []
    };

    list = list || {};

    _.forEach(list.categorys, (item) => {
        let newCont = [];

        if (item.categoryItems) {
            item.categoryItems = _processLink(item.categoryItems);
        } else {
            item.contentItems = _processLink(item.contentItems, item.id);
        }

        switch (item.categoryName) {
            case '新手指南':
                result.newGuide = item;
                break;
            case '公告':
                _.forEach(item.contentItems, c => {
                    c.caption = c.caption.substring(0, 9);
                });

                _.chunk(item.contentItems, 2).forEach(data => {
                    newCont.push({
                        row: data
                    });
                });

                result.notice = newCont;
                break;
            default:
                result.other.push(item);
                break;
        }
    });

    result.problem = {
        categoryName: '常见问题',
        list: list.faqs
    };

    return result;
};

// 处理分类
const _processMenuType = (item) => {
    let curMenu = {
        id: item.id,
        categoryName: item.categoryName
    };

    let subsets = [];

    if (item.categoryItems) {
        _.forEach(item.categoryItems, c => {
            subsets.push({
                id: c.id,
                parendId: item.id,
                categoryName: c.categoryName
            });
        });
        curMenu.subsets = subsets;
    }

    return curMenu;
};

// 统计当前分类下的内容是否大于1
const _processCountLength = (c) => {
    if (!c.contentItems) {
        return false;
    } else {
        return c.contentItems.length > 1;
    }
};



const returnCont = (data, contId) => {
    let curCont = {};

    if (!data.contentItems) {
        return;
    }

    _.forEach(data.contentItems, c => {
        if (c.id === contId) {
            curCont = c;
        }
    });

    return {
        isShowMenu: _processCountLength(data),
        id: data.id,
        title: data.categoryName,
        list: data.contentItems,
        contId: contId ? contId : data.contentItems[0].id,
        cont: contId ? curCont : data.contentItems[0]
    };
};

// 处理详情内容
const _processDetailCont = (item, params) => {
    let cont = false;

    if (item.contentItems && item.id === params.id) {
        return returnCont(item, params.contId);
    }

    if (item.categoryItems) {
        _.forEach(item.categoryItems, c => {
            if (c.id === params.id) {
                cont = returnCont(c, params.contId);
            }
        });
        return cont;
    }
    return false;
};

// 处理搜索结果内容
const _processSearch = (list, params) => {
    list = list || [];

    if (list.total === 0) {
        return {
            error: true
        };
    } else {
        list = _.assign({
            paginationData: {
                page: list.page,
                limit: list.page_size || 10,
                total: list.total,
                pageTotal: list.page_total,
                queryParams: {
                    page: params.page,
                    helpQuery: params.keywords
                }
            }
        }, list);
    }

    return list;
};

// 详情内容及分类数据
const _processDetailData = (result, params) => {
    let menuData = [];
    let contData = false;
    let data = result[1].data;
    let nav = [];

    // 常见问题
    if (data.faqs) {
        menuData.push({
            id: 1,
            categoryName: '常见问题'
        });

        if (params.id === 1 && !params.keywords) {
            contData = {
                isShowMenu: _processCountLength(data.faqs),
                id: 1,
                commonProblem: true,
                faqs: data.faqs,
                allQa: result[2].data
            };
        }
    }

    // 其它分类数据处理
    _.forEach(data.categorys, item => {
        let resCont;

        menuData.push(_processMenuType(item));

        if (contData || params.keywords) {
            return;
        }

        resCont = _processDetailCont(item, params);

        if (resCont) {
            contData = _.assign({
                helpDetail: true
            }, resCont);
        }
    });

    if (params.keywords) {
        nav = [
            {
                name: '搜索结果'
            }
        ];
    } else {
        nav = _getNav(params.id, menuData);
    }

    return {
        nav: nav,
        data: {
            menuData: menuData,
            contData: contData
        }
    };
};

// 首页
const helpData = () => {
    return Promise.all([
        helpApi.getHotSearch(),
        helpApi.gethomeCategoryFaq(),
        helpApi.getSelfService()
    ]).then(result => {
        return _.assign({
            hotSearch: _.slice(result[0].data, 0, 5),
            selfService: {
                categoryName: '自助服务',
                list: result[2].data
            }
        }, _processHomeCategoryFaq(result[1].data));
    });
};

// 详情
const detailData = (params) => {
    let promiseData = [
        helpApi.getHotSearch(),
        helpApi.gethomeCategoryFaq()
    ];

    if (params.id === 1 && !params.keywords) {
        promiseData.push(helpApi.allQA());
    }

    return Promise.all(promiseData).then(result => {
        let newData = _processDetailData(result, params);

        newData.data.hotSearch = _.slice(result[0].data, 0, 5);
        return newData;
    });
};

// 搜索
const searchData = (params) => {
    return helpApi.helpSearch(params).then(result => {
        return _.assign({
            keywords: params.keywords
        }, _processSearch(result.data.data, params));
    });
};

// 在线客服
const onlineService = () => {
    return helpApi.onlineService().then(result => {
        let url = '';

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

        return url;
    });
};

module.exports = {
    helpData,
    detailData,
    searchData,
    onlineService
};