修改数据库配置

This commit is contained in:
Yakumo Hokori
2026-02-25 22:58:50 +08:00
parent 1b36901a70
commit a89288630f
6 changed files with 327 additions and 36 deletions

View File

View File

@@ -156,15 +156,34 @@ pnpm tauri build
## 配置说明 ## 配置说明
### 数据库配置 ### 数据库配置
数据库连接配置位于 `src-tauri/src/lib.rs`
```rust 本应用使用 **PostgreSQL** 数据库进行数据持久化存储。数据库连接配置通过本地配置文件管理。
let db_url = "postgres://tmstools:521707@honulla.com:5432/tmstools";
#### 配置文件位置
所有平台统一使用 `~/.config/` 目录:
- **macOS**: `~/.config/tms_tools/config.toml`
- **Windows**: `~/.config/tms_tools/config.toml`
- **Linux**: `~/.config/tms_tools/config.toml`
#### 配置文件格式
在启动应用前,需要手动创建配置文件:
```toml
database_url = "postgres://用户名:密码@主机地址:端口/数据库名"
``` ```
### 外部 API **示例:**
- 登录接口: `https://c.baobaot.com/user/ajax_login` ```toml
- 工单平台: `https://c.baobaot.com/cinema/workorder` database_url = "postgres://tmstools:521707@localhost:5432/tmstools"
```
#### 启动检查
应用启动时会自动检查配置文件:
- ✅ 配置文件存在且格式正确 → 正常启动
- ❌ 配置文件不存在 → 弹出错误提示,点击确定后退出
- ❌ 数据库连接失败 → 在登录时提示错误
## 开发计划 ## 开发计划

282
src-tauri/Cargo.lock generated
View File

@@ -75,6 +75,24 @@ version = "1.0.98"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487" checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
[[package]]
name = "ashpd"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd884d7c72877a94102c3715f3b1cd09ff4fac28221add3e57cfbe25c236d093"
dependencies = [
"async-fs",
"async-net",
"enumflags2",
"futures-channel",
"futures-util",
"rand 0.8.5",
"serde",
"serde_repr",
"url",
"zbus 4.4.0",
]
[[package]] [[package]]
name = "async-broadcast" name = "async-broadcast"
version = "0.7.2" version = "0.7.2"
@@ -113,6 +131,17 @@ dependencies = [
"slab", "slab",
] ]
[[package]]
name = "async-fs"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8034a681df4aed8b8edbd7fbe472401ecf009251c8b40556b304567052e294c5"
dependencies = [
"async-lock",
"blocking",
"futures-lite",
]
[[package]] [[package]]
name = "async-io" name = "async-io"
version = "2.4.1" version = "2.4.1"
@@ -143,6 +172,17 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
] ]
[[package]]
name = "async-net"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b948000fad4873c1c9339d60f2623323a0cfd3816e5181033c6a5cb68b2accf7"
dependencies = [
"async-io",
"blocking",
"futures-lite",
]
[[package]] [[package]]
name = "async-process" name = "async-process"
version = "2.3.1" version = "2.3.1"
@@ -285,6 +325,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "block"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a"
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.10.4" version = "0.10.4"
@@ -2167,6 +2213,15 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4" checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
[[package]]
name = "malloc_buf"
version = "0.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62bb907fe88d54d8d9ce32a3cceab4218ed2f6b7d35617cafe9adf84e43919cb"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "markup5ever" name = "markup5ever"
version = "0.12.1" version = "0.12.1"
@@ -2338,6 +2393,19 @@ version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086"
[[package]]
name = "nix"
version = "0.29.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46"
dependencies = [
"bitflags 2.9.1",
"cfg-if",
"cfg_aliases",
"libc",
"memoffset",
]
[[package]] [[package]]
name = "nix" name = "nix"
version = "0.30.1" version = "0.30.1"
@@ -2394,6 +2462,26 @@ dependencies = [
"syn 2.0.104", "syn 2.0.104",
] ]
[[package]]
name = "objc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1"
dependencies = [
"malloc_buf",
]
[[package]]
name = "objc-foundation"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1add1b659e36c9607c7aab864a76c7a4c2760cd0cd2e120f3fb8b952c7e22bf9"
dependencies = [
"block",
"objc",
"objc_id",
]
[[package]] [[package]]
name = "objc-sys" name = "objc-sys"
version = "0.3.5" version = "0.3.5"
@@ -2608,6 +2696,15 @@ dependencies = [
"objc2-foundation 0.3.1", "objc2-foundation 0.3.1",
] ]
[[package]]
name = "objc_id"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b"
dependencies = [
"objc",
]
[[package]] [[package]]
name = "object" name = "object"
version = "0.36.7" version = "0.36.7"
@@ -2975,6 +3072,12 @@ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.59.0",
] ]
[[package]]
name = "pollster"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22686f4785f02a4fcc856d3b3bb19bf6c8160d103f7a99cc258bddd0251dc7f2"
[[package]] [[package]]
name = "postgres-protocol" name = "postgres-protocol"
version = "0.6.8" version = "0.6.8"
@@ -3120,9 +3223,9 @@ dependencies = [
[[package]] [[package]]
name = "quick-xml" name = "quick-xml"
version = "0.38.0" version = "0.38.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8927b0664f5c5a98265138b7e3f90aa19a6b21353182469ace36d4ac527b7b1b" checksum = "b66c2058c55a409d601666cffe35f04333cf1013010882cec174a7467cd4e21c"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
@@ -3383,6 +3486,29 @@ dependencies = [
"web-sys", "web-sys",
] ]
[[package]]
name = "rfd"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a73a7337fc24366edfca76ec521f51877b114e42dab584008209cca6719251"
dependencies = [
"ashpd",
"block",
"dispatch",
"js-sys",
"log",
"objc",
"objc-foundation",
"objc_id",
"pollster",
"raw-window-handle",
"urlencoding",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "ring" name = "ring"
version = "0.17.14" version = "0.17.14"
@@ -3798,6 +3924,17 @@ dependencies = [
"stable_deref_trait", "stable_deref_trait",
] ]
[[package]]
name = "sha1"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.10.9" version = "0.10.9"
@@ -4264,7 +4401,7 @@ dependencies = [
"thiserror 2.0.12", "thiserror 2.0.12",
"url", "url",
"windows", "windows",
"zbus", "zbus 5.8.0",
] ]
[[package]] [[package]]
@@ -4505,6 +4642,7 @@ dependencies = [
"dirs 5.0.1", "dirs 5.0.1",
"lazy_static", "lazy_static",
"reqwest", "reqwest",
"rfd",
"scraper", "scraper",
"serde", "serde",
"serde_json", "serde_json",
@@ -4934,6 +5072,12 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "urlencoding"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
[[package]] [[package]]
name = "urlpattern" name = "urlpattern"
version = "0.3.0" version = "0.3.0"
@@ -5811,6 +5955,16 @@ dependencies = [
"pkg-config", "pkg-config",
] ]
[[package]]
name = "xdg-home"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec1cdab258fb55c0da61328dc52c8764709b249011b2cad0454c72f0bf10a1f6"
dependencies = [
"libc",
"windows-sys 0.59.0",
]
[[package]] [[package]]
name = "yoke" name = "yoke"
version = "0.8.0" version = "0.8.0"
@@ -5835,6 +5989,44 @@ dependencies = [
"synstructure", "synstructure",
] ]
[[package]]
name = "zbus"
version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb97012beadd29e654708a0fdb4c84bc046f537aecfde2c3ee0a9e4b4d48c725"
dependencies = [
"async-broadcast",
"async-executor",
"async-fs",
"async-io",
"async-lock",
"async-process",
"async-recursion",
"async-task",
"async-trait",
"blocking",
"enumflags2",
"event-listener",
"futures-core",
"futures-sink",
"futures-util",
"hex",
"nix 0.29.0",
"ordered-stream",
"rand 0.8.5",
"serde",
"serde_repr",
"sha1",
"static_assertions",
"tracing",
"uds_windows",
"windows-sys 0.52.0",
"xdg-home",
"zbus_macros 4.4.0",
"zbus_names 3.0.0",
"zvariant 4.2.0",
]
[[package]] [[package]]
name = "zbus" name = "zbus"
version = "5.8.0" version = "5.8.0"
@@ -5855,7 +6047,7 @@ dependencies = [
"futures-core", "futures-core",
"futures-lite", "futures-lite",
"hex", "hex",
"nix", "nix 0.30.1",
"ordered-stream", "ordered-stream",
"serde", "serde",
"serde_repr", "serde_repr",
@@ -5863,9 +6055,22 @@ dependencies = [
"uds_windows", "uds_windows",
"windows-sys 0.59.0", "windows-sys 0.59.0",
"winnow 0.7.12", "winnow 0.7.12",
"zbus_macros", "zbus_macros 5.8.0",
"zbus_names", "zbus_names 4.2.0",
"zvariant", "zvariant 5.6.0",
]
[[package]]
name = "zbus_macros"
version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "267db9407081e90bbfa46d841d3cbc60f59c0351838c4bc65199ecd79ab1983e"
dependencies = [
"proc-macro-crate 3.3.0",
"proc-macro2",
"quote",
"syn 2.0.104",
"zvariant_utils 2.1.0",
] ]
[[package]] [[package]]
@@ -5878,9 +6083,20 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.104", "syn 2.0.104",
"zbus_names", "zbus_names 4.2.0",
"zvariant", "zvariant 5.6.0",
"zvariant_utils", "zvariant_utils 3.2.0",
]
[[package]]
name = "zbus_names"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b9b1fef7d021261cc16cba64c351d291b715febe0fa10dc3a443ac5a5022e6c"
dependencies = [
"serde",
"static_assertions",
"zvariant 4.2.0",
] ]
[[package]] [[package]]
@@ -5892,7 +6108,7 @@ dependencies = [
"serde", "serde",
"static_assertions", "static_assertions",
"winnow 0.7.12", "winnow 0.7.12",
"zvariant", "zvariant 5.6.0",
] ]
[[package]] [[package]]
@@ -5975,6 +6191,20 @@ dependencies = [
"syn 2.0.104", "syn 2.0.104",
] ]
[[package]]
name = "zvariant"
version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2084290ab9a1c471c38fc524945837734fbf124487e105daec2bb57fd48c81fe"
dependencies = [
"endi",
"enumflags2",
"serde",
"static_assertions",
"url",
"zvariant_derive 4.2.0",
]
[[package]] [[package]]
name = "zvariant" name = "zvariant"
version = "5.6.0" version = "5.6.0"
@@ -5985,8 +6215,21 @@ dependencies = [
"enumflags2", "enumflags2",
"serde", "serde",
"winnow 0.7.12", "winnow 0.7.12",
"zvariant_derive", "zvariant_derive 5.6.0",
"zvariant_utils", "zvariant_utils 3.2.0",
]
[[package]]
name = "zvariant_derive"
version = "4.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73e2ba546bda683a90652bac4a279bc146adad1386f25379cf73200d2002c449"
dependencies = [
"proc-macro-crate 3.3.0",
"proc-macro2",
"quote",
"syn 2.0.104",
"zvariant_utils 2.1.0",
] ]
[[package]] [[package]]
@@ -5999,7 +6242,18 @@ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn 2.0.104", "syn 2.0.104",
"zvariant_utils", "zvariant_utils 3.2.0",
]
[[package]]
name = "zvariant_utils"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c51bcff7cc3dbb5055396bcf774748c3dab426b4b8659046963523cee4808340"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.104",
] ]
[[package]] [[package]]

View File

@@ -31,4 +31,5 @@ scraper = "0.19.0"
tms_service = { path = "./tms_service" } tms_service = { path = "./tms_service" }
dirs = "5" dirs = "5"
toml = "0.8" toml = "0.8"
rfd = "0.14"

View File

@@ -7,31 +7,27 @@ pub struct Config {
pub database_url: String, pub database_url: String,
} }
impl Default for Config {
fn default() -> Self {
Config {
database_url: "postgres://tmstools:521707@honulla.com:5432/tmstools".to_string(),
}
}
}
impl Config { impl Config {
/// 获取配置文件路径:~/.config/tms_tools/config.toml /// 获取配置文件路径:~/.config/tms_tools/config.toml
/// 统一使用 ~/.config 目录,跨平台一致
pub fn config_path() -> Result<PathBuf, String> { pub fn config_path() -> Result<PathBuf, String> {
let config_dir = dirs::config_dir() let home_dir = dirs::home_dir()
.ok_or_else(|| "无法获取用户配置目录".to_string())?; .ok_or_else(|| "无法获取用户目录".to_string())?;
let config_dir = home_dir.join(".config");
let app_config_dir = config_dir.join("tms_tools"); let app_config_dir = config_dir.join("tms_tools");
let config_file = app_config_dir.join("config.toml"); let config_file = app_config_dir.join("config.toml");
Ok(config_file) Ok(config_file)
} }
/// 从配置文件加载配置,如果不存在则返回默认配置 /// 从配置文件加载配置,如果不存在或解析失败则返回错误
pub fn load() -> Result<Self, String> { pub fn load() -> Result<Self, String> {
let config_path = Self::config_path()?; let config_path = Self::config_path()?;
if !config_path.exists() { if !config_path.exists() {
// 配置文件不存在,返回默认配置 return Err(format!(
return Ok(Config::default()); "配置文件不存在: {}\n请创建配置文件,例如:\n\n[config]\ndatabase_url = \"postgres://username:password@host:port/database\"",
config_path.display()
));
} }
let content = fs::read_to_string(&config_path) let content = fs::read_to_string(&config_path)
@@ -40,6 +36,10 @@ impl Config {
let config: Config = toml::from_str(&content) let config: Config = toml::from_str(&content)
.map_err(|e| format!("解析配置文件失败: {}", e))?; .map_err(|e| format!("解析配置文件失败: {}", e))?;
if config.database_url.is_empty() {
return Err("配置文件中的 database_url 不能为空".to_string());
}
Ok(config) Ok(config)
} }
@@ -77,8 +77,8 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_default_config() { fn test_config_path() {
let config = Config::default(); let path = Config::config_path();
assert!(!config.database_url.is_empty()); assert!(path.is_ok());
} }
} }

View File

@@ -583,6 +583,23 @@ async fn feedback_and_close_today_workorders() -> Result<String, String> {
#[cfg_attr(mobile, tauri::mobile_entry_point)] #[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() { pub fn run() {
// 首先验证配置
if let Err(e) = config::Config::load() {
// 配置加载失败,显示错误弹窗并退出
eprintln!("配置错误: {}", e);
// 使用 rfd 显示错误弹窗(阻塞模式)
rfd::MessageDialog::new()
.set_title("配置错误")
.set_description(&format!("{}\n\n软件将关闭。", e))
.set_buttons(rfd::MessageButtons::Ok)
.set_level(rfd::MessageLevel::Error)
.show();
// 用户点击确定后退出应用
std::process::exit(1);
}
tauri::Builder::default() tauri::Builder::default()
.plugin(tauri_plugin_opener::init()) .plugin(tauri_plugin_opener::init())
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![