form.vue.btl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <xn-form-container
  3. <% for(var i = 0; i < configList.~size; i++) { %>
  4. <% if(configList[i].needTableId) { %>
  5. :title="formData.${configList[i].fieldNameCamelCase} ? '编辑${functionName}' : '新增${functionName}'"
  6. <% } %>
  7. <% } %>
  8. :width="700"
  9. v-model:open="open"
  10. :destroy-on-close="true"
  11. @close="onClose"
  12. >
  13. <a-form ref="formRef" :model="formData" :rules="formRules" layout="${formLayout}">
  14. <% if(gridWhether) { %>
  15. <a-row :gutter="16">
  16. <% for(var i = 0; i < configList.~size; i++) { %>
  17. <% if(!configList[i].needTableId && configList[i].whetherAddUpdate) { %>
  18. <a-col :span="12">
  19. <a-form-item label="${configList[i].fieldRemark}:" name="${configList[i].fieldNameCamelCase}">
  20. <% if(configList[i].effectType == 'input') { %>
  21. <a-input v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请输入${configList[i].fieldRemark}" allow-clear />
  22. <% } else if (configList[i].effectType == 'textarea') {%>
  23. <a-textarea v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请输入${configList[i].fieldRemark}" :auto-size="{ minRows: 3, maxRows: 5 }" />
  24. <% } else if (configList[i].effectType == 'select') {%>
  25. <a-select v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请选择${configList[i].fieldRemark}" :options="${configList[i].fieldNameCamelCase}Options" show-search allow-clear option-filter-prop="label"/>
  26. <% } else if (configList[i].effectType == 'radio') {%>
  27. <a-radio-group v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请选择${configList[i].fieldRemark}" :options="${configList[i].fieldNameCamelCase}Options" />
  28. <% } else if (configList[i].effectType == 'checkbox') {%>
  29. <a-checkbox-group v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请选择${configList[i].fieldRemark}" :options="${configList[i].fieldNameCamelCase}Options" />
  30. <% } else if (configList[i].effectType == 'datepicker') {%>
  31. <a-date-picker v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择${configList[i].fieldRemark}" style="width: 100%" allow-clear/>
  32. <% } else if (configList[i].effectType == 'timepicker') {%>
  33. <a-time-picker v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请选择${configList[i].fieldRemark}" style="width: 100%" allow-clear/>
  34. <% } else if (configList[i].effectType == 'inputNumber') {%>
  35. <a-input-number v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" :min="1" :max="10000" style="width: 100%" />
  36. <% } else if (configList[i].effectType == 'slider') {%>
  37. <a-slider v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" :max="1000" style="width: 100%" />
  38. <% } else if (configList[i].effectType == 'fileUpload') {%>
  39. <xn-upload v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" />
  40. <% } else if (configList[i].effectType == 'imageUpload') {%>
  41. <xn-upload v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" uploadMode="image" />
  42. <% } else if (configList[i].effectType == 'editor') {%>
  43. <xn-editor v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请输入${configList[i].fieldRemark}" />
  44. <% } %>
  45. </a-form-item>
  46. </a-col>
  47. <% } %>
  48. <% } %>
  49. </a-row>
  50. <% } else { %>
  51. <% for(var i = 0; i < configList.~size; i++) { %>
  52. <% if(!configList[i].needTableId && configList[i].whetherAddUpdate) { %>
  53. <a-form-item label="${configList[i].fieldRemark}:" name="${configList[i].fieldNameCamelCase}">
  54. <% if(configList[i].effectType == 'input') { %>
  55. <a-input v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请输入${configList[i].fieldRemark}" allow-clear />
  56. <% } else if (configList[i].effectType == 'textarea') {%>
  57. <a-textarea v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请输入${configList[i].fieldRemark}" :auto-size="{ minRows: 3, maxRows: 5 }" />
  58. <% } else if (configList[i].effectType == 'select') {%>
  59. <a-select v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请选择${configList[i].fieldRemark}" :options="${configList[i].fieldNameCamelCase}Options" show-search allow-clear option-filter-prop="label"/>
  60. <% } else if (configList[i].effectType == 'radio') {%>
  61. <a-radio-group v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请选择${configList[i].fieldRemark}" :options="${configList[i].fieldNameCamelCase}Options" />
  62. <% } else if (configList[i].effectType == 'checkbox') {%>
  63. <a-checkbox-group v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请选择${configList[i].fieldRemark}" :options="${configList[i].fieldNameCamelCase}Options" />
  64. <% } else if (configList[i].effectType == 'datepicker') {%>
  65. <a-date-picker v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择${configList[i].fieldRemark}" style="width: 100%" allow-clear/>
  66. <% } else if (configList[i].effectType == 'timepicker') {%>
  67. <a-time-picker v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择${configList[i].fieldRemark}" style="width: 100%" allow-clear/>
  68. <% } else if (configList[i].effectType == 'inputNumber') {%>
  69. <a-input-number v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请输入${configList[i].fieldRemark}" :min="1" :max="10000" style="width: 100%" />
  70. <% } else if (configList[i].effectType == 'slider') {%>
  71. <a-slider v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请滑动${configList[i].fieldRemark}" :max="1000" style="width: 100%" />
  72. <% } else if (configList[i].effectType == 'fileUpload') {%>
  73. <xn-upload v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" />
  74. <% } else if (configList[i].effectType == 'imageUpload') {%>
  75. <xn-upload v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" uploadMode="image" />
  76. <% } else if (configList[i].effectType == 'editor') {%>
  77. <xn-editor v-model:value="formData.${configList[i].fieldNameCamelCase}" :disabled="isView" placeholder="请输入${configList[i].fieldRemark}" />
  78. <% } %>
  79. </a-form-item>
  80. <% } %>
  81. <% } %>
  82. <% } %>
  83. </a-form>
  84. <template #footer>
  85. <a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
  86. <a-popconfirm title="请确认是否保存?" @confirm="onSubmit" v-if="!isView">
  87. <a-button type="primary" :loading="submitLoading">保存</a-button>
  88. </a-popconfirm>
  89. </template>
  90. </xn-form-container>
  91. </template>
  92. <script setup name="${classNameFirstLower}Form">
  93. <%
  94. var iptTool = 0;
  95. for(var i = 0; i < configList.~size; i++) {
  96. if(!configList[i].needTableId) {
  97. if(configList[i].effectType == 'select' || configList[i].effectType == 'radio' || configList[i].effectType == 'checkbox') {
  98. iptTool++;
  99. }
  100. }
  101. }
  102. %>
  103. <% if(iptTool > 0) { %>
  104. import tool from '@/utils/tool'
  105. <% } %>
  106. import { cloneDeep } from 'lodash-es'
  107. import { required } from '@/utils/formRules'
  108. import ${classNameFirstLower}Api from '@/api/${moduleName}/${classNameFirstLower}Api'
  109. // 抽屉状态
  110. const open = ref(false)
  111. const emit = defineEmits({ successful: null })
  112. const formRef = ref()
  113. // 表单数据
  114. const formData = ref({})
  115. const submitLoading = ref(false)
  116. const isView = ref(false)
  117. <% for(var i = 0; i < configList.~size; i++) { %>
  118. <% if(!configList[i].needTableId) { %>
  119. <% if(configList[i].effectType == 'select' || configList[i].effectType == 'radio' || configList[i].effectType == 'checkbox') { %>
  120. const ${configList[i].fieldNameCamelCase}Options = ref([])
  121. <% } %>
  122. <% } %>
  123. <% } %>
  124. // 打开抽屉
  125. const onOpen = (record, view = false) => {
  126. open.value = true
  127. isView.value = view
  128. if (record) {
  129. let recordData = cloneDeep(record)
  130. <% for(var i = 0; i < configList.~size; i++) { %>
  131. <% if(!configList[i].needTableId && configList[i].whetherAddUpdate && configList[i].effectType == 'checkbox') { %>
  132. recordData.${configList[i].fieldNameCamelCase} = JSON.parse(recordData.${configList[i].fieldNameCamelCase})
  133. <% } %>
  134. <% } %>
  135. formData.value = Object.assign({}, recordData)
  136. }
  137. <% for(var i = 0; i < configList.~size; i++) { %>
  138. <% if(!configList[i].needTableId) { %>
  139. <% if(configList[i].effectType == 'select' || configList[i].effectType == 'radio' || configList[i].effectType == 'checkbox') { %>
  140. ${configList[i].fieldNameCamelCase}Options.value = tool.dictList('${configList[i].dictTypeCode}')
  141. <% } %>
  142. <% } %>
  143. <% } %>
  144. }
  145. // 关闭抽屉
  146. const onClose = () => {
  147. formRef.value.resetFields()
  148. formData.value = {}
  149. open.value = false
  150. isView.value = true
  151. }
  152. // 默认要校验的
  153. const formRules = {
  154. <% for(var i = 0; i < configList.~size; i++) { %>
  155. <% if(!configList[i].needTableId) { %>
  156. <% if(configList[i].required) { %>
  157. ${configList[i].fieldNameCamelCase}: [required('请输入${configList[i].fieldRemark}')],
  158. <% } %>
  159. <% } %>
  160. <% } %>
  161. }
  162. // 验证并提交数据
  163. const onSubmit = () => {
  164. formRef.value
  165. .validate()
  166. .then(() => {
  167. submitLoading.value = true
  168. const formDataParam = cloneDeep(formData.value)
  169. <% for(var i = 0; i < configList.~size; i++) { %>
  170. <% if(configList[i].whetherAddUpdate && configList[i].effectType == 'checkbox') { %>
  171. formDataParam.${configList[i].fieldNameCamelCase} = JSON.stringify(formDataParam.${configList[i].fieldNameCamelCase})
  172. <% } %>
  173. <% } %>
  174. ${classNameFirstLower}Api
  175. <% for(var i = 0; i < configList.~size; i++) { %>
  176. <% if(configList[i].needTableId) { %>
  177. .${classNameFirstLower}SubmitForm(formDataParam, formDataParam.${configList[i].fieldNameCamelCase})
  178. <% } %>
  179. <% } %>
  180. .then(() => {
  181. onClose()
  182. emit('successful')
  183. })
  184. .finally(() => {
  185. submitLoading.value = false
  186. })
  187. })
  188. .catch(() => {})
  189. }
  190. // 抛出函数
  191. defineExpose({
  192. onOpen
  193. })
  194. </script>
  195. <style lang="less" scoped>
  196. </style>