Repository navigation
Expand file tree
/
Copy pathcardConfigService.ts
More file actions
91 lines (85 loc) · 2.39 KB
/
Copy pathcardConfigService.ts
File metadata and controls
91 lines (85 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { invoke } from "@tauri-apps/api/core";
import { logDebug, logError } from "@/utils/logger";
import type { BaseCardSettings } from "@/types/card-settings";
/**
* 统一的卡片配置管理服务
*
* 所有卡片的配置都通过此服务进行读写,配置存储在 app_config.json 的 card_settings 对象下
*/
export class CardConfigService {
/**
* 获取卡片配置
* @param cardId 卡片实例ID
* @returns 卡片配置对象
*/
static async getCardSettings<T extends BaseCardSettings = BaseCardSettings>(
cardId: string
): Promise<T> {
try {
const config = await invoke<any>("get_card_settings", { cardId });
return (config || {}) as T;
} catch (error) {
logError(`Failed to get settings for card ${cardId}:`, error);
return {} as T;
}
}
/**
* 保存卡片配置
* @param cardId 卡片实例ID
* @param settings 配置对象(会与现有配置合并)
*/
static async saveCardSettings(
cardId: string,
settings: Partial<BaseCardSettings>
): Promise<void> {
try {
await invoke("save_card_settings", {
cardId,
settings,
});
logDebug(`Saved settings for card ${cardId}:`, settings);
} catch (error) {
logError(`Failed to save settings for card ${cardId}:`, error);
throw error;
}
}
/**
* 更新卡片配置的特定字段
* @param cardId 卡片实例ID
* @param key 配置键
* @param value 配置值
*/
static async updateCardSetting(
cardId: string,
key: string,
value: any
): Promise<void> {
try {
// 先获取现有配置
const currentSettings = await this.getCardSettings(cardId);
// 更新指定字段
const updatedSettings = {
...currentSettings,
[key]: value,
};
// 保存更新后的配置
await this.saveCardSettings(cardId, updatedSettings);
} catch (error) {
logError(`Failed to update setting ${key} for card ${cardId}:`, error);
throw error;
}
}
/**
* 删除卡片配置
* @param cardId 卡片实例ID
*/
static async removeCardSettings(cardId: string): Promise<void> {
try {
await invoke("remove_card_settings", { cardId });
logDebug(`Removed settings for card ${cardId}`);
} catch (error) {
logError(`Failed to remove settings for card ${cardId}:`, error);
throw error;
}
}
}