diff --git a/dashboard/src/views/ProviderPage.vue b/dashboard/src/views/ProviderPage.vue index e807af287..683085eb8 100644 --- a/dashboard/src/views/ProviderPage.vue +++ b/dashboard/src/views/ProviderPage.vue @@ -306,6 +306,24 @@ + + + + + + mdi-alert-circle-outline + 确认保存 + + + 您没有填写 API Key,确定要保存吗?这可能会导致该服务提供商无法正常工作。 + + + + 取消 + 确定 + + + @@ -336,6 +354,10 @@ export default { sessionSeparationEnabled: false, sessionSettingLoading: false, + // Key确认对话框 + showKeyConfirm: false, + keyConfirmResolve: null, + newSelectedProviderName: '', newSelectedProviderConfig: {}, updatingMode: false, @@ -383,6 +405,16 @@ export default { } }, + watch: { + showKeyConfirm(newValue) { + // 当对话框关闭时,如果 Promise 还在等待,则拒绝它以防止内存泄漏 + if (!newValue && this.keyConfirmResolve) { + this.keyConfirmResolve(false); + this.keyConfirmResolve = null; + } + } + }, + computed: { // 根据选择的标签过滤提供商列表 filteredProviders() { @@ -563,32 +595,40 @@ export default { this.updatingMode = true; }, - newProvider() { + async newProvider() { + // 检查 key 是否为空 + if ( + 'key' in this.newSelectedProviderConfig && + (!this.newSelectedProviderConfig.key || this.newSelectedProviderConfig.key.length === 0) + ) { + const confirmed = await this.confirmEmptyKey(); + if (!confirmed) { + return; // 如果用户取消,则中止保存 + } + } + this.loading = true; - if (this.updatingMode) { - axios.post('/api/config/provider/update', { - id: this.newSelectedProviderName, - config: this.newSelectedProviderConfig - }).then((res) => { - this.loading = false; - this.showProviderCfg = false; - this.getConfig(); + const wasUpdating = this.updatingMode; + try { + if (wasUpdating) { + const res = await axios.post('/api/config/provider/update', { + id: this.newSelectedProviderName, + config: this.newSelectedProviderConfig + }); this.showSuccess(res.data.message || "更新成功!"); - }).catch((err) => { - this.loading = false; - this.showError(err.response?.data?.message || err.message); - }); - this.updatingMode = false; - } else { - axios.post('/api/config/provider/new', this.newSelectedProviderConfig).then((res) => { - this.loading = false; - this.showProviderCfg = false; - this.getConfig(); + } else { + const res = await axios.post('/api/config/provider/new', this.newSelectedProviderConfig); this.showSuccess(res.data.message || "添加成功!"); - }).catch((err) => { - this.loading = false; - this.showError(err.response?.data?.message || err.message); - }); + } + this.showProviderCfg = false; + this.getConfig(); + } catch (err) { + this.showError(err.response?.data?.message || err.message); + } finally { + this.loading = false; + if (wasUpdating) { + this.updatingMode = false; + } } }, @@ -670,7 +710,21 @@ export default { this.loadingStatus = false; this.showError(err.response?.data?.message || err.message); }); - } + }, + + confirmEmptyKey() { + this.showKeyConfirm = true; + return new Promise((resolve) => { + this.keyConfirmResolve = resolve; + }); + }, + + handleKeyConfirm(confirmed) { + if (this.keyConfirmResolve) { + this.keyConfirmResolve(confirmed); + } + this.showKeyConfirm = false; + }, } }