diff --git a/src/api/system/codegen.ts b/src/api/system/codegen.ts index 5134bce..f1c2581 100644 --- a/src/api/system/codegen.ts +++ b/src/api/system/codegen.ts @@ -57,6 +57,7 @@ export interface UserTemplate { useCount?: number; status?: number; // 0-草稿 1-发布 2-禁用 isEnabled?: number; // 0-隐藏 1-显示 + isContentPublic?: number; // 0-内容不公开 1-内容公开 createTime?: string; updateTime?: string; createBy?: string; @@ -218,6 +219,14 @@ export function toggleTemplateVisibilityApi(id: number, isEnabled: number) { ); } +// 获取模板内容 +export function getTemplateContentApi(id: number | string) { + return http.request>( + "get", + `/codegen/user-template/content/${id}` + ); +} + // ============= 模板仓库管理 API ============= // 获取用户的模板仓库列表 diff --git a/src/views/system/codegen/components/CustomTemplateDialog.vue b/src/views/system/codegen/components/CustomTemplateDialog.vue index a0bb13f..e194594 100644 --- a/src/views/system/codegen/components/CustomTemplateDialog.vue +++ b/src/views/system/codegen/components/CustomTemplateDialog.vue @@ -55,7 +55,9 @@ const editForm = reactive({ description: "", isPublic: 0, version: "1.0.0", - status: 1 + status: 1, + isEnabled: 1, + isContentPublic: 0 }); const editFormRef = ref(); @@ -174,7 +176,8 @@ const handleAdd = () => { isPublic: 0, version: "1.0.0", status: 1, - isEnabled: 1 + isEnabled: 1, + isContentPublic: 0 }); activeTab.value = "edit"; }; @@ -563,7 +566,7 @@ watch( - + 私有 公开 @@ -580,6 +583,22 @@ watch( + + + + + 不公开 + 公开 + +
+ 公开后其他用户可以查看模板源代码 +
+
+
+
+ (null); const previewCode = ref(""); +const templateContentVisible = ref(false); +const templateContentLoading = ref(false); +const templateContent = ref(""); // 计算属性 const dialogVisible = computed({ @@ -209,6 +213,53 @@ const copyCode = async () => { } }; +// 查看模板内容 +const viewTemplateContent = async () => { + if (templateContentVisible.value) { + templateContentVisible.value = false; + return; + } + + templateContentLoading.value = true; + templateContent.value = ""; + + try { + if (currentPreviewTemplate.value?.templateSource === "system") { + // 系统模板:直接显示模板文件内容(这里需要后端支持) + templateContent.value = "// 系统模板内容(需要后端实现系统模板内容获取)"; + } else { + // 用户模板:检查权限并获取内容 + const res = await getTemplateContentApi( + currentPreviewTemplate.value?.templateId || "0" + ); + if (res.code === 200) { + templateContent.value = + res.data !== undefined ? res.data : "// 暂无内容"; + } else { + templateContent.value = res.data || "// 获取失败"; + } + } + templateContentVisible.value = true; + } catch (error) { + templateContent.value = "// 获取失败: 网络错误"; + ElMessage.error("获取模板内容失败"); + console.error(error); + } finally { + templateContentLoading.value = false; + } +}; + +// 复制模板内容 +const copyTemplateContent = async () => { + try { + await navigator.clipboard.writeText(templateContent.value); + ElMessage.success("模板内容已复制到剪贴板"); + } catch (error) { + ElMessage.error("复制失败"); + console.error(error); + } +}; + // 对话框关闭时 const handleClose = () => { dialogVisible.value = false; @@ -427,6 +478,43 @@ watch( + +
+
+ 模板内容预览 +
+ + {{ templateContentVisible ? "隐藏内容" : "查看内容" }} + + + 复制内容 + +
+
+ +
+
{{ templateContentLoading ? '// 加载中...' : (templateContent || '// 暂无内容') }}
+
+
+
+
@@ -784,5 +872,58 @@ watch( } } } + + .template-content-preview { + margin-bottom: 20px; + + .content-header { + display: flex; + align-items: center; + justify-content: space-between; + padding: 12px 16px; + background: var(--el-bg-color-page); + border: 1px solid var(--el-border-color-lighter); + border-bottom: none; + border-radius: 6px 6px 0 0; + + .content-title { + font-size: 14px; + font-weight: 600; + color: var(--el-text-color-primary); + } + + .content-actions { + display: flex; + gap: 8px; + } + } + + .template-content { + max-height: 300px; + overflow-y: auto; + background: var(--el-bg-color); + border: 1px solid var(--el-border-color-lighter); + border-radius: 0 0 6px 6px; + + pre { + padding: 16px; + margin: 0; + font-family: Monaco, Menlo, "Ubuntu Mono", monospace; + font-size: 12px; + line-height: 1.4; + color: var(--el-text-color-regular); + word-wrap: break-word; + white-space: pre-wrap; + background: transparent; + + code { + font-family: inherit; + font-size: inherit; + color: inherit; + background: transparent; + } + } + } + } }