feat: rebuild portable auto-update flow

This commit is contained in:
Misaka
2026-03-20 21:20:28 +08:00
parent 5e50a8fbcf
commit 6ad9463e73
29 changed files with 2333 additions and 13 deletions

View File

@@ -18,6 +18,7 @@ import type { UserInfo } from '../types/user.types'
import { IPC_CHANNELS } from '../../shared/ipc-channels'
import { ValidationError } from '../types/errors'
import { withErrorHandling, type IpcResult } from './index'
import { UpdateService } from '../services/update/update-service'
const log = createLogger('AuthHandler')
@@ -70,6 +71,7 @@ export interface CurrentUserResponse {
*/
export function registerAuthHandlers(): void {
const sessionManager = SessionManager.getInstance()
const updateService = UpdateService.getInstance()
/**
* Get computer name
@@ -93,6 +95,8 @@ export function registerAuthHandlers(): void {
const userInfo = sessionManager.getUserInfo()
if (success && userInfo) {
await updateService.setUserContext(userInfo.userType)
// Check if admin needs user selection
const requiresUserSelection = userInfo.userType === 'Admin'
@@ -119,6 +123,7 @@ export function registerAuthHandlers(): void {
}
}
await updateService.setUserContext(null)
throw new ValidationError('无感登录失败:未找到匹配用户', 'VAL_INVALID_INPUT')
}, 'auth:silentLogin')
}
@@ -144,6 +149,7 @@ export function registerAuthHandlers(): void {
if (success && userInfo) {
log.info('Login successful', { username, userType: userInfo.userType })
await updateService.setUserContext(userInfo.userType)
// Audit log: LOGIN success (non-blocking)
const os = await import('os')
@@ -172,6 +178,7 @@ export function registerAuthHandlers(): void {
}).catch((err) => log.warn('Failed to write audit log', { err }))
log.warn('Login failed - invalid credentials', { username })
await updateService.setUserContext(null)
throw new ValidationError('用户名或密码错误', 'VAL_INVALID_INPUT')
}, 'auth:login')
}
@@ -198,6 +205,7 @@ export function registerAuthHandlers(): void {
}
sessionManager.logout()
await updateService.setUserContext(null)
}, 'auth:logout')
})
@@ -241,6 +249,7 @@ export function registerAuthHandlers(): void {
if (success) {
const newUser = sessionManager.getUserInfo()
log.info('User switch successful', { newUsername: newUser?.username })
await updateService.setUserContext(newUser?.userType ?? null)
return {
success: true,
userInfo: newUser ?? undefined

View File

@@ -15,6 +15,7 @@ import { registerMaterialTypeHandlers } from './material-type-handler'
import { registerUserErpConfigHandlers } from './user-erp-config-handler'
import { registerLoggerHandlers } from './logger-handler'
import { registerReportHandlers } from './report-handler'
import { registerUpdateHandlers } from './update-handler'
import { createLogger, logError } from '../services/logger'
import { serializeError, sanitizeError } from '../services/logger/error-utils'
import { getErrorMessage, getErrorCode, isBaseError } from '../types/errors'
@@ -105,5 +106,6 @@ export function registerIpcHandlers(): void {
registerUserErpConfigHandlers()
registerLoggerHandlers()
registerReportHandlers()
registerUpdateHandlers()
log.info('All IPC handlers registered')
}

View File

@@ -0,0 +1,55 @@
import { ipcMain } from 'electron'
import { IPC_CHANNELS } from '../../shared/ipc-channels'
import { withErrorHandling, type IpcResult } from './index'
import { UpdateService } from '../services/update/update-service'
import type {
DownloadReleaseRequest,
UpdateDialogCatalog,
UpdateStatus
} from '../types/update.types'
export function registerUpdateHandlers(): void {
const updateService = UpdateService.getInstance()
ipcMain.handle(IPC_CHANNELS.UPDATE_GET_STATUS, async (): Promise<IpcResult<UpdateStatus>> => {
return withErrorHandling(async () => updateService.getStatus(), 'update:getStatus')
})
ipcMain.handle(IPC_CHANNELS.UPDATE_CHECK_NOW, async (): Promise<IpcResult<UpdateStatus>> => {
return withErrorHandling(async () => updateService.checkForUpdates(), 'update:checkNow')
})
ipcMain.handle(
IPC_CHANNELS.UPDATE_GET_CATALOG,
async (): Promise<IpcResult<UpdateDialogCatalog>> => {
return withErrorHandling(async () => updateService.getCatalog(), 'update:getCatalog')
}
)
ipcMain.handle(
IPC_CHANNELS.UPDATE_GET_CHANGELOG,
async (_event, request: DownloadReleaseRequest): Promise<IpcResult<string>> => {
return withErrorHandling(
async () => updateService.getChangelog(request),
'update:getChangelog'
)
}
)
ipcMain.handle(
IPC_CHANNELS.UPDATE_DOWNLOAD_RELEASE,
async (_event, request: DownloadReleaseRequest): Promise<IpcResult<UpdateStatus>> => {
return withErrorHandling(
async () => updateService.downloadRelease(request),
'update:downloadRelease'
)
}
)
ipcMain.handle(IPC_CHANNELS.UPDATE_INSTALL_DOWNLOADED, async (): Promise<IpcResult<void>> => {
return withErrorHandling(
async () => updateService.installDownloadedRelease(),
'update:installDownloaded'
)
})
}