help-service.js
10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
* 帮助中心
* @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 = _.last(nav).name;
}
return {
nav: nav,
data: {
menuData: menuData,
contData: contData,
seoData: {
title: `有货${ht}-帮助中心-YOHO!BUY有货 100%正品保证`,
keywords: `${ht},有货${ht},有货${ht}帮助中心`,
description: `有货${ht}。YOHO!BUY有货帮助中心${ht}频道,提供有关${ht}相关问题解答。YOHO!BUY有货100%品牌正品保证,支持货到付款。`
}
}
};
}
// 自助服务数据处理
_processServiceData(data) {
_.forEach(data, (item, k) => {
item.index = k + 1;
});
return _.chunk(data, 4);
}
// 首页
helpData(channel) {
let helpDataModel = new HelpApi(this.ctx);
let _this = this;
return Promise.all([
headerModel.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 promiseData = [
headerModel.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', '');
});
}
};