helpler.js
6.99 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
/*
* @Descripttion:
* @version: 1.0.0
* @Author: limin
* @Date: 2025-01-06 14:07:42
* @LastEditors: limin
* @LastEditTime: 2026-01-22 15:47:34
* @FilePath: \gl_tape\utils\recordManager\helpler.js
*/
import utils from '../utils'
import PathUtils from './PathUtils'
/**
* 内部方法,用于获取完整的音频文件路径
*
* 此函数调用 `getAudioPathName` 获取文件名,并通过 `getAudioPath` 获取完整路径。
* @param {string} excludePath - 需要排除的文件路径(完整路径)
*/
async function getLatestAudioFilePath(excludePath = '') {
try {
// 从完整路径中提取文件名,用于底层过滤
let excludeName = ''
if (excludePath) {
excludeName = excludePath.split('/').pop()
}
const pathName = await __getAudioPathName(excludeName)
if (!pathName) {
uni.$logger.local.warn(`【接待录音】未找到录音文件, excludeName=${excludeName}`)
return ''
}
const fullPath = await __getAudioPath(pathName)
uni.$logger.local.info(`【接待录音】找到最新录音: ${fullPath}`)
return fullPath
} catch (error) {
uni.$logger.local.warn('〖接待记录Record〗 == 获取最新录音路径异常: ' + error.message)
return ''
}
}
/**
* 异步获取音频文件路径名称
*
* 此函数用于查找特定目录中所有以 'uniapp_temp' 开头的文件,并返回修改时间最晚的文件名。
* 如果没有找到符合条件的文件,则返回一个空字符串或抛出错误。
* @param {string} excludeName - 需要排除的文件名
*/
async function __getAudioPathName(excludeName = '') {
const path = await PathUtils.getAudioFilePath()
return __getLatestFileName(path, 'uniapp_temp', excludeName)
}
/**
* 异步获取指定名称的音频文件路径
*
* 此函数用于根据给定的文件名查找其完整路径,并返回修改时间最晚的文件路径。
* 如果没有找到符合条件的文件,则返回一个空字符串或抛出错误。
* @param {string} name - 文件名(不包含扩展名)
*/
async function __getAudioPath(name) {
const path = await PathUtils.getAudioFilePath(name + '/recorder')
return __getLatestFilePath(path)
}
/**
* 获取指定目录中修改时间最晚的文件名
*
* @param {string} directoryPath - 目录路径
* @param {string} prefix - 文件名前缀(可选)
* @param {string} excludeName - 需要排除的文件名(可选)
* @returns {Promise<string>} - 修改时间最晚的文件名
*/
async function __getLatestFileName(directoryPath, prefix = '', excludeName = '') {
const latestFile = await __getLatestFile(directoryPath, prefix, excludeName)
return latestFile ? latestFile.name : ''
}
/**
* 获取指定目录中修改时间最晚的文件路径
*
* @param {string} directoryPath - 目录路径
* @returns {Promise<string>} - 修改时间最晚的文件路径
*/
async function __getLatestFilePath(directoryPath) {
const latestFile = await __getLatestFile(directoryPath)
return latestFile ? directoryPath + '/' + latestFile.name : ''
}
/**
* 获取文件的元数据
*
* @param {object} fileEntry - 文件条目
* @returns {Promise<object>} - 文件元数据
*/
function __getFileMetadata(fileEntry) {
return new Promise(resolve => {
fileEntry.getMetadata(
metadata => {
resolve({
name: fileEntry.name,
modificationTime: metadata.modificationTime.getTime()
})
},
e => {
uni.$logger.local.warn(`〖接待记录Record〗 == 获取文件元数据失败: ${fileEntry.name} ${e.message}`)
resolve({
name: fileEntry.name,
modificationTime: 0
})
}
)
})
}
/**
* 获取指定目录中修改时间最晚的文件信息
*
* @param {string} directoryPath - 目录路径
* @param {string} prefix - 文件名前缀(可选)
* @param {string} excludeName - 需要排除的文件名(可选)
* @returns {Promise<object|null>} - 文件对象 {name, modificationTime} 或 null
*/
async function __getLatestFile(directoryPath, prefix = '', excludeName = '') {
return new Promise((resolve, reject) => {
// [测试注入] 模拟系统异常:劫持 API 强制失败
// const originalResolve = plus.io.resolveLocalFileSystemURL
// plus.io.resolveLocalFileSystemURL = function (path, success, fail) {
// uni.$logger.local.info(`〖接待记录Record〗 == [测试模拟] 拦截目录读取: ${path}`)
// setTimeout(() => {
// fail && fail({ code: 12, message: 'Simulated Path Not Found' })
// // 恢复 API,避免影响全局
// plus.io.resolveLocalFileSystemURL = originalResolve
// }, 0)
// }
plus.io.resolveLocalFileSystemURL(
directoryPath,
e => {
const dirR = e.createReader()
dirR.readEntries(
async entries => {
try {
// 先根据前缀和排除名单进行过滤
const files = entries.filter(item => {
if (prefix && !item.name.startsWith(prefix)) return false
if (excludeName && item.name === excludeName) return false
return true
})
const filePromises = files.map(item => __getFileMetadata(item))
const results = await Promise.all(filePromises)
const latest = results.reduce((acc, current) => {
// 优先尝试从文件名解析时间戳 (假设文件名是时间戳或包含时间戳)
// 例如: 1767921591256.mp3 -> 1767921591256
const getTimestamp = (name) => {
if (!name) return 0
const match = name.match(/(\d{13})/) // 匹配13位毫秒时间戳
return match ? parseInt(match[1]) : 0
}
const accTs = getTimestamp(acc.name)
const currTs = getTimestamp(current.name)
if (accTs > 0 && currTs > 0) {
return currTs > accTs ? current : acc
}
// 回退到修改时间
return (current.modificationTime > acc.modificationTime ? current : acc)
}, { name: '', modificationTime: 0 })
resolve(latest.name ? latest : null)
} catch (error) {
uni.$logger.local.error(`〖接待记录Record〗 == 处理目录项异常: ${error.message}`)
resolve(null)
}
},
err => {
uni.$logger.local.warn(`〖接待记录Record〗 == 读取目录项失败: ${directoryPath}, err: ${JSON.stringify(err)} ${err.message}`)
resolve(null)
}
)
},
err => {
// 目录不存在或无法访问,视为无文件
uni.$logger.local.warn(`〖接待记录Record〗 == 目录不存在或无法访问: ${directoryPath} err: ${JSON.stringify(err)} ${err.message}`)
resolve(null)
}
)
})
}
async function sleep(time) {
return new Promise(resolve => setTimeout(resolve, time))
}
export default {
getLatestAudioFilePath,
sleep
}