Commit c72aa873 by liimin

refactor: 重构上传服务与录音管理初始化逻辑

- 将 RecordService 初始化移至构造函数,移除组件中的手动调用
- 优化 Webview 获取逻辑,增强健壮性与自动恢复能力
- 简化上传服务错误处理与任务调度,移除冗余黑名单检查
- 采用事件驱动方式初始化上传 Webview,提升可靠性
- 清理调试日志与无用代码,提升代码可维护性
1 parent 45724665
......@@ -105,9 +105,6 @@ export default {
this.userInfo = userInfo
this.currentType = uni.$storage.get('typeItem')
// if (this.checkRecordAudio) permision.requestAndroidPermission('android.permission.RECORD_AUDIO') // 进入界面时检查权限
// 初始化上传相关逻辑
this.initUploader()
},
provide() {
return {
......@@ -163,28 +160,23 @@ export default {
this.$emit('changeType')
},
handleUploadMessage(event) {
// 接收来自 Webview 的消息,并转发给 UploadService
// 接收来自 Webview 的消息
const data = event.detail.data[0]
if (data) {
// 构造 Service 需要的格式 { detail: { data: [payload] } }
// 或者直接让 Service 接收 payload
// 目前 Service.handlerMessage 期望的是 { detail: { data: [...] } }
UploadService.handlerMessage(event)
// 1. 处理 Webview 就绪信号 (Event-Driven 初始化)
if (data.action === 'webview_ready') {
uni.$logger.local.info('【上传服务】收到 Webview 就绪信号,开始注入 Context')
// 此时 Webview 必然已存在,无需延迟
this.$nextTick(() => {
UploadService.setWebviewContext(this.$refs.uploaderWebview, this)
})
return
} else {
// 2. 正常的文件上传回调
UploadService.handlerMessage(event)
}
}
},
initUploader() {
// 初始化上传服务 (移除 init 调用,交由 App.vue 统一管理)
// UploadService.init()
// 注入 Webview 实例
this.injectWebview()
},
injectWebview() {
// 恢复:通过 $refs 获取模板中声明的 Webview 实例
// 注意:需要等待 mounted 后一段时间确保 webview 已经渲染
this.$nextTick(() => {
UploadService.setWebviewContext(this.$refs.uploaderWebview, this)
})
},
handleTouchStart() {
uni.$emit('reset_timeout')
}
......
......@@ -47,6 +47,14 @@
<script src="js/OSSUpLoader.js"></script>
<script>
// document.addEventListener('UniAppJSBridgeReady', function(){
document.addEventListener('UniAppJSBridgeReady', function() {
// 主动告知宿主环境,Webview 已准备就绪
uni.postMessage({
data: {
action: 'webview_ready'
}
});
});
/**
* 该函数用于接收来自 UniApp 的消息,它包含了文件上传所需的所有信息
* @param {string} accessKeyId - OSS 访问密钥 ID
......
......@@ -2,7 +2,7 @@
* @Author: 前端 qianduan01@didihu.com.cn
* @Date: 2024-12-26 10:03:54
* @LastEditors: limin
* @LastEditTime: 2026-01-21 17:13:07
* @LastEditTime: 2026-01-21 19:05:50
* @FilePath: \simgle_app\mixin\record\recorder.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
......@@ -22,7 +22,6 @@ export default {
},
async mounted() {
this.recordService = RecordService.getInstance()
await this.recordService.init()
},
methods: {
...mapMutations('audio', ['setReceptionStatus']),
......@@ -127,9 +126,5 @@ export default {
async isPermission() {
return this.recordService.isPermission()
}
},
beforeDestroy() {
this.recordService?.destroy()
this.recordService = null
}
}
......@@ -14,7 +14,7 @@ class RecordService {
this._startAudioPromise = null
this.timeoutService = null
// 初始化
// this.init() // 不在构造函数中自动初始化,留给外部调用或 getInstance
this.init()
}
static getInstance() {
......
......@@ -4,8 +4,8 @@
* @Author: limin
* @Date: 2024-12-06 10:13:02
* @LastEditors: limin
* @LastEditTime: 2026-01-21 17:02:23
* @FilePath: \gl_tape\utils\recordManager\TimeoutService.js
* @LastEditTime: 2026-01-21 18:21:56
* @FilePath: \simgle_app\utils\recordManager\TimeoutService.js
*/
class TimeoutService {
TIMEOUT = 40 // 40分钟
......@@ -62,7 +62,6 @@ class TimeoutService {
//实现防抖
if (this._debounceTimer) clearTimeout(this._debounceTimer)
this._debounceTimer = setTimeout(() => {
console.log("🚀 ~ TimeoutService ~ handleTouchStart ~ this._debounceTimer:")
this._lastActiveTime = Date.now()
}, 1000)
}
......
......@@ -2,7 +2,7 @@
* @Author: 前端 qianduan01@didihu.com.cn
* @Date: 2024-04-08 17:16:46
* @LastEditors: limin
* @LastEditTime: 2026-01-21 15:02:24
* @LastEditTime: 2026-01-22 11:24:40
* @FilePath: \simgle_app\utils\utils.js
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
......@@ -173,36 +173,69 @@ export function serializeIfObject(value) {
}
/**
* 获取 Webview 实例的工具方法
* @param {Object} webviewComponent - Vue 组件引用
* @param {Object} vueContext - Vue 实例上下文
* 获取 Webview 实例 (增强版)
* @param {Object} component - Vue 组件引用
* @param {Object} context - Vue 上下文
*/
export function getWebviewInstance(component, context, retries = 3, interval = 300) {
export function getWebviewInstance(component, context) {
return new Promise((resolve) => {
const tryGet = (left) => {
try {
// 按照指定逻辑获取: 依赖父组件作用域获取原生 Webview,并取第一个子窗口
const currentWebview = context?.$parent?.$scope?.$getAppWebview?.()
const wv = currentWebview?.children?.()?.[0]
if (wv) {
resolve(wv)
return
}
} catch (e) {
console.warn('Webview获取异常', e)
// 1. 尝试直接获取
if (component && typeof component.$getAppWebview === 'function') {
const wv = component.$getAppWebview()
if (wv) {
resolve(wv)
return
}
if (left > 0) {
setTimeout(() => tryGet(left - 1), interval)
} else {
}
// 2. 尝试通过页面查找 (解决组件未挂载完全时的获取问题)
const tryFind = () => {
try {
let pageWebview = null
if (context && context.$scope && typeof context.$scope.$getAppWebview === 'function') {
pageWebview = context.$scope.$getAppWebview()
} else if (context && context.$parent && context.$parent.$scope && typeof context.$parent.$scope.$getAppWebview === 'function') {
pageWebview = context.$parent.$scope.$getAppWebview()
}
if (pageWebview && typeof pageWebview.children === 'function') {
const children = pageWebview.children()
for (let i = 0; i < children.length; i++) {
const child = children[i]
const url = child.getURL ? child.getURL() : ''
if (url && url.indexOf('hybrid/html/index.html') > -1) {
return child
}
}
}
} catch (e) {
console.warn('getWebviewInstance error:', e)
}
return null
}
const wv = tryFind()
if (wv) {
resolve(wv)
return
}
// 3. 异步重试 (处理渲染延迟)
let retries = 0
const timer = setInterval(() => {
retries++
const wv = tryFind()
if (wv) {
clearInterval(timer)
resolve(wv)
} else if (retries >= 10) {
clearInterval(timer)
resolve(null)
}
}
tryGet(retries)
}, 200)
})
}
export function isInteger(value) {
if (typeof value === 'number') {
return Number.isInteger(value)
......
......@@ -3,7 +3,7 @@ export default class RecordsWorker {
constructor() {}
async work(deviceId, data, ws) {
try {
const records = await uni.$dbRecord.getRecords()
const records = await uni.$dbRecord.getCompletedRecords()
const msg = `记录获取成功 records -> ${JSON.stringify(records)}`
uni.$logger.local.info(msg)
ws.send({ code: 200, msg: '获取记录成功', data:records })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!