feat: 重构EGM计算核心并添加实时日志推送功能
将EGM计算逻辑从webview_app.py移到main.py中的run_egm函数 添加实时日志推送和计算结果回调机制 支持后台线程计算不阻塞前端
This commit is contained in:
@@ -317,7 +317,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, computed } from 'vue'
|
||||
import { ref, reactive, computed, onMounted, onUnmounted } from 'vue'
|
||||
import type { AllParameters } from '@/types'
|
||||
import LogComponent from './Log.vue'
|
||||
|
||||
@@ -411,17 +411,10 @@ const calculate = async () => {
|
||||
try {
|
||||
// 调用 pywebview 的 Python 函数
|
||||
if (window.pywebview) {
|
||||
const response = await window.pywebview.api.calculate(params)
|
||||
|
||||
// 显示后端日志
|
||||
const logs = response.logs
|
||||
if (Array.isArray(logs) && logs.length > 0) {
|
||||
for (const log of logs) {
|
||||
logRef.value?.addLog(log.level as any, log.message)
|
||||
}
|
||||
}
|
||||
|
||||
result.value = JSON.stringify(response, null, 2)
|
||||
// 后台线程启动计算,实时日志通过 addLogFromBackend 推送
|
||||
// 结果通过 receiveResult 回调接收
|
||||
await window.pywebview.api.calculate(params)
|
||||
// 不在这里设置 calculating = false,等待 receiveResult 回调
|
||||
} else {
|
||||
// 开发模式下的模拟
|
||||
await new Promise(resolve => setTimeout(resolve, 1000))
|
||||
@@ -436,11 +429,11 @@ const calculate = async () => {
|
||||
parameters: params
|
||||
}
|
||||
}, null, 2)
|
||||
calculating.value = false
|
||||
}
|
||||
} catch (e: any) {
|
||||
error.value = e.message || '计算失败'
|
||||
logRef.value?.addLog('error', e.message || '计算失败')
|
||||
} finally {
|
||||
calculating.value = false
|
||||
}
|
||||
}
|
||||
@@ -514,8 +507,33 @@ declare global {
|
||||
export_config: (params: AllParameters) => Promise<any>
|
||||
}
|
||||
}
|
||||
addLogFromBackend?: (log: { level: string; time: string; message: string }) => void
|
||||
receiveResult?: (result: { success: boolean; message: string; data?: any; error?: string }) => void
|
||||
}
|
||||
}
|
||||
|
||||
// 注册全局日志接收函数,供后端实时调用
|
||||
onMounted(() => {
|
||||
// 实时日志推送
|
||||
window.addLogFromBackend = (log: { level: string; time: string; message: string }) => {
|
||||
logRef.value?.addLog(log.level as any, log.message)
|
||||
}
|
||||
|
||||
// 接收计算结果
|
||||
window.receiveResult = (res: { success: boolean; message: string; data?: any; error?: string }) => {
|
||||
calculating.value = false
|
||||
if (res.success) {
|
||||
result.value = JSON.stringify(res, null, 2)
|
||||
} else {
|
||||
error.value = res.error || res.message
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
window.addLogFromBackend = undefined
|
||||
window.receiveResult = undefined
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
|
||||
Reference in New Issue
Block a user