Skip to content

Commit b1e3fcb

Browse files
authored
fix(CreateMcp): 修复工具编辑数据无法回显 --story=131249935
fix(CreateMcp): 修复工具编辑数据无法回显 --story=131249935
2 parents 70ab6e9 + ed503d7 commit b1e3fcb

1 file changed

Lines changed: 63 additions & 28 deletions

File tree

src/dashboard-front/src/views/mcp-server/components/CreateSlider.vue

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -855,30 +855,66 @@ const previewUrl = computed(() => {
855855
return `${prefix || ''}/${serverNamePrefix.value}${formData.value.name}/${!['sse'].includes(formData.value.protocol_type) ? 'mcp' : formData.value.protocol_type}/`;
856856
});
857857
858-
watch(isShow, async () => {
859-
if (isShow.value) {
860-
clearValidate();
861-
if (isEditMode.value) {
862-
await fetchServer();
863-
}
864-
if (isEnablePrompt.value) {
865-
await Promise.allSettled([
866-
fetchStageList(),
867-
fetchPromptResources(),
868-
]);
869-
}
870-
else {
871-
resourceTabList.value = resourceTabList.value.filter(item => !['prompt'].includes(item.value));
872-
await fetchStageList();
858+
/**
859+
* 获取公共异步数据(提取重复逻辑,降低耦合)
860+
* @param {boolean} isEnablePrompt 是否启用 Prompt 功能
861+
*/
862+
const fetchCommonData = async (isEnablePrompt: boolean) => {
863+
// 定义基础请求列表
864+
const requestList = isEditMode.value ? [] : [fetchStageList()];
865+
// 启用 Prompt 时,追加 Prompt 资源请求
866+
if (isEnablePrompt) {
867+
requestList.push(fetchPromptResources());
868+
}
869+
else {
870+
// 禁用 Prompt 时,过滤掉 prompt 相关的标签
871+
resourceTabList.value = resourceTabList.value.filter(
872+
item => !['prompt'].includes(item.value),
873+
);
874+
}
875+
if (!requestList.length) return;
876+
const results = await Promise.allSettled(requestList);
877+
// 处理单个请求的失败情况
878+
results.forEach((result) => {
879+
if (result.status === 'rejected') {
880+
Message({
881+
theme: 'error',
882+
message: JSON.stringify(result?.reason?.stack),
883+
});
873884
}
874-
const initFormData = {
875-
formData: formData.value,
876-
toolSelections: toolSelections.value,
877-
promptSelections: promptSelections.value,
878-
};
879-
initSidebarFormData(initFormData);
885+
});
886+
};
887+
888+
// 获取常用环境列表 分类列表数据,设置默认初始化表单
889+
const getCommonListData = async () => {
890+
await fetchCommonData(isEnablePrompt.value);
891+
initSidebarFormData(getDiffFormData());
892+
};
893+
894+
/**
895+
* 处理 isShow 状态变化的核心逻辑
896+
* @param {boolean} isShowVal 当前 isShow 的值
897+
*/
898+
const handleIsShowChange = async (isShowVal: boolean) => {
899+
// 仅在 isShow 为 true 时执行后续逻辑
900+
if (!isShowVal) return;
901+
902+
clearValidate();
903+
if (isEditMode.value) {
904+
await fetchServer();
880905
}
881-
});
906+
getCommonListData();
907+
};
908+
909+
watch(isShow, handleIsShowChange, { immediate: false });
910+
911+
const getDiffFormData = () => {
912+
return {
913+
formData: formData.value,
914+
toolSelections: toolSelections.value,
915+
promptSelections: promptSelections.value,
916+
};
917+
};
882918
883919
const resetResizeLayout = () => {
884920
nextTick(() => {
@@ -1106,6 +1142,7 @@ const fetchServer = async () => {
11061142
url.value = response?.url ?? '';
11071143
// 渲染tool勾选数据
11081144
if (resource_names.length) {
1145+
await fetchStageList();
11091146
toolSelections.value = resourceList.value
11101147
.filter(item => resource_names.includes(item.name))
11111148
.map(({ name, id }) => ({
@@ -1143,6 +1180,9 @@ const fetchServer = async () => {
11431180
catch {
11441181
formData.value = cloneDeep(defaultFormData.value);
11451182
}
1183+
finally {
1184+
initSidebarFormData(getDiffFormData());
1185+
}
11461186
};
11471187
11481188
const fetchStageResources = async () => {
@@ -1415,12 +1455,7 @@ const handleScrollView = (el: HTMLInputElement | HTMLElement) => {
14151455
};
14161456
14171457
const handleBeforeClose = () => {
1418-
const diffFormData = {
1419-
formData: formData.value,
1420-
toolSelections: toolSelections.value,
1421-
promptSelections: promptSelections.value,
1422-
};
1423-
const results = isSidebarClosed(JSON.stringify(diffFormData));
1458+
const results = isSidebarClosed(JSON.stringify(getDiffFormData()));
14241459
return results;
14251460
};
14261461

0 commit comments

Comments
 (0)