helpler.js
5.04 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
import utils from '../utils'
/**
* 内部方法,用于获取完整的音频文件路径
*
* 此函数调用 `getAudioPathName` 获取文件名,并通过 `getAudioPath` 获取完整路径。
*/
async function getLatestAudioFilePath() {
try {
const pathName = await __getAudioPathName()
if (!pathName) return ''
return await __getAudioPath(pathName)
} catch (error) {
uni.$logger.local.warn('〖接待记录Record〗 == 获取最新录音路径异常: ' + error.message)
return ''
}
}
/**
* 异步获取音频文件路径名称
*
* 此函数用于查找特定目录中所有以 'uniapp_temp' 开头的文件,并返回修改时间最晚的文件名。
* 如果没有找到符合条件的文件,则返回一个空字符串或抛出错误。
*/
async function __getAudioPathName() {
const path = await utils.getAudioFilePath()
return __getLatestFileName(path, 'uniapp_temp')
}
/**
* 异步获取指定名称的音频文件路径
*
* 此函数用于根据给定的文件名查找其完整路径,并返回修改时间最晚的文件路径。
* 如果没有找到符合条件的文件,则返回一个空字符串或抛出错误。
* @param {string} name - 文件名(不包含扩展名)
*/
async function __getAudioPath(name) {
const path = await utils.getAudioFilePath(name + '/recorder')
return __getLatestFilePath(path)
}
/**
* 获取指定目录中修改时间最晚的文件名
*
* @param {string} directoryPath - 目录路径
* @param {string} prefix - 文件名前缀(可选)
* @returns {Promise<string>} - 修改时间最晚的文件名
*/
async function __getLatestFileName(directoryPath, prefix = '') {
const latestFile = await __getLatestFile(directoryPath, prefix)
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 - 文件名前缀(可选)
* @returns {Promise<object|null>} - 文件对象 {name, modificationTime} 或 null
*/
async function __getLatestFile(directoryPath, prefix = '') {
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 => (prefix ? item.name.startsWith(prefix) : true))
const filePromises = files.map(item => __getFileMetadata(item))
const results = await Promise.all(filePromises)
const latest = results.reduce((acc, current) => (current.modificationTime > acc.modificationTime ? current : acc), { 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
}