feat(desktop): 优化一些逻辑

1. 优化通知配置

2. 优化命名规范

3. 优化代码逻辑
This commit is contained in:
2026-01-10 23:37:28 +08:00
parent bce411af7e
commit 36cf521851
30 changed files with 674 additions and 575 deletions

View File

@@ -4,9 +4,16 @@ import { readingReflectionGraph } from '@main/services/ai/graph/readingReflectio
import { AppDataSource } from '@main/db/data-source'
import { ReadingReflectionTaskBatch } from '@main/db/entities/ReadingReflectionTaskBatch'
import { ReadingReflectionTaskItem } from '@main/db/entities/ReadingReflectionTaskItem'
import Store from 'electron-store'
import { CONFIG_STORE_KEY } from '@rpc/constants/store_key'
import { Notification } from 'electron'
export const readingReflectionTaskEvent = new EventEmitter()
// 兼容性处理获取 Store 构造函数
const StoreClass = (Store as any).default || Store
const store = new StoreClass({ encryptionKey: CONFIG_STORE_KEY })
class TaskManager {
private limit = pLimit(2)
private batchRepo = AppDataSource.getRepository(ReadingReflectionTaskBatch)
@@ -113,6 +120,7 @@ class TaskManager {
result?: any
) {
const displayId = total === 1 ? taskId : `${taskId}-${index}`
//发送 tRPC 实时事件(驱动前端 UI 进度条)
readingReflectionTaskEvent.emit('readingReflectionTaskProgress', {
taskId: displayId,
progress,
@@ -120,6 +128,44 @@ class TaskManager {
statusText: `[任务${index + 1}/${total}] ${status}`, // 传描述文字
result
})
// 2. 添加任务状态通知判断
this.handleNotification(status, progress, total, index)
}
/**
* 内部私有方法:处理通知逻辑
*/
private handleNotification(status: string, progress: number, total: number, index: number) {
// 从 electron-store 获取用户偏好
const config = store.get('notification') || {
masterSwitch: true,
taskCompleted: true,
taskFailed: true
}
// 如果总开关关闭,直接拦截
if (!config.masterSwitch) return
// 场景 A: 任务全部完成 (100%)
if (progress === 100 && config.taskCompleted) {
// 只有当所有子任务都完成,或者当前是单任务时才弹出
// 如果是批量任务,你可以选择在最后一个子任务完成时通知
if (index + 1 === total) {
new Notification({
title: '🎉 读书心得已生成',
body: total > 1 ? `${total} 篇心得已全部处理完成。` : '您的书籍心得已准备就绪。',
silent: config.silentMode
}).show()
}
}
// 场景 B: 任务失败 (假设你传入的 status 是 'FAILED')
if (status === 'FAILED' && config.taskFailed) {
new Notification({
title: '❌ 任务生成失败',
body: `${index + 1} 项任务执行异常,请检查网络或 API 余额。`,
silent: config.silentMode
}).show()
}
}
}