client-service.js
2.33 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
/**
* 客服用户端 service
*
* @author: liqi <qi.li@yoho.cn>
* @date: 2016/11/4
*/
'use strict';
const _ = require('lodash');
const Promise = require('bluebird');
const clientAPI = require('./client-api');
/**
* 在线客服客服端页面
* @param { number } type
* @param { string } encryptedUid
* @return { Object } 客服设置
*/
const getClientData = (uid, type, encryptedUid) => {
const logoSize = '136x40';
const qcSize = '135x135';
const advSize = '160x335';
const regExp = /\{width}x\{height}/g;
let apiMethod = [
clientAPI.getCsSetting(type),
clientAPI.getMsgHistory(uid, encryptedUid),
clientAPI.getLastTenOrders(uid, encryptedUid),
clientAPI.getQas(uid, encryptedUid)
];
return Promise.all(apiMethod)
.then(res => {
let csSetting = {};
let orderList = [];
let qasList = [];
let records = [];
let hasHistory = false;
if (res[1].code === 401 ||
res[2].code === 401 ||
res[3].code === 401) {
return {
code: 401
};
}
if (res[0] && res[0].code === 200) {
if (res[0].data.config) {
csSetting = res[0].data.config;
csSetting.qrCode = csSetting.qrCode.replace(regExp, qcSize);
csSetting.pcAdImg = csSetting.pcAdImg.replace(regExp, advSize);
csSetting.windowLogo = csSetting.windowLogo.replace(regExp, logoSize);
}
}
if (res[1] && res[1].code === 200) {
records = res[1].data.records;
hasHistory = records.length > 0;
}
if (res[2] && res[2].code === 200) {
orderList = res[2].data;
}
if (res[3] && res[3].code === 200) {
_.each(res[3].data, function(item) {
item.content = (item.content || '').replace(/src=["']https?:/ig, 'src="');
});
qasList = res[3].data;
}
return {
csSetting: csSetting,
hasHistory: hasHistory,
orders: orderList,
qas: qasList
};
});
};
module.exports = {
getClientData
};