addUpdateModal.vue.vm 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <a-modal
  3. v-model:visible="pageDataObj.visible"
  4. :title="pageDataObj.title"
  5. width="60%"
  6. :destroyOnClose="true"
  7. @ok="clickModalOk"
  8. @cancel="handleModalCancel"
  9. >
  10. <a-form ref="formRef" :model="addUpdateFormObj.data" :rules="addUpdateFormObj.rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 18 }">
  11. <a-row :gutter="16">
  12. #foreach($column in $columns)
  13. #if($column.insert || $column.edit)
  14. #set($field=$column.javaField)
  15. #set($dictType=$column.dictType)
  16. #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
  17. #set($parentheseIndex=$column.columnComment.indexOf("("))
  18. #if($parentheseIndex != -1)
  19. #set($comment=$column.columnComment.substring(0, $parentheseIndex))
  20. #else
  21. #set($comment=$column.columnComment)
  22. #end
  23. #if(!$column.pk)
  24. #if($column.htmlType == "input")
  25. <a-col span="12">
  26. <a-form-item name="${field}" label="${comment}" :rules="[{required: ${column.required}, message: '${comment}不能为空!'}]">
  27. <a-input v-model:value="addUpdateFormObj.data.${field}" placeholder="请输入${comment}" />
  28. </a-form-item>
  29. </a-col>
  30. #elseif($column.htmlType == "imageUpload")
  31. <a-col span="12">
  32. <a-form-item name="${field}" label="${comment}">
  33. <image-upload v-model:value="addUpdateFormObj.data.${field}" />
  34. </a-form-item>
  35. </a-col>
  36. #elseif($column.htmlType == "fileUpload")
  37. <a-col span="12">
  38. <a-form-item name="${field}" label="${comment}">
  39. <file-upload v-model:value="addUpdateFormObj.data.${field}" />
  40. </a-form-item>
  41. </a-col>
  42. #elseif($column.htmlType == "editor")
  43. <a-col span="24">
  44. <a-form-item name="${field}" label="${comment}" :label-col="{ span: 3 }" :wrapper-col="{ span: 21 }">
  45. <WangEditor v-model:value="addUpdateFormObj.data.${field}" :height="300" />
  46. </a-form-item>
  47. </a-col>
  48. #elseif($column.htmlType == "select" && "" != $dictType)
  49. <a-col span="12">
  50. <a-form-item name="${field}" label="${comment}" :rules="[{required: ${column.required}, message: '请选择${comment}!'}]">
  51. <a-select v-model:value="addUpdateFormObj.data.${field}" placeholder="请选择${comment}">
  52. <a-select-option v-for="dict in ${field}Options" :key="dict.value" :value="dict.value">
  53. {{ dict.label }}
  54. </a-select-option>
  55. </a-select>
  56. </a-form-item>
  57. </a-col>
  58. #elseif($column.htmlType == "select" && $dictType)
  59. <a-col span="12">
  60. <a-form-item name="${field}" label="${comment}" :rules="[{required: ${column.required}, message: '请选择${comment}!'}]">
  61. <a-select v-model:value="addUpdateFormObj.data.${field}" placeholder="请选择${comment}">
  62. <a-select-option value="">请选择字典生成</a-select-option>
  63. </a-select>
  64. </a-form-item>
  65. </a-col>
  66. #elseif($column.htmlType == "checkbox" && "" != $dictType)
  67. <a-col span="12">
  68. <a-form-item name="${field}" label="${comment}">
  69. <a-checkbox-group v-model:value="addUpdateFormObj.data.${field}">
  70. <a-checkbox v-for="dict in ${field}Options" :key="dict.value" :value="dict.value">
  71. {{ dict.label }}
  72. </a-checkbox>
  73. </a-checkbox-group>
  74. </a-form-item>
  75. </a-col>
  76. #elseif($column.htmlType == "checkbox" && $dictType)
  77. <a-col span="12">
  78. <a-form-item name="${field}" label="${comment}">
  79. <a-checkbox-group v-model:value="addUpdateFormObj.data.${field}">
  80. <a-checkbox>请选择字典生成</a-checkbox>
  81. </a-checkbox-group>
  82. </a-form-item>
  83. </a-col>
  84. #elseif($column.htmlType == "radio" && "" != $dictType)
  85. <a-col span="12">
  86. <a-form-item name="${field}" label="${comment}" :rules="[{required: ${column.required}, message: '请选择${comment}!'}]">
  87. <a-radio-group v-model:value="addUpdateFormObj.data.${field}">
  88. <a-radio v-for="dict in ${field}Options" :key="dict.value" :value="dict.value">
  89. {{ dict.label }}
  90. </a-radio>
  91. </a-radio-group>
  92. </a-form-item>
  93. </a-col>
  94. #elseif($column.htmlType == "radio" && $dictType)
  95. <a-col span="12">
  96. <a-form-item name="${field}" label="${comment}" :rules="[{required: ${column.required}, message: '请选择${comment}!'}]">
  97. <a-radio-group v-model:value="addUpdateFormObj.data.${field}">
  98. <a-radio value="">请选择字典生成</a-radio>
  99. </a-radio-group>
  100. </a-form-item>
  101. </a-col>
  102. #elseif($column.htmlType == "datetime")
  103. <a-col span="12">
  104. <a-form-item name="${field}" label="${comment}" :rules="[{required: ${column.required}, message: '请选择${comment}!'}]">
  105. <a-date-picker
  106. v-model:value="addUpdateFormObj.data.${field}"
  107. style="width: 100%"
  108. format="YYYY-MM-DD HH:mm:ss"
  109. placeholder="请选择${comment}"
  110. />
  111. </a-form-item>
  112. </a-col>
  113. #elseif($column.htmlType == "textarea")
  114. <a-col span="24">
  115. <a-form-item name="${field}" label="${comment}" :label-col="{ span: 3 }" :wrapper-col="{ span: 21 }">
  116. <a-textarea v-model:value="addUpdateFormObj.data.${field}" placeholder="请输入${comment}" :rows="4" />
  117. </a-form-item>
  118. </a-col>
  119. #end
  120. #end
  121. #end
  122. #end
  123. #if($tplCategory == 'tree')
  124. <a-col span="12">
  125. <a-form-item name="${treeParentCode}" label="上级${functionName}">
  126. <a-tree-select
  127. v-model:value="addUpdateFormObj.data.${treeParentCode}"
  128. style="width: 100%"
  129. :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
  130. :tree-data="${businessName}Options"
  131. placeholder="选择上级${functionName}"
  132. tree-default-expand-all
  133. :field-names="{ children: 'children', label: '${treeName}', value: '${treeCode}' }"
  134. />
  135. </a-form-item>
  136. </a-col>
  137. #end
  138. </a-row>
  139. </a-form>
  140. </a-modal>
  141. </template>
  142. <script setup>
  143. import { getCurrentInstance, reactive, ref } from 'vue';
  144. import { message } from 'ant-design-vue';
  145. import dayjs from 'dayjs';
  146. import { get${BusinessName}, add${BusinessName}, update${BusinessName} } from '@/api/${moduleName}/${businessName}';
  147. #if($table.sub)
  148. import WangEditor from '@/components/editor/WangEditor.vue';
  149. #end
  150. // 获取当前实例
  151. const { proxy } = getCurrentInstance();
  152. // Props
  153. const props = defineProps({
  154. #foreach ($column in $columns)
  155. #if(${column.dictType} != '')
  156. ${column.javaField}Options: {
  157. type: Array,
  158. default: () => []
  159. },
  160. #end
  161. #end
  162. #if($tplCategory == 'tree')
  163. ${businessName}Options: {
  164. type: Array,
  165. default: () => []
  166. },
  167. #end
  168. });
  169. // Emits
  170. const emit = defineEmits(['success']);
  171. // 组件引用
  172. const formRef = ref();
  173. // 页面数据对象
  174. const pageDataObj = reactive({
  175. visible: false,
  176. title: '',
  177. type: ''
  178. });
  179. // 表单数据对象
  180. const addUpdateFormObj = reactive({
  181. data: {
  182. #foreach ($column in $columns)
  183. #if($column.insert || $column.edit)
  184. #if(!$column.pk)
  185. ${column.javaField}: undefined,
  186. #end
  187. #end
  188. #end
  189. },
  190. rules: {
  191. #foreach ($column in $columns)
  192. #if($column.insert || $column.edit)
  193. #if($column.required && !$column.pk)
  194. #set($parentheseIndex=$column.columnComment.indexOf("("))
  195. #if($parentheseIndex != -1)
  196. #set($comment=$column.columnComment.substring(0, $parentheseIndex))
  197. #else
  198. #set($comment=$column.columnComment)
  199. #end
  200. ${column.javaField}: [
  201. { required: true, message: '${comment}不能为空', trigger: 'blur' }
  202. ],
  203. #end
  204. #end
  205. #end
  206. }
  207. });
  208. // 打开弹窗
  209. const open = async (type, record = {}) => {
  210. pageDataObj.type = type;
  211. pageDataObj.title = type === 'add' ? '新增${functionName}' : '编辑${functionName}';
  212. pageDataObj.visible = true;
  213. // 重置表单
  214. resetForm();
  215. if (type === 'edit' && record.${pkColumn.javaField}) {
  216. try {
  217. const response = await get${BusinessName}(record.${pkColumn.javaField});
  218. const data = response.data;
  219. // 转换日期字符串为dayjs对象
  220. Object.keys(data).forEach(key => {
  221. const value = data[key];
  222. // 检查是否是日期时间字符串格式 (YYYY-MM-DD HH:mm:ss)
  223. if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(value)) {
  224. data[key] = dayjs(value);
  225. }
  226. });
  227. Object.assign(addUpdateFormObj.data, data);
  228. } catch (error) {
  229. console.error('获取数据失败:', error);
  230. message.error('获取数据失败');
  231. }
  232. } else if (type === 'add' && record) {
  233. // 新增时可能需要设置父级ID等
  234. Object.assign(addUpdateFormObj.data, record);
  235. }
  236. };
  237. // 重置表单
  238. const resetForm = () => {
  239. addUpdateFormObj.data = {
  240. #foreach ($column in $columns)
  241. #if($column.insert || $column.edit)
  242. #if(!$column.pk)
  243. ${column.javaField}: undefined,
  244. #end
  245. #end
  246. #end
  247. };
  248. formRef.value?.resetFields();
  249. };
  250. // 确定按钮点击
  251. const clickModalOk = async () => {
  252. try {
  253. await formRef.value.validate();
  254. // 转换dayjs对象为字符串格式
  255. const submitData = { ...addUpdateFormObj.data };
  256. Object.keys(submitData).forEach(key => {
  257. const value = submitData[key];
  258. // 如果是dayjs对象,转换为字符串
  259. if (value && typeof value === 'object' && value.format) {
  260. submitData[key] = value.format('YYYY-MM-DD HH:mm:ss');
  261. }
  262. });
  263. if (pageDataObj.type === 'add') {
  264. await add${BusinessName}(submitData);
  265. message.success('新增成功');
  266. } else {
  267. await update${BusinessName}(submitData);
  268. message.success('修改成功');
  269. }
  270. pageDataObj.visible = false;
  271. emit('success');
  272. } catch (error) {
  273. console.error('操作失败:', error);
  274. }
  275. };
  276. // 取消按钮点击
  277. const handleModalCancel = () => {
  278. pageDataObj.visible = false;
  279. resetForm();
  280. };
  281. // 暴露方法
  282. defineExpose({
  283. open
  284. });
  285. </script>
  286. <style lang="less" scoped>
  287. // 自定义样式
  288. </style>