interruptRecord.js
5.86 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
/*
* @Author: 前端 qianduan01@didihu.com.cn
* @Date: 2024-10-14 17:38:35
* @LastEditors: 前端 qianduan01@didihu.com.cn
* @LastEditTime: 2024-10-18 18:09:15
* @FilePath: \gl-tape\utils\recordManager\interruptRecord.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import RecordManager from '../RecordManager.js'
import utils from '@/utils/utils.js'
import { storageApi } from '../cache'
import store from '@/store'
class InterruptRecord extends RecordManager {
constructor() {
super()
const records = this.getRecords()
store.dispatch('audio/setPerRecordNumber', records.length)
// console.log(store)
// storageApi.removeRecords('preRecordList')
// storageApi.removeRecords('preRecordId')
// storageApi.removeRecords('recordList')
}
records = {}
pathName = null
/**
* 获取记录列表
*
* @returns {Array} - 从 storageApi 获取的记录列表
*/
getRecords() {
console.log(storageApi.getRecords('preRecordList'), 'ddd')
return storageApi.getRecords('preRecordList') || []
}
/**
* 获取待提交的记录id
*
*/
getPreRecord() {
const id = storageApi.getRecords('preRecordId')
if (!id) {
return
}
return this.getRecordById(id)
}
/**
* 删除待提交的记录
*
*/
removePreRecord(id) {
this.removeRecord(id)
storageApi.setRecords('', 'preRecordId')
}
/**
* 初始化保存记录
*
* @param {string} name - 录音文件目录名
* @returns {void} - 此函数不返回任何值
*/
async addPreRecords(params) {
console.log(storageApi.getRecords('preRecordId'), 6666)
if (storageApi.getRecords('preRecordId')) {
this.addPreAudio()
return
}
console.log('pathName')
const pathName = await this.getAudioPathName()
const audioPath = await this.getAudioPath(pathName)
const id = utils.uuid()
storageApi.setRecords(id, 'preRecordId')
const ids = storageApi.getRecords('preRecordId')
console.log(ids, 'id')
this.records = {
id,
...params,
audioPath: [audioPath]
}
const records = this.getRecords() || []
records.push(this.records)
this.setRecords(records)
}
/**
* 添加录音文件
*
* @returns {void} - 此函数不返回任何值
*/
async addPreAudio() {
const pathName = await this.getAudioPathName()
const audioPath = await this.getAudioPath(pathName)
this.records.audioPath.push(audioPath)
const records = this.getPreRecord()
const params = { ...records, ...this.records }
this.updateRecord(params)
}
/**
* 更新参数
*
* @returns {void} - 此函数不返回任何值
*/
updatePreRecords(params, isRemoveId) {
if (isRemoveId) {
storageApi.setRecords('', 'preRecordId')
}
const updateParams = { ...this.records, ...params }
console.log(updateParams, 'updateParams')
this.updateRecord(updateParams)
}
/**
* 获取录音文件全路径
*
* @param {string} name - 录音文件目录名
* @returns {void} - 此函数不返回任何值
*/
async getAudioPath(name) {
console.log(name, 'name')
return new Promise(async (resolve, reject) => {
const path = await utils.getAudioFilePath(name + '/recorder')
console.log(path, 'path')
plus.io.resolveLocalFileSystemURL(
path,
e => {
var dirR = e.createReader()
dirR.readEntries(
function (er) {
Promise.all(
er.map((item, index) => {
return new Promise(resolve => {
item.getMetadata(metadata => {
resolve({
index: index,
name: item.name,
modificationTime: new Date(metadata.modificationTime).getTime()
})
})
})
})
).then(results => {
console.log(results, 'results')
// 找到修改时间最晚的文件
const latest = results.reduce(
(acc, current) => {
if (current.modificationTime > acc.modificationTime) {
return current
}
return acc
},
{ modificationTime: 0 }
)
// 修改时间最晚的文件名
resolve(path + '/' + latest.name)
})
},
err => {
reject(err)
}
)
},
err => {}
)
})
}
/**
* 获取录音文件名
*
* @returns {void} - 此函数不返回任何值
*/
async getAudioPathName() {
return new Promise(async (resolve, reject) => {
const path = await utils.getAudioFilePath()
plus.io.resolveLocalFileSystemURL(
path,
e => {
var dirR = e.createReader()
dirR.readEntries(
function (er) {
for (var i = 0; i < er.length; i++) {
if (er[i].name.startsWith('uniapp_temp')) {
resolve(er[i].name)
}
// er[i].getMetadata(function (metadata) {
// console.log(metadata.modificationTime, '修改时间')
// })
}
},
err => {
console.log(err)
}
)
},
err => {}
)
})
}
/**
* 设置记录列表
*
* @param {Array} records - 要设置的记录列表
* @returns {void} - 此函数不返回任何值
*/
setRecords(records) {
storageApi.setRecords(records, 'preRecordList')
console.log(records, 'records')
store.dispatch('audio/setPerRecordNumber', records.length)
}
}
export default new InterruptRecord()