wangzhibo
2026-10-08 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//功能配置工具
import Api from "@/apis/api";
import store from "@/store";
 
class FeatureManager {
    constructor() {
        this.defaultFeatures = {
            voiceprintRecognition: {
                name: 'feature.voiceprintRecognition.name',
                enabled: false,
                description: 'feature.voiceprintRecognition.description'
            },
            voiceClone: {
                name: 'feature.voiceClone.name',
                enabled: false,
                description: 'feature.voiceClone.description'
            },
            knowledgeBase: {
                name: 'feature.knowledgeBase.name',
                enabled: false,
                description: 'feature.knowledgeBase.description'
            },
            mcpAccessPoint: {
                name: 'feature.mcpAccessPoint.name',
                enabled: false,
                description: 'feature.mcpAccessPoint.description'
            },
            vad: {
                name: 'feature.vad.name',
                enabled: false,
                description: 'feature.vad.description'
            },
            asr: {
                name: 'feature.asr.name',
                enabled: false,
                description: 'feature.asr.description'
            },
            addressBook: {
                name: 'feature.addressBook.name',
                enabled: false,
                description: 'feature.addressBook.description'
            }
        };
        this.currentFeatures = { ...this.defaultFeatures }; // 当前内存中的配置
        this.initialized = false;
        this.initPromise = null;
    }
 
    /**
     * 等待初始化完成
     */
    async waitForInitialization() {
        if (!this.initPromise) {
            this.initPromise = this.init();
        }
        await this.initPromise;
        return this.initialized;
    }
 
    /**
     * 初始化功能配置
     */
    async init() {
        try {
            // 从pub-config接口获取配置
            const config = await this.getConfigFromPubConfig();
            if (config) {
                this.currentFeatures = { ...config }; // 保存到内存
                this.initialized = true;
                return;
            }
        } catch (error) {
            console.warn('从pub-config接口获取配置失败:', error);
        }
 
        // pub-config接口失败,使用默认配置
        this.currentFeatures = { ...this.defaultFeatures }; // 保存默认配置到内存
        this.initialized = true;
    }
 
    /**
     * 更新config缓存
     */
    updateConfigCache(config) {
        store.commit('setPubConfig', config);
        localStorage.setItem('pubConfig', JSON.stringify(config));
    }
 
    /**
     * 从pub-config接口获取配置
     */
    async getConfigFromPubConfig() {
        return new Promise((resolve) => {
            // 直接调用pub-config接口获取配置
            Api.user.getPubConfig((result) => {
                // 检查返回结果的结构
                if (result && result.status === 200) {
                    // 检查是否有data字段
                    if (result.data) {
                        const configCache = result.data.data || {};
                        // 检查是否有code字段,如果有则按照code判断
                        if (result.data.code !== undefined) {
                            if (result.data.code === 0 && result.data.data && result.data.data.systemWebMenu) {
                                try {
                                    let config;
                                    if (typeof result.data.data.systemWebMenu === 'string') {
                                        // 如果是字符串,需要解析JSON
                                        config = JSON.parse(result.data.data.systemWebMenu);
                                    } else {
                                        // 如果已经是对象,直接使用
                                        config = result.data.data.systemWebMenu;
                                    }
 
                                    // 检查配置中是否包含features对象
                                    if (config && config.features) {
                                        // 确保knowledgeBase功能存在且配置正确
                                        if (!config.features.knowledgeBase) {
                                            console.warn('配置中缺少knowledgeBase功能,合并默认配置');
                                            config.features = { ...this.defaultFeatures, ...config.features };
                                        }
                                        resolve(config.features);
                                    } else {
                                        console.warn('配置中缺少features对象,使用默认配置');
                                        resolve(this.defaultFeatures);
                                    }
                                    configCache.systemWebMenu = config;
                                } catch (error) {
                                    console.warn('处理systemWebMenu配置失败:', error);
                                    resolve(null);
                                }
                            } else {
                                console.warn('接口返回code不为0或缺少必要数据,使用默认配置');
                                resolve(null);
                            }
                        } else {
                            // 如果没有code字段,直接检查systemWebMenu
                            if (result.data && result.data.systemWebMenu) {
                                try {
                                    let config;
                                    if (typeof result.data.systemWebMenu === 'string') {
                                        // 如果是字符串,需要解析JSON
                                        config = JSON.parse(result.data.systemWebMenu);
                                    } else {
                                        // 如果已经是对象,直接使用
                                        config = result.data.systemWebMenu;
                                    }
 
                                    // 检查配置中是否包含features对象
                                    if (config && config.features) {
                                        // 确保knowledgeBase功能存在且配置正确
                                        if (!config.features.knowledgeBase) {
                                            console.warn('配置中缺少knowledgeBase功能,合并默认配置');
                                            config.features = { ...this.defaultFeatures, ...config.features };
                                        }
                                        resolve(config.features);
                                    } else {
                                        console.warn('配置中缺少features对象,使用默认配置');
                                        resolve(this.defaultFeatures);
                                    }
                                    configCache.systemWebMenu = config;
                                } catch (error) {
                                    console.warn('处理systemWebMenu配置失败:', error);
                                    resolve(null);
                                }
                            } else {
                                console.warn('接口返回缺少systemWebMenu数据,使用默认配置');
                                resolve(null);
                            }
                        }
                        this.updateConfigCache(configCache)
                    } else {
                        console.warn('接口返回数据中缺少data字段,使用默认配置');
                        resolve(null);
                    }
                } else {
                    console.warn('pub-config接口调用失败,使用默认配置');
                    resolve(null);
                }
            });
        });
    }
 
