dialogue.js
9.61 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
// packageEquity/pages/receptionDetail/components/dialogue/dialogue.js
let thisPage = null
Component({
/**
* 组件的属性列表
*/
properties: {
recordData: Object,
batchId: {
type: Number,
observer: function (n) {
if (this.IntersectionObserver) {
this.IntersectionObserver.disconnect()
this.IntersectionObserver = null
}
this.setData({
flag: false,
progress: 0,
dialogueList: []
})
const data = {
batchId: n,
};
this.findConversationDetailByBatchId(data)
}
},
totalDuration: {
type: Number,
value: 0
},
qualityData: {
type: Array,
observer: function (n) {
if (n.length) this.findCbByBatchId(n)
}
}
},
lifetimes: {
ready() {},
detached() {
this.IntersectionObserver?.disconnect()
}
},
/**
* 组件的初始数据
*/
data: {
progress: 0,
dialogueList: [],
},
/**
* 组件的方法列表
*/
methods: {
findCbByBatchId(data) {
let {
recordingDuration: totalDuration
} = this.data.recordData,
margin = 20
wx.p.wxApi.getViewInfo('.axis-bar', ({
height: containerHeight,
top: barTop
}) => {
if (containerHeight == 0) {
return setTimeout(() => {
this.findCbByBatchId()
}, 100)
}
this.barTop = barTop
this.containerHeight = containerHeight
let axisList = []
data?.sort((a, b) => a.offset - b.offset).forEach(e => {
const {
offset: mergedOffset,
} = e
e.top = mergedOffset / totalDuration * containerHeight
e.docTop = mergedOffset / totalDuration * containerHeight
e.text_id = `text_${mergedOffset}`
e.mergedOffset = mergedOffset
if (mergedOffset != null) axisList.push(e)
})
axisList.map((e, index) => {
// 检查并调整 top 值,避免重叠
for (let i = 0; i < index; i++) {
if (e.top < axisList[i].top + margin &&
e.top + margin > axisList[i].top) {
// 如果重叠,将当前元素的 top 设置为前一个元素的底部加上 margin
e.top = axisList[i].top + margin;
}
}
})
this.setData({
axisList
})
}, this)
},
getAudioObject() {
thisPage = wx.p.router.getPageInstance()
if (thisPage.qualityAudio) this.innerAudioContext = thisPage.qualityAudio
else setTimeout(() => {
this.getAudioObject()
}, 100)
},
onTimeUpdate() {
const {
dialogueList
} = this.data, {
paused
} = this.innerAudioContext, {
mergedOffset
} = this.setProgress(),
item = dialogueList.find(e => e.mergedOffset == mergedOffset)
if (!paused) this.setData({
isPlayIng: true
})
if (this.data.isScrollling) return
if (item && item.id) this.setTopId(item, true)
},
setTopId(item, scroll = false) {
const {
dialogueList
} = this.data
let centerIndex = dialogueList.findIndex(e => e.text_id == item.text_id)
centerIndex -= 3
if (centerIndex < 0) centerIndex = 0
wx.nextTick(() => {
this.setData({
activeId: item.text_id,
})
})
if (scroll) this.setData({
toId: dialogueList[centerIndex].text_id
})
},
setProgress() {
const {
duration,
currentTime
} = this.innerAudioContext,
mergedOffset = Math.ceil(currentTime)
this.setData({
progress: mergedOffset / duration * 100
})
return {
currentTime,
mergedOffset
}
},
toDefenceDetail(e) {
let {
item,
} = e.currentTarget.dataset
this.setData({
toId: item.text_id
})
this.setData({
isScrollling: true
})
this.touchend()
},
seekPlay(e) {
let {
item,
click,
scroll
} = e.currentTarget.dataset, {
activeId
} = this.data
if (!item) return
thisPage.seek(item.mergedOffset)
this.setTopId(item, scroll)
if (click && item.text_id == activeId) {
const {
paused
} = this.innerAudioContext
if (paused) thisPage.play()
else thisPage.onPause()
thisPage.setData({
isOperate: !paused
})
} else {
thisPage.play()
thisPage.setData({
isOperate: false
})
}
},
formatTime(num) {
let minutes = Math.floor(num / 60) || '0',
seconds = num % 60 || '0'
seconds = seconds < 10 ? `0${seconds}` : seconds
return `${minutes}:${seconds}`
},
relativeToViewport() {
if (this.IntersectionObserver) return
this.IntersectionObserver = this.createIntersectionObserver({
observeAll: true
})
this.IntersectionObserver.relativeToViewport({
bottom: 1000,
top: 0,
}).observe('.item', res => {
const {
index
} = res.dataset, {
dialogueList
} = this.data, {
originalList
} = this,
item = dialogueList[index]
if (!item?.visible && res.intersectionRatio > 0) {
this.setData({
[`dialogueList[${index}]`]: originalList[index]
})
}
})
},
/**对话列表 */
async findConversationDetailByBatchId(params) {
const dialogueList = await wx.p.api.receptionApi.findConversationDetailByBatchId(params)
this.getAudioObject()
dialogueList?.map(e => {
e.visible = true
e.time = this.formatTime(e.mergedOffset)
e.text_id = `text_${e.mergedOffset}`
if (e?.recordingCheckResultList && e.recordingCheckResultList.length) {
e.recordingCheckResultList.map(i => {
i.showCheckDetail = false
let [scriptRec] = JSON.parse(i.scriptRec)
if (scriptRec) i.scriptRec = scriptRec.replace(/\n/g, '<br>')
else i.scriptRec = ''
})
}
})
this.setData({
flag: true,
})
this.originalList = dialogueList
this.setDialogueData(dialogueList)
},
setDialogueData(dialogueList) {
wx.p.wxApi.showLoading('加载中...')
const batchSize = 100
let batch = []
// 遍历对话列表,累积数据
for (let i = 0; i < dialogueList.length; i++) {
batch.push({
visible: false,
text_id: dialogueList[i].text_id,
id: dialogueList[i].id,
mergedOffset: dialogueList[i].mergedOffset
});
// 当累积的数据达到批量大小时,执行 setData 更新
if ((i + 1) % batchSize === 0 || i === dialogueList.length - 1) {
this.setData({
dialogueList: [...this.data.dialogueList, ...batch]
});
batch = []; // 清空累积的数据,准备下一批
}
}
// // 确保最后不足100条的数据也被更新
if (batch.length > 0) {
this.setData({
dialogueList: [...this.data.dialogueList, ...batch]
})
}
// // 更新完成后清除加载提示
wx.hideLoading();
this.relativeToViewport();
},
/** 查询质检侧边栏 (质检使用id匹配,攻防用偏移量匹配) */
switchDetail(e) {
const {
index,
value,
ind,
key
} = e.currentTarget.dataset
let target = `dialogueList[${index}]`
if (ind > -1) target = `dialogueList[${index}].recordingCheckResultList[${ind}]`
this.setData({
[`${target}.${key}`]: !value
})
},
touchstart() {
this.setData({
isScrollling: true
})
this.clearTimer()
},
touchend() {
this.touchTimer = setTimeout(() => {
this.setData({
isScrollling: false
})
this.clearTimer()
}, 3e3)
},
clearTimer() {
if (this.touchTimer) {
clearTimeout(this.touchTimer)
this.touchTimer = null
}
},
onPause() {
this.setData({
isPlayIng: false
})
},
barSeek(e) {
function findNearest(arr, target) {
let closest = arr[0];
let minDiff = Math.abs(target - arr[0]);
for (let i = 1; i < arr.length; i++) {
const diff = Math.abs(target - arr[i]);
if (diff < minDiff) {
minDiff = diff;
closest = arr[i];
}
}
return closest;
}
let [touches] = e.touches, {
barTop,
containerHeight
} = this, {
duration
} = this.innerAudioContext, {
dialogueList
} = this.data
if (touches) {
let {
clientY
} = touches,
progress = (clientY - barTop) / containerHeight * 100,
offsetList = dialogueList.map(i => i.mergedOffset),
mergedOffset = findNearest(offsetList, duration * progress / 100),
item = dialogueList.find(i => i.mergedOffset == mergedOffset)
this.seekPlay({
currentTarget: {
dataset: {
item,
scroll: true
}
}
}, false)
}
},
}
})