view.js
9.79 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
const loading = require('plugin/loading'),
tip = require('plugin/tip'),
lazyLoad = require('yoho-jquery-lazyload');
require('common');
import {EventEmitter, api} from './store';
import appBridge from 'yoho-app';
const LeaveMSGView = function(elem) {
this.$elem = $(elem);
this.$input = this.$elem.find('.leave-text');
this.$doc = $(document);
this.bindEvents();
this.isSending = false;
};
LeaveMSGView.prototype = $.extend({}, EventEmitter.prototype, {
/**
* 绑定事件
* 1. 提交按钮事件
**/
bindEvents() {
let self = this;
this.$elem
.on('click', '.chat-page-back', function(event) {
event.stopPropagation();
self.toggleHide(true);
})
.on('click.LeavseMSG.submit', '.leave-submit', function() {
event.stopPropagation();
self.submit();
});
this.on('show.LeaveMSGView', $.proxy(this.toggleHide, this, false));
},
/**
* method: show/hide 留言
* @param boolean show;
*/
toggleHide(willHide) {
this.$elem.toggleClass('chat-page-hide', willHide);
return false;
},
/**
* 提交留言的hanlder
*/
submit() {
if (this.isSending) {
tip.show('正在发送..');
return;
}
this.isSending = true;
let self = this;
let content = $.trim(this.$input.val());
if (!content) {
return;
}
api.leaveMsg(content)
.done(function() {
self.trigger('save.LeaveMSGView', '留言成功,');
self.$input.val('');
})
.fail(function() {
self.trigger('save.LeaveMSGView', '留言失败');
})
.always(function() {
self.isSending = false;
});
self.toggleHide(true);
}
});
LeaveMSGView.prototype.constructer = LeaveMSGView;
/*
评价view
*/
const RatingView = function(elem, cmEntity) {
this.elem = $(elem);
this.$ranks = this.elem.find('.stars i');
this.$label = this.elem.find('.rank');
this.$more = this.elem.find('.more-comment');
this.cmEntity = cmEntity;
this.rank = 3;
this.bindEvents();
this.isSending = false;
this.reasonsRendered = false;
};
RatingView.prototype = $.extend({}, EventEmitter.prototype, {
bindEvents() {
const self = this;
this.elem
.on('click.RatingView.rating', '.stars i', function(event) {
event.stopPropagation();
let starVal = $(this).index();
self.rating(starVal);
})
.on('click.RatingView.close', '.close', $.proxy(this.toggle, this, false))
.on('moreComment.toggle', '.more-comment', function(event, starVal) {
let $this = $(this);
let visible = $this.is(':visible');
if (starVal < 3 && visible) {
return;
}
$this.find('.words-to-say').val('');
$this.find('.cause span').removeClass('select');
if (starVal < 3) {
let cvId = self.cmEntity.conversationId;
return self.renderReasons(cvId);
}
$this.hide();
})
.on('click.CauseItem.SelectToggle', '.cause span', function() {
let $this = $(this);
$this.toggleClass('select');
});
},
renderReasons(cvId) {
let self = this;
if (self.reasonsRendered) {
self.$more.show();
return;
}
// 评价原因渲染
api.queryReasons({
conversationId: cvId
})
.done(res => {
if (res && res.code === 200) {
let $html;
let cause = [];
res.data.forEach((item) => {
cause.push(`<span data-id="${item.id}">${item.content}</span>`);
});
$html = cause.join('');
self.$more.find('.cause').append($html);
self.reasonsRendered = true;
self.$more.show();
return;
}
tip.show('拉取评价原因失败');
})
.fail(()=> {
tip.show('拉取评价原因失败');
});
},
rating(starVal) {
const rankMap = {
0: '非常不满意',
1: '不满意',
2: '一般',
3: '满意',
4: '非常满意',
};
this.rank = starVal;
this.rankText = rankMap[starVal] || rankMap[3];
this.$label.text(this.rankText);
this.$ranks.removeClass('rated');
this.$ranks.toggleClass(index => {
return index <= starVal ? 'rated' : null;
});
// added for customer service v1.2 optimization
this.$more.trigger('moreComment.toggle', starVal);
},
post(data) {
if (this.isSending) {
tip.show('正在发送..');
return;
}
this.isSending = true;
const self = this,
elem = this.elem;
var params = {
stars: ++this.rank,
promoter: 1,
reasonIds: '',
reasonMsg: elem.find('.words-to-say').val(),
};
let temp = [];
let $select = self.$more.find('.cause span.select');
[].forEach.call($select, (item) => {
temp.push($(item).data('id'));
});
params.reasonIds = temp.join(':');
Object.assign(params, data);
api.saveEvalute(params)
.done(res => {
if (res && res.code === 200) {
elem.hide();
elem.trigger('rating-success', [self.rankText]);
return;
}
tip.show('评价失败');
})
.fail(()=> {
tip.show('评价失败');
})
.always(() => {
self.isSending = false;
});
},
toggle(willShow) {
if (willShow === this.elem.is(':visible')) {
return;
}
this.elem.toggle(willShow);
this.rating(4);
return false;
}
});
RatingView.prototype.constructer = RatingView;
const OrderListView = function(elem) {
this.$elem = $(elem);
this.orderListT = require('service/chat/orders.hbs');
this.$localOrders = this.$elem.find('[data-tab=local]');
this.$globalOrders = this.$elem.find('[data-tab=global]');
// 分页数据
this.state = {
global: {
curCount: 0,
totalCount: null
},
order: {
curCount: 0,
totalCount: null
}
};
this.bindEvents();
// this.fetchOrders();
};
OrderListView.prototype = $.extend({}, EventEmitter.prototype, {
bindEvents: function() {
const self = this;
this.$elem
.on('click.OrderListView.back', '.chat-page-back', function(event) {
event.stopPropagation();
self.toggleHide(true);
})
.on('click.OrderListView.select', '[data-action=select]', function(event) {
event.stopPropagation();
self.selectOrder($(event.target));
});
// 被通知显示,执行显示
this.on('show.OderListView', (event, type) => {
self.toggleHide(false, type);
});
},
/**
* method: 选择订单,然后关闭
*/
selectOrder: function($btn) {
let $order = $btn.closest('.order-page');
// 订单数据
let data = {
imgSrc: $order.find('.thumb').attr('src'),
orderCode: $order.find('[data-id]').attr('data-id'),
cost: $order.find('.sum-cost').text(),
createTime: $order.find('.js-create-time').val(),
orderStatus: $order.find('.order-status').text()
};
// 通知chat,订单列表已选择订单
this.trigger('selectOrder.OrderListView', [data]);
// 关闭
this.toggleHide(true);
},
/**
* method: show/hide 订单列表
* @param boolean willHide;
* @return this
*/
toggleHide: function(willHide, type) {
this.$elem.toggleClass('chat-page-hide', willHide);
if (!willHide) {
let orderState = this.state[type];
let {totalCount} = orderState;
totalCount === null && this.fetchOrderByType(type, orderState);
if (type === 'global') {
this.$globalOrders.show();
this.$localOrders.hide();
} else if (type === 'order') {
this.$globalOrders.hide();
this.$localOrders.show();
}
} else {
this.$globalOrders.hide();
this.$localOrders.hide();
}
return this;
},
fetchOrderByType: function(type, orderState) {
let self = this;
let $order = type === 'global' ? this.$globalOrders : this.$localOrders;
let {totalCount} = orderState;
// TODO: 分页
if (totalCount === null) { // 还未加载数据
api.fetchOrders(type).done(
result => {
let html = self.orderListT({
orders: result.data,
isApp: appBridge.isApp,
isGlobal: type === 'global'
});
orderState.totalCount = result.data.length;
orderState.curCount = result.data.length;
$order.append(html);
}
);
}
}
});
OrderListView.prototype.constructer = OrderListView;
export {RatingView, LeaveMSGView, OrderListView};