TempRecord.js
11.7 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
/*
* @Descripttion:
* @version: 1.0.0
* @Author: limin
* @Date: 2025-01-02 18:49:17
* @LastEditors: limin
* @LastEditTime: 2026-06-11 16:29:07
* @FilePath: \gl_tape\utils\recordManager\TempRecord.js
*/
import utils from '../utils'
import { FileStatus } from '../common/constants'
import { RecordCacheManager, PathUtils, UploadService,RecordSaveService } from './index.js'
class TempRecord {
timer = null
timer_interval = 30 * 1000
record = null
path = ''
newRecord = null
onTempRecordNumberChanged = null
_isDestroyed = false // 新增销毁标志位
constructor(record, onTempRecordNumberChanged, attributesProvider) {
this.newRecord = record
// this.dbRecord = uni.$dbRecord // 延迟获取,避免初始化顺序问题
this.onTempRecordNumberChanged = onTempRecordNumberChanged
this.attributesProvider = attributesProvider
}
get dbRecord() {
return uni.$dbRecord
}
/**
* 获取全部临时记录
* @returns {Array} 临时记录列表
*/
async getRecords() {
return this.dbRecord.getTempRecords()
}
/**
* 根据 id 获取单条临时记录
* @param {string} id - 临时记录的唯一标识
* @returns {Object|null} 找到则返回记录,否则返回 null
*/
getRecord(id) {
return this.dbRecord.getRecordById(id)
}
/**
* 添加一条临时记录并触发数量变化通知
* @param {Object} [record=this.record] - 要添加的记录对象
* @returns {Promise<void>}
*/
async addRecord(record = this.record) {
try {
await this.dbRecord.addRecord(record)
uni.$logger.local.info('〖接待记录Record〗 == 新增临时记录 id: ' + record.id)
this.triggerRecordNumChanged()
} catch (e) {
uni.$logger.local.error('〖接待记录Record〗 == 新增临时记录失败: ' + e.message)
}
}
/**
* 更新临时记录(部分字段)
* @param {Object} item - 需要更新的字段键值对
* @param {Object} where - 更新条件(一般为 { id })
* @returns {Promise<void>}
*/
updateRecord(item, where) {
return this.dbRecord.updateRecord(item, where)
}
async __saveData(toUpdateData = {}, record) {
try {
if (!record) throw new Error('记录对象为空')
uni.$logger.local.info('〖接待记录Record〗 == 开始持久化记录 ==' + JSON.stringify(record))
uni.$logger.local.info(`【接待录音】转正入库: recordId=${record.id}, fileCount=${record.recordUrlList?.length || 0}`)
uni.$logger.local.info('〖接待记录Record〗 == 更新文件,保存记录!')
// 直接更新原记录,不再插入新记录
await this.dbRecord.updateRecord(toUpdateData, { id: record.id })
// 删除本地临时文件 (不再删除数据库记录,因为已经转正)
await this.clearLocalRecord(record, false) // 传入 false 表示不删除数据库记录
this.triggerRecordNumChanged()
// 触发轮询开始事件,通知上传管理重新开始轮询
uni.$logger.local.info(`【接待录音】触发上传轮询: recordId=${record.id}`)
RecordSaveService.startPolling()
} catch (error) {
uni.$logger.local.info('〖接待记录Record〗 == 保存记录异常' + error.message)
}
}
saveInternal(record) {
if (!record || !record.id) return
const { id, recordUrlList, tempFilePath, ...__record } = record
return this.__save(record, __record)
}
async save(record = this.record) {
const fileCount = record?.recordUrlList?.length || 0
const files = (record?.recordUrlList || []).map(i => i.file).join(', ')
uni.$logger.local.info(`【接待录音】TempRecord.save: recordId=${record?.id}, fileCount=${fileCount}, files=[${files}]`)
await this.beforeSave(record)
return this.__save(record, {})
}
/**
* 将当前临时记录持久化到正式记录表,并清理本地数据
* - 开始前会停止轮询
* - 会对过长的录音列表进行截断
* @param {Object} [record=this.record] - 要保存的记录
* @returns {Promise<void>}
*/
async __save(record, toUpdateData) {
try {
// const { recordUrlList } = record
// 从缓存恢复大字段数据
const restoredData = RecordCacheManager.consumeAll(record)
// const recordLongList = await this.analysisUrlList(recordUrlList)
const updateData = {
...toUpdateData,
recordStatus: 1,
// recordUrlList: recordLongList,
...restoredData // 合并恢复的大字段数据
}
return this.__saveData(updateData, record)
} catch (error) {
uni.$logger.local.info('〖接待记录Record〗 == 保存记录异常' + error.message)
}
}
/**
* 将已保存的文件列表映射为上传列表(统一拼接基础保存路径)
* @param {Array} recordUrlList - 已保存文件路径列表(形如 { file })
* @returns {Promise<Array>} 规范化后的上传列表(形如 { status, file })
*/
// async analysisUrlList(recordUrlList) {
// if (!recordUrlList?.length) return []
// try {
// const basePath = await PathUtils.getBasePath()
// const list = []
// for (const item of recordUrlList) {
// const filePath = item?.file
// if (typeof filePath === 'string' && filePath.length > 0) {
// const baseName = filePath.split('/').pop()
// list.push({
// status: FileStatus.Waiting,
// file: PathUtils.join(basePath, baseName)
// })
// } else {
// uni.$logger.local.warn('〖接待记录Record〗 == 有文件为空路径,舍弃!! ==')
// }
// }
// return list
// } catch (error) {
// uni.$logger.local.info('〖接待记录Record〗 == 解析接待文件列表异常' + error.message)
// throw new Error(error.message)
// }
// }
/**
* 删除一条临时记录并触发数量变化通知
* @param {string} id - 临时记录 id
* @returns {Promise<void>}
*/
async removeRecord(id) {
await this.dbRecord.removeRecord(id)
uni.$logger.local.info('〖接待记录Record〗 == 删除临时记录 id: ' + id)
this.triggerRecordNumChanged()
}
/**
* 更新当前内存中的记录并同步到数据库(值未变则跳过)
* @param {Object} items - 包含多个字段的对象 { key: value, ... }
* @returns {Promise<void>|undefined} 值未变化时返回 undefined
*/
async __update(items) {
if (!this.record || !items || typeof items !== 'object') return
const updates = Object.fromEntries(Object.entries(items).filter(([k, v]) => !utils.nullValues.includes(v) && this.record[k] !== v))
if (Object.keys(updates).length) {
Object.assign(this.record, updates)
return this.updateRecord(updates, { id: this.record.id })
}
}
/**
* 彻底删除一条接待记录
* @param {Object} record - 要删除的记录对象
* @returns {Promise<void>}
*/
async deleteRecord(record) {
if (!record) return
uni.$logger.local.warn('〖接待记录Record〗 == 开始彻底删除记录 id: ' + record.id)
if (record.recordUrlList?.length) {
await this.clearSavedFiles(record)
}
await this.clearLocalRecord(record, true)
uni.$logger.local.warn('〖接待记录Record〗 == 记录彻底删除完成')
}
async clearLocalRecord(record = this.record, removeDbRecord = true) {
const { id, tempFilePath } = record || {}
uni.$logger.local.info('〖接待记录Record〗 == 删除临时文件' + tempFilePath)
await this.removeFile(tempFilePath)
if (id && removeDbRecord) {
await this.removeRecord(id)
}
RecordCacheManager.clearAll(id)
this.record = null
this.path = ''
uni.$logger.local.info('〖接待记录Record〗 == 置空接待记录')
}
/**
* 清除已保存的录音文件
* @param {Object} options - 配置参数
* @param {Array} options.recordUrlList - 录音文件列表,默认为当前记录的recordUrlList
*/
async clearSavedFiles({ recordUrlList = [] } = this.record) {
for (const item of recordUrlList) {
uni.$logger.local.info('〖接待记录Record〗 == 清除已保存文件 -> ' + item.file)
await this.removeFile(item.file)
}
}
/**
* 缓存物料点数据
* @param {Array} list
*/
cacheMaterialPoints(list) {
if (this.record && this.record.id) {
RecordCacheManager.set(this.record.id, 'MATERIAL_POINTS', list)
}
}
/**
* 删除文件(持久化路径使用 `uni.removeSavedFile`)
* @param {string} filePath - 要删除的文件路径(可能为 `_doc/...` 或其他)
* @returns {Promise<void>}
*/
async removeFile(filePath) {
if (!filePath) return
let file = filePath
// 若非 file:// 开头的绝对路径,则补全为本地存储目录下的绝对路径
if (!filePath.startsWith('file://')) {
// 优化:使用 getBasePath() 利用缓存,减少 Native Bridge 调用
const saveDir = await PathUtils.getBasePath()
// 仅保留文件名,防止 _doc/ 等相对路径前缀干扰
file = PathUtils.join(saveDir, filePath.split('/').pop())
}
return new Promise(resolve => {
try {
uni.removeSavedFile({ filePath: file, complete: resolve })
} catch (e) {
resolve()
}
})
}
/**
* 停止保存轮询并清理定时器句柄
* @returns {void}
*/
clearLoopTimer() {
if (this.timer) {
clearInterval(this.timer)
this.timer = null
}
}
async destroy() {
this._isDestroyed = true
this.clearLoopTimer()
uni.$logger.local.info('〖接待记录Record〗 == TempRecord 实例已销毁')
}
/**
* 获取当前内存中的接待记录对象
* @returns {Object|null} 当前记录,可能为空
*/
get currentRecord() {
return this.record
}
/**
* 设置接待结束时间为当前时间并同步到数据库
* @returns {Promise<void>}
*/
setReceptionEndTime() {
return this.__update({ receptionEndTime: this.getTimeNow() })
}
/**
* 获取当前时间字符串(格式化)
* @returns {string} 形如 `YYYY/MM/DD HH:mm:ss`
*/
getTimeNow() {
return utils.convertDateTimeFormat(new Date(), '/')
}
/**
* 标记记录为错误结束原因
* @returns {Promise<void>}
*/
withError() {
return this.__update({ endReasonType: 1 })
}
/**
* 批量更新当前记录的多个预填字段
* @param {Object} items - 需要更新的键值对
* @returns {Promise<Object>} 返回更新后的记录对象
*/
async updatePreRecords(items) {
await this.__update(items)
return this.record
}
/**
* 通用添加记录流程
* - 创建内存中的记录对象
* - 同步写入临时记录数据库
* @param {Array} initialUrlList - 初始文件列表
* @returns {Promise<void>}
*/
async add(initialUrlList = []) {
this.record = this.__createNewRecord(initialUrlList)
await this.addRecord(this.record)
}
/**
* 构造一条新的接待记录的初始对象
* @param {Array} initialUrlList - 初始的文件列表
* @returns {Object} 新记录对象
*/
__createNewRecord(initialUrlList = []) {
const dynamicAttributes = this.attributesProvider ? this.attributesProvider() : {}
return {
id: utils.uuid(),
...this.newRecord,
...dynamicAttributes,
receptionStartTime: this.getTimeNow(),
receptionEndTime: '',
recordUrlList: initialUrlList,
recordStatus: 0
}
}
/**
* 保存前的钩子函数 (子类可覆盖)
* @param {Object} record
* @returns {Promise<void>}
*/
async beforeSave(record) {
// 默认不做任何事
}
/**
* 触发临时记录数量变化通知
* @returns {Promise<void>|void}
*/
async triggerRecordNumChanged() {
if (this.onTempRecordNumberChanged) {
this.onTempRecordNumberChanged()
}
}
}
export default TempRecord