修复 OOBE 建表异常并提升遮罩层内错误提示层级

This commit is contained in:
JSR
2026-03-18 18:00:17 +08:00
parent 8729b5a294
commit f90687b983
3 changed files with 64 additions and 11 deletions
+26 -1
View File
@@ -4,6 +4,8 @@ use std::sync::Arc;
use tauri::State; use tauri::State;
use crate::db::connection::{create_postgres_connection, create_sqlite_connection}; use crate::db::connection::{create_postgres_connection, create_sqlite_connection};
use crate::db::connection::DatabaseType;
use crate::db::migration::run_migration;
use crate::services::permission::PermissionLevel; use crate::services::permission::PermissionLevel;
use crate::state::AppState; use crate::state::AppState;
@@ -119,6 +121,9 @@ pub async fn db_switch_connection(
let conn = create_postgres_connection(&connection_string) let conn = create_postgres_connection(&connection_string)
.await .await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
run_migration(&conn, DatabaseType::PostgreSQL)
.await
.map_err(|e| e.to_string())?;
let state_guard = state.read(); let state_guard = state.read();
let mut db_guard = state_guard.db.write(); let mut db_guard = state_guard.db.write();
@@ -138,6 +143,9 @@ pub async fn db_switch_connection(
let conn = create_sqlite_connection(&path) let conn = create_sqlite_connection(&path)
.await .await
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
run_migration(&conn, DatabaseType::SQLite)
.await
.map_err(|e| e.to_string())?;
let state_guard = state.read(); let state_guard = state.read();
let mut db_guard = state_guard.db.write(); let mut db_guard = state_guard.db.write();
@@ -189,7 +197,24 @@ pub async fn db_sync(
let state_guard = state.read(); let state_guard = state.read();
let db_guard = state_guard.db.read(); let db_guard = state_guard.db.read();
if let Some(conn) = db_guard.as_ref() { if let Some(conn) = db_guard.as_ref() {
match conn.ping().await { let settings = state_guard.settings.read();
let status_json = settings.get_value(crate::services::settings::SettingsKey::PgConnectionStatus);
let db_type = match status_json {
crate::services::settings::SettingsValue::Json(json) => json
.get("type")
.and_then(|t| t.as_str())
.map(|s| s.to_string())
.unwrap_or_else(|| "sqlite".to_string()),
_ => "sqlite".to_string(),
};
let migration_result = if db_type == "postgresql" {
run_migration(conn, DatabaseType::PostgreSQL).await
} else {
run_migration(conn, DatabaseType::SQLite).await
};
match migration_result {
Ok(_) => Ok(IpcResponse::success(SyncResult { Ok(_) => Ok(IpcResponse::success(SyncResult {
success: true, success: true,
message: Some("Database synchronized successfully".to_string()), message: Some("Database synchronized successfully".to_string()),
+6
View File
@@ -6,6 +6,8 @@ pub mod state;
pub mod utils; pub mod utils;
use crate::db::connection::create_sqlite_connection; use crate::db::connection::create_sqlite_connection;
use crate::db::connection::DatabaseType;
use crate::db::migration::run_migration;
use tauri::{ use tauri::{
image::Image, image::Image,
menu::{Menu, MenuItem}, menu::{Menu, MenuItem},
@@ -51,6 +53,10 @@ fn setup_database(app: &mut App) -> Result<(), Box<dyn std::error::Error>> {
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
match create_sqlite_connection(&db_path_str).await { match create_sqlite_connection(&db_path_str).await {
Ok(conn) => { Ok(conn) => {
if let Err(e) = run_migration(&conn, DatabaseType::SQLite).await {
eprintln!("Failed to run sqlite migration: {}", e);
return;
}
let state = handle.state::<crate::state::SafeAppState>(); let state = handle.state::<crate::state::SafeAppState>();
let state_guard = state.write(); let state_guard = state.write();
let mut db_guard = state_guard.db.write(); let mut db_guard = state_guard.db.write();
+32 -10
View File
@@ -98,6 +98,20 @@ export const OOBE: React.FC<oobeProps> = ({ visible, onComplete }) => {
const primaryColor = workingTheme?.config?.tdesign?.brandColor || "#1677FF" const primaryColor = workingTheme?.config?.tdesign?.brandColor || "#1677FF"
const isDark = workingTheme?.mode === "dark" const isDark = workingTheme?.mode === "dark"
const showOobeMessage = (
type: "success" | "error" | "warning" | "info",
content: string
) => {
messageApi.open({
type,
content,
style: {
marginTop: 8,
zIndex: 12000,
},
})
}
useEffect(() => { useEffect(() => {
if (!currentTheme) return if (!currentTheme) return
const base = deepClone(currentTheme) const base = deepClone(currentTheme)
@@ -186,7 +200,7 @@ export const OOBE: React.FC<oobeProps> = ({ visible, onComplete }) => {
}) })
if (newStudents.length === 0) { if (newStudents.length === 0) {
messageApi.warning(t("oobe.steps.students.studentExists")) showOobeMessage("warning", t("oobe.steps.students.studentExists"))
return return
} }
@@ -237,14 +251,15 @@ export const OOBE: React.FC<oobeProps> = ({ visible, onComplete }) => {
.map((name) => ({ name: name.trim() })) .map((name) => ({ name: name.trim() }))
if (newStudents.length > 0) { if (newStudents.length > 0) {
setStudents([...students, ...newStudents]) setStudents([...students, ...newStudents])
messageApi.success( showOobeMessage(
"success",
t("oobe.steps.students.importSuccess", { count: newStudents.length }) t("oobe.steps.students.importSuccess", { count: newStudents.length })
) )
} else { } else {
messageApi.info(t("oobe.steps.students.noNewStudents")) showOobeMessage("info", t("oobe.steps.students.noNewStudents"))
} }
} catch { } catch {
messageApi.error(t("oobe.steps.students.parseFailed")) showOobeMessage("error", t("oobe.steps.students.parseFailed"))
} }
} else if (ext === "xlsx") { } else if (ext === "xlsx") {
try { try {
@@ -266,17 +281,18 @@ export const OOBE: React.FC<oobeProps> = ({ visible, onComplete }) => {
.map((name) => ({ name })) .map((name) => ({ name }))
if (newStudents.length > 0) { if (newStudents.length > 0) {
setStudents([...students, ...newStudents]) setStudents([...students, ...newStudents])
messageApi.success( showOobeMessage(
"success",
t("oobe.steps.students.importSuccess", { count: newStudents.length }) t("oobe.steps.students.importSuccess", { count: newStudents.length })
) )
} else { } else {
messageApi.info(t("oobe.steps.students.noNewStudents")) showOobeMessage("info", t("oobe.steps.students.noNewStudents"))
} }
} catch { } catch {
messageApi.error(t("oobe.steps.students.parseFailed")) showOobeMessage("error", t("oobe.steps.students.parseFailed"))
} }
} else { } else {
messageApi.error(t("oobe.steps.students.unsupportedFormat")) showOobeMessage("error", t("oobe.steps.students.unsupportedFormat"))
} }
}, },
[students, messageApi] [students, messageApi]
@@ -319,6 +335,12 @@ export const OOBE: React.FC<oobeProps> = ({ visible, onComplete }) => {
} }
} }
const syncRes = await (window as any).api.dbSync()
ensureSuccess(syncRes, t("common.error"))
if (!syncRes?.data?.success) {
throw new Error(syncRes?.data?.message || t("common.error"))
}
if (workingTheme) { if (workingTheme) {
const exists = themes.some((t) => t.id === workingTheme.id) const exists = themes.some((t) => t.id === workingTheme.id)
if (!exists) { if (!exists) {
@@ -355,10 +377,10 @@ export const OOBE: React.FC<oobeProps> = ({ visible, onComplete }) => {
const res = await (window as any).api.setSetting("is_wizard_completed", true) const res = await (window as any).api.setSetting("is_wizard_completed", true)
ensureSuccess(res, "failed") ensureSuccess(res, "failed")
messageApi.success(t("common.success")) showOobeMessage("success", t("common.success"))
onComplete() onComplete()
} catch (e: any) { } catch (e: any) {
messageApi.error(e?.message || t("common.error")) showOobeMessage("error", e?.message || t("common.error"))
} finally { } finally {
setLoading(false) setLoading(false)
} }