refactor(balance): 重构余额监控存储架构 #57
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
动机
余额监控页面当前使用 localStorage 存储配置,存在以下问题:
本 PR 将存储层重构为基于 JSON 文件的后端管理架构,解决上述问题。
主要改动点
1. 后端存储服务
新增模块:
models/balance.rs:数据模型定义(BalanceConfig、BalanceStore)services/balance/manager.rs:BalanceManager服务,提供 CRUD 接口~/.duckcoding/balance_configs.jsonDataManager统一管理,支持原子写入和 Unix 权限控制(0o600)数据结构:
2. 新增 Tauri 命令
配置管理命令(位于
commands/balance_commands.rs):get_all_balance_configs(): 获取所有配置save_balance_config(config): 保存单个配置(创建或更新)delete_balance_config(id): 删除配置get_balance_config(id): 获取单个配置3. 自动迁移系统
迁移逻辑(
migrations/balance_localstorage_to_json.rs):4. 前端集成改造
Hook 重构:
useBalanceConfigs:切换到新的 Tauri 命令(get_all_balance_configs等)useApiKeys:支持从配置加载持久化的 API Key,内存保存模式不变UI 增强:
ui/checkbox.tsx,基于 shadcn/ui)ConfigFormDialog添加"记住 API Key"选项类型更新:
BalanceConfig类型新增saveApiKey和apiKey字段技术细节
存储优势
数据安全:
性能提升:
多端同步:
向后兼容
API Key 管理策略
两种模式:
持久化模式(
save_api_key: true):内存模式(
save_api_key: false,默认):测试情况
后端测试
前端测试
集成测试
风险评估
低风险
需要注意
安全建议
受影响模块
models/balance.rs、services/balance/、commands/balance_commands.rsBalancePage/、lib/tauri-commands.ts、components/ui/checkbox.tsxmigration_manager/migrations/balance_localstorage_to_json.rs截图/演示
(本地验证通过,如需截图可补充)
检查清单
npm run check且全部通过cargo test --locked)后续计划