chat.js
4.88 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
'use strict';
const orderModel = require('../../home/models/order');
const crypto = global.yoho.crypto;
const imApi = require('../models/im-api');
const userApi = require('../../passport/models/auth-helper');
const imModel = require('../models/im');
const helpers = global.yoho.helpers;
const sign = global.yoho.sign;
exports.appAdapter = (req, res, next) => {
if (req.yoho.isApp) {
req.query.client_secret = req.yoho.client_secret;
if (sign.checkSign(req.query)) {
let uid = (req.query.uid || '').trim();
req.user.uid = uid;
}
}
next();
};
exports.page = (req, res, next) => {
let uid = req.user.uid;
if (!uid) {
uid = req.query.uid;
}
userApi.profile(req.user.uid)
.then(userinfo => {
userinfo = userinfo.data;
res.render('chat/index', {
module: 'service',
page: 'chat',
pageStyle: 'service-chat',
width750: true,
localCss: true,
imServer: global.yoho.config.domains.imServer,
imSocket: global.yoho.config.domains.imSocket,
userData: {
uid: uid,
encrypteduid: crypto.encryption(null, uid + ''),
avatar: helpers.image(userinfo.head_ico, 100, 100),
uname: userinfo.profile_name
}
});
})
.catch(next);
};
exports.getOrders = (req, res, next) => {
let uid = req.user.uid;
if (!uid) {
uid = req.query.uid;
}
orderModel.getOrders({
type: req.query.type || 1,
page: req.query.page || 1,
gender: req.query.gender || '1,3',
yh_channel: req.query.channel || 1,
uid
}).then(result => {
res.render('chat/chat-order', {
layout: false,
orders: result
});
}).catch(next);
};
/**
* 获取历史消息
* Request:
*
*/
exports.fetchHistory = (req, res) => {
let uid = req.user.uid;
if (!uid) {
uid = req.query.uid;
}
const endTime = req.query.endTime;
imApi.fetchImHistory(uid, endTime).then(result => {
res.json(result);
});
};
exports.msghistory = (req, res) => {
let uid = req.user.uid;
if (!uid) {
uid = req.query.uid;
}
imApi.fetchImHistory(uid).then(result => {
res.json(result);
});
};
/**
* Request: 发表留言
* type: POST
* params:
* conversationId 会话id
* content 留言内容
*/
exports.saveMSG = (req, res) => {
let uid = req.user.uid;
if (!uid) {
uid = req.body.uid;
}
const conversationId = req.body.conversationId;
const content = req.body.content;
imApi.saveMessage(uid, conversationId, content)
.then(result => {
res.json(result);
});
};
/**
* Request: 获取订单
* type: GET
*
* TODO:
* 1. 分页, 目前接口不支持
* 2. 失败情况
*/
exports.fetchOrders = (req, res) => {
let uid = req.user.uid;
if (!uid) {
uid = req.query.uid;
}
imApi.fetchOrderList(uid)
.then(result => {
imModel.handleOrderList(result.data, 128, 170);
res.json(result);
}).catch(() =>{
return res.json({
code: 500,
data: []
});
});
};
exports.saveEvalute = (req, res) => {
let uid = req.user.uid;
if (!uid) {
uid = req.body.uid;
}
const conversationId = req.body.conversationId;
const promoter = req.body.promoter;
const stars = req.body.stars;
const reasonMsg = req.body.reasonMsg || '';
imApi.saveEvalute(uid, conversationId, promoter, stars, reasonMsg)
.then(result => {
return res.json(result);
}).catch(() => {
return res.json({
code: 500,
message: '评价失败'
});
});
};
exports.queryGlobalOrder = (req, res) => {
let uid = req.user.uid;
if (!uid) {
uid = req.query.uid;
}
let emptyOrder = {
code: 200,
data: [],
message: '获取失败'
};
imApi.queryGlobalOrder(uid)
.then(result=> {
imModel.handleOrderList(result.data, 128, 170);
res.json(result);
}, () => {
res.json(emptyOrder);
});
};
/**
* Request: 获取评价原因
* type: GET
* params:
* cvId 会话id
*/
exports.queryReasons = (req, res) => {
let uid = req.user.uid;
if (!uid) {
uid = req.body.uid;
}
const cvId = req.query.conversationId;
imApi.queryReasons(cvId)
.then(result=> {
res.json(result);
})
.catch(() => {
return res.json({
code: 500,
message: '拉取评价原因失败'
});
});
};