    /**
     * 获取当前配置
     */
    getCurrentConfig() {
        // 返回内存中的当前配置
        return this.currentFeatures;
    }
 
    /**
     * 保存配置到后端API
     */
    async saveConfig(config) {
        try {
            // 更新内存中的配置
            this.currentFeatures = { ...config };
 
            // 异步保存到后端API
            this.saveConfigToAPI(config).catch(error => {
                console.warn('保存配置到API失败:', error);
            }).finally(() => {
                this.init()
            });
 
            // 触发配置变更事件
            window.dispatchEvent(new CustomEvent('featureConfigChanged', {
                detail: config
            }));
        } catch (error) {
            console.error('保存功能配置失败:', error);
        }
    }
 
    /**
     * 保存配置到后端API
     */
    async saveConfigToAPI(config) {
        return new Promise((resolve) => {
            // 直接使用已知的ID(600)更新参数
            Api.admin.updateParam(
                {
                    id: 600,
                    paramCode: 'system-web.menu',
                    paramValue: JSON.stringify({
                        features: config,
                        groups: {
                            featureManagement: ["voiceprintRecognition", "voiceClone", "knowledgeBase", "mcpAccessPoint", "addressBook"],
                            voiceManagement: ["vad", "asr"]
                        }
                    }),
                    valueType: 'json',
                    remark: '系统功能菜单配置'
                },
                (updateResult) => {
                    if (updateResult.code === 0) {
                        resolve();
                    } else {
                        // 如果更新失败,可能是参数不存在或其他错误,记录但不阻止保存到localStorage
                        console.warn('更新参数失败:', updateResult.msg);
                        resolve(); // 不阻止保存到localStorage
                    }
                },
                (error) => {
                    console.warn('更新参数失败:', error);
                    resolve(); // 不阻止保存到localStorage
                }
            );
        });
    }
 
 
 
    /**
     * 获取所有功能配置
     */
    getAllFeatures() {
        return this.getCurrentConfig();
    }
 
    /**
     * 获取简化的配置对象(用于首页组件)
     */
    getConfig() {
        const features = this.getAllFeatures();
        return {
            voiceprintRecognition: features.voiceprintRecognition?.enabled || false,
            voiceClone: features.voiceClone?.enabled || false,
            knowledgeBase: features.knowledgeBase?.enabled || false,
            mcpAccessPoint: features.mcpAccessPoint?.enabled || false,
            vad: features.vad?.enabled || false,
            asr: features.asr?.enabled || false,
            addressBook: features.addressBook?.enabled || false
        };
    }
 
    /**
     * 获取指定功能的状态
     */
    getFeatureStatus(featureKey) {
        const features = this.getAllFeatures();
        return features[featureKey]?.enabled || false;
    }
 
    /**
     * 设置功能状态
     */
    setFeatureStatus(featureKey, enabled) {
        const features = this.getAllFeatures();
        if (features[featureKey]) {
            features[featureKey].enabled = enabled;
            this.saveConfig(features);
            return true;
        }
        return false;
    }
 
    /**
     * 启用功能
     */
    enableFeature(featureKey) {
        return this.setFeatureStatus(featureKey, true);
    }
 
    /**
     * 禁用功能
     */
    disableFeature(featureKey) {
        return this.setFeatureStatus(featureKey, false);
    }
 
    /**
     * 切换功能状态
     */
    toggleFeature(featureKey) {
        const currentStatus = this.getFeatureStatus(featureKey);
        return this.setFeatureStatus(featureKey, !currentStatus);
    }
 
    /**
     * 重置所有功能为默认状态
     */
    resetToDefault() {
        this.saveConfig(this.defaultFeatures);
    }
 
    /**
     * 批量更新功能状态
     */
    updateFeatures(featureUpdates) {
        const features = this.getAllFeatures();
        Object.keys(featureUpdates).forEach(featureKey => {
            if (features[featureKey]) {
                features[featureKey].enabled = featureUpdates[featureKey];
            } else if (this.defaultFeatures[featureKey]) {
                features[featureKey] = { ...this.defaultFeatures[featureKey] };
                features[featureKey].enabled = featureUpdates[featureKey];
            }
        });
        this.saveConfig(features);
    }
 
    /**
     * 获取已启用的功能列表
     */
    getEnabledFeatures() {
        const features = this.getAllFeatures();
        return Object.keys(features).filter(key => features[key].enabled);
    }
 
    /**
     * 检查功能是否启用
     */
    isFeatureEnabled(featureKey) {
        return this.getFeatureStatus(featureKey);
    }
}
 
// 创建单例实例
const featureManager = new FeatureManager();
 
export default featureManager;