Commit bc02d785 by liimin

fix(record): 优化录音文件处理和异常恢复逻辑

修复SQLite中JSON解析可能导致数字精度丢失的问题
改进录音文件查找逻辑,增加排除路径和文件名时间戳优化
重构TempRecord类,简化流程并增强销毁处理
移除不必要的日志输出,提升性能
1 parent 79555092
...@@ -29,13 +29,16 @@ class SQLite { ...@@ -29,13 +29,16 @@ class SQLite {
const processedRows = result.map(row => const processedRows = result.map(row =>
Object.fromEntries( Object.fromEntries(
Object.entries(row).map(([key, value]) => { Object.entries(row).map(([key, value]) => {
// 优化:仅当 value 是字符串且以 { 或 [ 开头时才尝试解析
// 避免将普通数字字符串(如 "123")误解析为 Number 导致精度丢失
if (typeof value === 'string' && (value.startsWith('{') || value.startsWith('['))) {
try { try {
// 尝试将字符串解析为对象或数组
return [key, JSON.parse(value)] return [key, JSON.parse(value)]
} catch { } catch {
// 如果解析失败,保留原始值
return [key, value] return [key, value]
} }
}
return [key, value]
}) })
) )
) )
...@@ -153,7 +156,7 @@ class SQLite { ...@@ -153,7 +156,7 @@ class SQLite {
} }
} }
drop(tabName) { drop(tabName) {
const sql = `DROP TABLE ${tabName}` const sql = `DROP TABLE IF EXISTS ${tabName}`
return this.executeSql(sql) return this.executeSql(sql)
} }
formatValue(value) { formatValue(value) {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: 前端 qianduan01@didihu.com.cn * @Author: 前端 qianduan01@didihu.com.cn
* @Date: 2024-12-26 10:03:54 * @Date: 2024-12-26 10:03:54
* @LastEditors: limin * @LastEditors: limin
* @LastEditTime: 2026-01-08 16:31:08 * @LastEditTime: 2026-01-09 16:14:40
* @FilePath: \simgle_app\mixin\record\recorder.js * @FilePath: \simgle_app\mixin\record\recorder.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/ */
......
...@@ -3,10 +3,17 @@ import utils from '../utils' ...@@ -3,10 +3,17 @@ import utils from '../utils'
* 内部方法,用于获取完整的音频文件路径 * 内部方法,用于获取完整的音频文件路径
* *
* 此函数调用 `getAudioPathName` 获取文件名,并通过 `getAudioPath` 获取完整路径。 * 此函数调用 `getAudioPathName` 获取文件名,并通过 `getAudioPath` 获取完整路径。
* @param {string} excludePath - 需要排除的文件路径(完整路径)
*/ */
async function getLatestAudioFilePath() { async function getLatestAudioFilePath(excludePath = '') {
try { try {
const pathName = await __getAudioPathName() // 从完整路径中提取文件名,用于底层过滤
let excludeName = ''
if (excludePath) {
excludeName = excludePath.split('/').pop()
}
const pathName = await __getAudioPathName(excludeName)
if (!pathName) return '' if (!pathName) return ''
return await __getAudioPath(pathName) return await __getAudioPath(pathName)
} catch (error) { } catch (error) {
...@@ -14,18 +21,17 @@ async function getLatestAudioFilePath() { ...@@ -14,18 +21,17 @@ async function getLatestAudioFilePath() {
return '' return ''
} }
} }
/** /**
* 异步获取音频文件路径名称 * 异步获取音频文件路径名称
* *
* 此函数用于查找特定目录中所有以 'uniapp_temp' 开头的文件,并返回修改时间最晚的文件名。 * 此函数用于查找特定目录中所有以 'uniapp_temp' 开头的文件,并返回修改时间最晚的文件名。
* 如果没有找到符合条件的文件,则返回一个空字符串或抛出错误。 * 如果没有找到符合条件的文件,则返回一个空字符串或抛出错误。
* @param {string} excludeName - 需要排除的文件名
*/ */
async function __getAudioPathName() { async function __getAudioPathName(excludeName = '') {
const path = await utils.getAudioFilePath() const path = await utils.getAudioFilePath()
return __getLatestFileName(path, 'uniapp_temp') return __getLatestFileName(path, 'uniapp_temp', excludeName)
} }
/** /**
* 异步获取指定名称的音频文件路径 * 异步获取指定名称的音频文件路径
* *
...@@ -37,19 +43,18 @@ async function __getAudioPath(name) { ...@@ -37,19 +43,18 @@ async function __getAudioPath(name) {
const path = await utils.getAudioFilePath(name + '/recorder') const path = await utils.getAudioFilePath(name + '/recorder')
return __getLatestFilePath(path) return __getLatestFilePath(path)
} }
/** /**
* 获取指定目录中修改时间最晚的文件名 * 获取指定目录中修改时间最晚的文件名
* *
* @param {string} directoryPath - 目录路径 * @param {string} directoryPath - 目录路径
* @param {string} prefix - 文件名前缀(可选) * @param {string} prefix - 文件名前缀(可选)
* @param {string} excludeName - 需要排除的文件名(可选)
* @returns {Promise<string>} - 修改时间最晚的文件名 * @returns {Promise<string>} - 修改时间最晚的文件名
*/ */
async function __getLatestFileName(directoryPath, prefix = '') { async function __getLatestFileName(directoryPath, prefix = '', excludeName = '') {
const latestFile = await __getLatestFile(directoryPath, prefix) const latestFile = await __getLatestFile(directoryPath, prefix, excludeName)
return latestFile ? latestFile.name : '' return latestFile ? latestFile.name : ''
} }
/** /**
* 获取指定目录中修改时间最晚的文件路径 * 获取指定目录中修改时间最晚的文件路径
* *
...@@ -60,7 +65,6 @@ async function __getLatestFilePath(directoryPath) { ...@@ -60,7 +65,6 @@ async function __getLatestFilePath(directoryPath) {
const latestFile = await __getLatestFile(directoryPath) const latestFile = await __getLatestFile(directoryPath)
return latestFile ? directoryPath + '/' + latestFile.name : '' return latestFile ? directoryPath + '/' + latestFile.name : ''
} }
/** /**
* 获取文件的元数据 * 获取文件的元数据
* *
...@@ -86,15 +90,15 @@ function __getFileMetadata(fileEntry) { ...@@ -86,15 +90,15 @@ function __getFileMetadata(fileEntry) {
) )
}) })
} }
/** /**
* 获取指定目录中修改时间最晚的文件信息 * 获取指定目录中修改时间最晚的文件信息
* *
* @param {string} directoryPath - 目录路径 * @param {string} directoryPath - 目录路径
* @param {string} prefix - 文件名前缀(可选) * @param {string} prefix - 文件名前缀(可选)
* @param {string} excludeName - 需要排除的文件名(可选)
* @returns {Promise<object|null>} - 文件对象 {name, modificationTime} 或 null * @returns {Promise<object|null>} - 文件对象 {name, modificationTime} 或 null
*/ */
async function __getLatestFile(directoryPath, prefix = '') { async function __getLatestFile(directoryPath, prefix = '', excludeName = '') {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// [测试注入] 模拟系统异常:劫持 API 强制失败 // [测试注入] 模拟系统异常:劫持 API 强制失败
// const originalResolve = plus.io.resolveLocalFileSystemURL // const originalResolve = plus.io.resolveLocalFileSystemURL
...@@ -113,10 +117,33 @@ async function __getLatestFile(directoryPath, prefix = '') { ...@@ -113,10 +117,33 @@ async function __getLatestFile(directoryPath, prefix = '') {
dirR.readEntries( dirR.readEntries(
async entries => { async entries => {
try { try {
const files = entries.filter(item => (prefix ? item.name.startsWith(prefix) : true)) // 先根据前缀和排除名单进行过滤
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 filePromises = files.map(item => __getFileMetadata(item))
const results = await Promise.all(filePromises) const results = await Promise.all(filePromises)
const latest = results.reduce((acc, current) => (current.modificationTime > acc.modificationTime ? current : acc), { modificationTime: 0 }) 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) resolve(latest.name ? latest : null)
} catch (error) { } catch (error) {
uni.$logger.local.error(`〖接待记录Record〗 == 处理目录项异常: ${error.message}`) uni.$logger.local.error(`〖接待记录Record〗 == 处理目录项异常: ${error.message}`)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!