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

'use strict';
const _ = require('lodash');
const helpApi = require('./help-api');
const headerModel = require('../../../doraemon/models/header');
const pager = require(`${global.utils}/pager`).setPager;

/**
 * 根据id获取当前所在位置,拼接面包屑
 * @param id
 * @param menuData
 * @returns {*[]}
 * @private
 */
const _getNav = (id, menuData, url)=> {
    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 [
                        {
                            href: url,
                            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 _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 _processHomeCategoryFaq = (list) => {
    let result = {
        menuData: []
    };

    list = list || {};

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

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

        result.menuData.push(_processMenuType(item));

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

        switch (item.categoryName) {
            case '新手指南':

                _.forEach(item.categoryItems, (ng, k) => {
                    ng.index = k + 1;
                });

                item.categoryItems = _.chunk(item.categoryItems, 6);
                item.num = _.flattenDeep(item.categoryItems).length;
                result.newGuide = item;

                break;
            case '公告':
                _.forEach(item.contentItems, (c, k) => {
                    c.caption = c.caption.substring(0, 10);
                    c.index = k;
                });

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

                result.notice = newCont;
                break;
            default:
                break;
        }
    });

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

    return result;
};

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 {
        let page = pager(_.get(list, 'page_total', 0), {
            page: params.page,
            helpQuery: params.keywords
        });

        list = _.assign({
            footPager: page
        }, list);
    }

    return list;
};

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

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

        if (params.id === 1 && !params.keywords) {
            let page = pager(_.get(result, '[3].data.page_total', 0), {
                id: params.id,
                page: params.page
            });

            contData = {
                isShowMenu: _processCountLength(data.faqs),
                id: 1,
                commonProblem: true,
                faqs: data.faqs,
                allQa: _.get(result, '[3].data.list', []),
                footPager: page
            };
        }
    }

    // 其它分类数据处理
    _.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, params.url);
    }

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

// 自助服务数据处理
const _processServiceData = (data) => {
    _.forEach(data, (item, k) => {
        item.index = k + 1;
    });

    return _.chunk(data, 4);
};

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

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

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

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

        _.set(newData, 'data.hotSearch', _.slice(_.get(result, '[1].data', []), 0, 4));
        return {
            headerData: _.get(result, '[0]', {}),
            newData: newData
        };
    });
};

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

// 在线客服
const onlineService = () => {
    return helpApi.onlineService().then(result => {
        return _.get(result, 'data.url', '');
    });
};

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