file-add.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <div class="ele-body">
  3. <el-card shadow="never">
  4. <el-tabs v-model="tabValue" type="card" @tab-click="handleTabClick">
  5. <el-tab-pane label="范文信息" name="1">
  6. <el-form
  7. :model="formData"
  8. ref="form"
  9. label-width="110px"
  10. :rules="rules"
  11. style="margin-top: 20px;"
  12. >
  13. <el-row :gutter="24">
  14. <el-col :span="6">
  15. <el-form-item label="范文编号" prop="code">
  16. <el-input
  17. placeholder="系统自动编号"
  18. disabled
  19. v-model="formData.code"
  20. >
  21. </el-input>
  22. </el-form-item>
  23. </el-col>
  24. <el-col :span="6">
  25. <el-form-item label="范文名称" prop="name">
  26. <el-input
  27. placeholder="请输入范文名称"
  28. v-model="formData.name"
  29. :disabled="disabled"
  30. >
  31. </el-input>
  32. </el-form-item>
  33. </el-col>
  34. <el-col :span="6">
  35. <el-form-item label="状态" prop="status">
  36. <el-switch
  37. v-model="formData.status"
  38. :disabled="disabled"
  39. active-text="启用"
  40. inactive-text="停用">
  41. </el-switch>
  42. </el-form-item>
  43. </el-col>
  44. </el-row>
  45. </el-form>
  46. </el-tab-pane>
  47. <el-tab-pane label="稿纸信息" name="2">
  48. <MainBodyTemplate ref="mainBodyTemplate" :type="routerQuery.type"></MainBodyTemplate>
  49. </el-tab-pane>
  50. <el-tab-pane label="正文" name="3">
  51. <tinymce-editor :disabled="disabled" v-model="formData.content" :init="{ height: 525 }" />
  52. </el-tab-pane>
  53. </el-tabs>
  54. <div v-if="!disabled" class="footer">
  55. <el-button type="primary" @click="save" v-click-once>保存</el-button>
  56. <el-button @click="cancel">取消</el-button>
  57. </div>
  58. </el-card>
  59. </div>
  60. </template>
  61. <script>
  62. import MainBodyTemplate from './mainBodyTemplate.vue'
  63. import TinymceEditor from '@/components/TinymceEditor/index.vue';
  64. import { createDoc, docTplTemplateById, updateDoc } from '@/api/documents/doc-manage';
  65. export default {
  66. components: {
  67. MainBodyTemplate,
  68. TinymceEditor
  69. },
  70. data() {
  71. return {
  72. tabValue: '1',
  73. formData: {
  74. code: '',
  75. name: '',
  76. status: true,
  77. content: '',
  78. fields: [],
  79. directoryId: ''
  80. },
  81. rules: {
  82. // code: [{ required: true, message: '请输入范文编号', trigger: 'blur' }],
  83. name: [{ required: true, message: '请输入范文名称', trigger: 'blur' }],
  84. status: [{ required: true, message: '请选择状态', trigger: 'blur' }],
  85. },
  86. }
  87. },
  88. created() {
  89. console.log('created~~~~');
  90. this.routerQuery = this.$route.query || {};
  91. this.formData.directoryId = this.routerQuery.treeId || '';
  92. if(this.routerQuery.type != 'add') {
  93. this.getDetail();
  94. }
  95. },
  96. computed: {
  97. disabled() {
  98. return this.routerQuery.type == 'detail';
  99. }
  100. },
  101. methods: {
  102. async getDetail() {
  103. console.log('getDetail~~~~');
  104. let res = await docTplTemplateById(this.routerQuery.id);
  105. res.status = res.status == 1 ? true : false;
  106. this.formData = res;
  107. this.$refs.mainBodyTemplate.setData(this.formData.fields);
  108. },
  109. handleTabClick(tab) {
  110. this.tabValue = tab.name;
  111. },
  112. async save() {
  113. try {
  114. // 先校验范文信息表单
  115. const formValid = await this.$refs.form.validate().catch(() => false);
  116. if (!formValid) {
  117. this.$message.warning('请完善范文信息');
  118. return;
  119. }
  120. // 校验子组件 mainBodyTemplate 的必填项
  121. const mainBodyValid = await this.$refs.mainBodyTemplate.$refs.form.validate().catch(() => false);
  122. if (!mainBodyValid) {
  123. this.$message.warning('请完善稿纸信息的必填项');
  124. return;
  125. }
  126. // 获取自定义字段数据
  127. const customFields = this.$refs.mainBodyTemplate.generateCustomFields();
  128. // 将 customFields 放到 formData 字段
  129. this.formData.fields = customFields;
  130. this.formData.status = this.formData.status ? 1 : 0;
  131. console.log('提交数据:', this.formData);
  132. // 提交到后端
  133. const loading = this.$loading({ lock: true });
  134. const requestUrl = this.routerQuery.type == 'edit' ? updateDoc : createDoc;
  135. requestUrl(this.formData)
  136. .then((res) => {
  137. loading.close();
  138. this.$message.success('保存成功');
  139. this.$router.go(-1);
  140. })
  141. .catch((e) => {
  142. loading.close();
  143. console.error('保存失败:', e);
  144. });
  145. } catch (error) {
  146. console.error('保存异常:', error);
  147. }
  148. },
  149. cancel() {
  150. this.$router.go(-1);
  151. },
  152. }
  153. }
  154. </script>
  155. <style scoped>
  156. .el-card {
  157. min-height: 600px;
  158. }
  159. .footer {
  160. text-align: center;
  161. }
  162. </style>