From efc0c3a697969503634138d7881543f4099b81ca Mon Sep 17 00:00:00 2001
From: yyb <995253665@qq.com>
Date: 星期三, 20 五月 2026 13:32:09 +0800
Subject: [PATCH] 审批模板导入只能从已有模板导入

---
 src/views/officeProcessAutomation/ApproveManage/approve-template/components/FormConfigEditor.vue |  108 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 96 insertions(+), 12 deletions(-)

diff --git a/src/views/officeProcessAutomation/ApproveManage/approve-template/components/FormConfigEditor.vue b/src/views/officeProcessAutomation/ApproveManage/approve-template/components/FormConfigEditor.vue
index b63383f..a5d1da7 100644
--- a/src/views/officeProcessAutomation/ApproveManage/approve-template/components/FormConfigEditor.vue
+++ b/src/views/officeProcessAutomation/ApproveManage/approve-template/components/FormConfigEditor.vue
@@ -21,12 +21,20 @@
           </el-tag>
         </div>
         <div class="fce-toolbar-actions">
-          <el-dropdown trigger="click" @command="applyPreset">
-            <el-button size="small">浠庨璁惧鍏�</el-button>
+          <el-dropdown trigger="click" @visible-change="onImportDropdownVisible" @command="importFromTemplate">
+            <el-button size="small" :loading="templateImportLoading">浠庡凡鏈夋ā鏉垮鍏�</el-button>
             <template #dropdown>
               <el-dropdown-menu>
-                <el-dropdown-item v-for="p in FORM_CONFIG_PRESETS" :key="p.key" :command="p.key">
-                  {{ p.label }}
+                <el-dropdown-item v-if="!templateImportOptions.length" disabled>
+                  鏆傛棤鍏朵粬瀹℃壒妯℃澘
+                </el-dropdown-item>
+                <el-dropdown-item
+                  v-for="t in templateImportOptions"
+                  :key="t.id"
+                  :command="t.id"
+                >
+                  <span>{{ t.label }}</span>
+                  <el-tag v-if="!t.enabled" size="small" type="info" class="import-tag">宸插仠鐢�</el-tag>
                 </el-dropdown-item>
               </el-dropdown-menu>
             </template>
@@ -38,7 +46,7 @@
       <el-empty
         v-if="!inner.fields.length"
         class="fce-empty"
-        description="鏆傛棤濉姤椤癸紝鍙坊鍔犳垨浠庨璁惧揩閫熷鍏�"
+        description="鏆傛棤濉姤椤癸紝鍙坊鍔犳垨浠庡凡鏈夊鎵规ā鏉垮鍏�"
         :image-size="72"
       />
 
@@ -288,14 +296,25 @@
 
 <script setup>
 import { Bottom, Delete, Plus, Top } from "@element-plus/icons-vue";
-import { reactive, watch } from "vue";
 import {
-  FORM_CONFIG_PRESETS,
+  getApprovalTemplateDetail,
+  listApprovalTemplate,
+  TEMPLATE_TYPE_BUILTIN,
+  TEMPLATE_TYPE_CUSTOM,
+} from "@/api/officeProcessAutomation/approvalTemplate.js";
+import { ElMessage, ElMessageBox } from "element-plus";
+import { reactive, ref, watch } from "vue";
+import {
+  mapEnabledFromApi,
+  unwrapTemplateDetail,
+  unwrapTemplateList,
+} from "../approveTemplateConstants.js";
+import {
   FORM_FIELD_TYPE_OPTIONS,
-  applyFormConfigPreset,
   createEmptyFormConfigData,
   createEmptyFormField,
   formFieldTypeLabel,
+  parseFormConfigToData,
 } from "../formConfigUtils.js";
 import {
   SELECT_OPTION_SOURCE,
@@ -306,6 +325,8 @@
 
 const props = defineProps({
   modelValue: { type: Object, default: () => createEmptyFormConfigData() },
+  /** 缂栬緫褰撳墠妯℃澘鏃舵帓闄よ嚜韬紝閬垮厤浠庤嚜宸卞鍏� */
+  excludeTemplateId: { type: [String, Number], default: null },
 });
 
 const emit = defineEmits(["update:modelValue"]);
@@ -313,6 +334,9 @@
 const inner = reactive(createEmptyFormConfigData());
 
 const { loading: optionSourceLoading, ensureForFields, getOptions } = useSelectOptionSources();
+
+const templateImportOptions = ref([]);
+const templateImportLoading = ref(false);
 
 function typeLabel(type) {
   return formFieldTypeLabel(type);
@@ -435,10 +459,66 @@
   emitOut();
 }
 
-function applyPreset(key) {
-  const data = applyFormConfigPreset(key);
-  syncFromProps(data);
-  emitOut();
+async function loadTemplateImportOptions() {
+  templateImportLoading.value = true;
+  try {
+    const [customRes, builtinRes] = await Promise.all([
+      listApprovalTemplate(TEMPLATE_TYPE_CUSTOM),
+      listApprovalTemplate(TEMPLATE_TYPE_BUILTIN),
+    ]);
+    const excludeId =
+      props.excludeTemplateId != null && props.excludeTemplateId !== ""
+        ? String(props.excludeTemplateId)
+        : "";
+    templateImportOptions.value = [...unwrapTemplateList(customRes), ...unwrapTemplateList(builtinRes)]
+      .filter((row) => row?.id != null && String(row.id) !== excludeId)
+      .map((row) => ({
+        id: row.id,
+        label: row.templateName || `妯℃澘 #${row.id}`,
+        enabled: mapEnabledFromApi(row.enabled),
+      }));
+  } catch {
+    templateImportOptions.value = [];
+    ElMessage.error("鍔犺浇瀹℃壒妯℃澘鍒楄〃澶辫触");
+  } finally {
+    templateImportLoading.value = false;
+  }
+}
+
+function onImportDropdownVisible(visible) {
+  if (visible) loadTemplateImportOptions();
+}
+
+async function importFromTemplate(templateId) {
+  if (!templateId) return;
+  if (inner.fields.length) {
+    try {
+      await ElMessageBox.confirm("灏嗚鐩栧綋鍓嶅~鎶ラ」閰嶇疆锛屾槸鍚︾户缁紵", "浠庢ā鏉垮鍏�", {
+        type: "warning",
+        confirmButtonText: "缁х画瀵煎叆",
+        cancelButtonText: "鍙栨秷",
+      });
+    } catch {
+      return;
+    }
+  }
+  templateImportLoading.value = true;
+  try {
+    const res = await getApprovalTemplateDetail(templateId);
+    const row = unwrapTemplateDetail(res);
+    const data = parseFormConfigToData(row?.formConfig);
+    if (!data.fields?.length) {
+      ElMessage.warning("璇ユā鏉挎湭閰嶇疆濉姤椤�");
+      return;
+    }
+    syncFromProps(data);
+    emitOut();
+    ElMessage.success(`宸插鍏ャ��${row.templateName || "妯℃澘"}銆嶇殑濉姤椤筦);
+  } catch {
+    ElMessage.error("鍔犺浇妯℃澘璇︽儏澶辫触");
+  } finally {
+    templateImportLoading.value = false;
+  }
 }
 </script>
 
@@ -496,6 +576,10 @@
   align-items: center;
   gap: 8px;
 }
+.import-tag {
+  margin-left: 8px;
+  vertical-align: middle;
+}
 
 .fce-empty {
   padding: 24px 0;

--
Gitblit v1.9.3