help-service.js 10.7 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;

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

    /**
     * 根据id获取当前所在位置,拼接面包屑
     * @param id
     * @param menuData
     * @returns {*[]}
     * @private
     */
    _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
                        }
                    ];
                }
            }
        }
    }

    // 首页链接组装
    _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;
    }

    // 处理分类
    _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
    _processCountLength(c) {
        if (!c.contentItems) {
            return false;
        } else {
            return c.contentItems.length > 1;
        }
    }

    // 首页模块内容处理
    _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(this._processMenuType(item));

            if (item.categoryItems) {
                item.categoryItems = this._processLink(item.categoryItems);
            } else {
                item.contentItems = this._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;
    }

    returnCont(data, contId) {
        let curCont = {};
        let _this = this;

        if (!data.contentItems) {
            return;
        }

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

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

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

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

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

    // 处理搜索结果内容
    _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;
    }

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

        // 常见问题
        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: _this._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(_this._processMenuType(item));

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

            resCont = _this._processDetailCont(item, params);

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

        let ht;

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

            ht = `${params.keywords}`;
        } else {
            nav = _this._getNav(params.id, menuData, params.url);
            ht = _.get(_.last(nav), 'name', '');
        }

        return {
            nav: nav,
            data: {
                menuData: menuData,
                contData: contData,
                seoData: {
                    title: `有货${ht}-帮助中心-YOHO!BUY有货 全场品牌正品保证`,
                    keywords: `${ht},有货${ht},有货${ht}帮助中心`,
                    description: `有货${ht}YOHO!BUY有货帮助中心${ht}频道,提供有关${ht}相关问题解答。YOHO!BUY有货全场品牌正品保证,支持货到付款。`
                }
            }
        };
    }

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

        return _.chunk(data, 4);
    }

    // 首页
    helpData(channel) {
        let helpDataModel = new HelpApi(this.ctx);
        let headerModelCtx = new HeaderModel(this.ctx);
        let _this = this;

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

    // 详情
    detailData(params) {

        let helpDataModel = new HelpApi(this.ctx);
        let headerModelCtx = new HeaderModel(this.ctx);
        let promiseData = [
            headerModelCtx.requestHeaderData(params.channel),
            helpDataModel.getHotSearch(),
            helpDataModel.gethomeCategoryFaq()
        ];
        let _this = this;

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

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

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

    // 搜索
    searchData(params) {
        let helpDataModel = new HelpApi(this.ctx);
        let _this = this;

        return helpDataModel.helpSearch(params).then(result => {
            return _.assign({
                keywords: params.keywords
            }, _this._processSearch(_.get(result, 'data', []), params));
        });
    }

    // 在线客服
    onlineService() {
        let helpDataModel = new HelpApi(this.ctx);

        return helpDataModel.onlineService().then(result => {
            return _.get(result, 'data.url', '');
        });
    }
};