edit.stub 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <component
  3. {% if component_type == 1 %}
  4. is="a-modal"
  5. {% else %}
  6. is="a-drawer"
  7. {% endif %}
  8. {% if is_full == 2 %}
  9. {% if component_type == 1 %}
  10. :fullscreen="true"
  11. {% else %}
  12. width="100%"
  13. {% endif %}
  14. {% else %}
  15. :width="tool.getDevice() === 'mobile' ? '100%' : '{{form_width}}px'"
  16. {% endif %}
  17. v-model:visible="visible"
  18. :title="title"
  19. :mask-closable="false"
  20. :ok-loading="loading"
  21. @cancel="close"
  22. @before-ok="submit">
  23. <!-- 表单信息 start -->
  24. <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true">
  25. {% for column in columns %}
  26. {% if column.is_insert == 2 %}
  27. {% if column.view_type == 'input' %}
  28. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  29. <a-input v-model="formData.{{column.column_name}}" placeholder="请输入{{column.column_comment}}" />
  30. </a-form-item>
  31. {% endif %}
  32. {% if column.view_type == 'password' %}
  33. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  34. <a-input-password v-model="formData.{{column.column_name}}" placeholder="请输入{{column.column_comment}}" />
  35. </a-form-item>
  36. {% endif %}
  37. {% if column.view_type == 'textarea' %}
  38. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  39. <a-textarea v-model="formData.{{column.column_name}}" placeholder="请输入{{column.column_comment}}" />
  40. </a-form-item>
  41. {% endif %}
  42. {% if column.view_type == 'inputNumber' %}
  43. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  44. <a-input-number v-model="formData.{{column.column_name}}" placeholder="请输入{{column.column_comment}}" />
  45. </a-form-item>
  46. {% endif %}
  47. {% if column.view_type == 'inputTag' %}
  48. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  49. <a-input-tag v-model="formData.{{column.column_name}}" placeholder="请输入{{column.column_comment}}" allow-clear />
  50. </a-form-item>
  51. {% endif %}
  52. {% if column.view_type == 'switch' %}
  53. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  54. <sa-switch v-model="formData.{{column.column_name}}" />
  55. </a-form-item>
  56. {% endif %}
  57. {% if column.view_type == 'slider' %}
  58. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  59. <a-slider v-model="formData.{{column.column_name}}" placeholder="请输入{{column.column_comment}}" />
  60. </a-form-item>
  61. {% endif %}
  62. {% if column.view_type == 'select' %}
  63. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  64. <a-select v-model="formData.{{column.column_name}}" :options="[]" placeholder="请选择{{column.column_comment}}" allow-clear />
  65. </a-form-item>
  66. {% endif %}
  67. {% if column.view_type == 'saSelect' %}
  68. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  69. <sa-select v-model="formData.{{column.column_name}}" dict="{{column.dict_type}}" placeholder="请选择{{column.column_comment}}" allow-clear />
  70. </a-form-item>
  71. {% endif %}
  72. {% if column.view_type == 'treeSelect' and column.column_name == options.tree_parent_id %}
  73. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  74. <a-tree-select
  75. v-model="formData.{{column.column_name}}"
  76. :data="treeData"
  77. :field-names="{ key: '{{options.tree_id}}', title: '{{options.tree_name}}', icon: 'customIcon' }"
  78. placeholder="请选择{{column.column_comment}}"
  79. allow-clear />
  80. </a-form-item>
  81. {% endif %}
  82. {% if column.view_type == 'treeSelect' and column.column_name != options.tree_parent_id %}
  83. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  84. <a-tree-select v-model="formData.{{column.column_name}}" :data="[]" placeholder="请选择{{column.column_comment}}" allow-clear />
  85. </a-form-item>
  86. {% endif %}
  87. {% if column.view_type == 'radio' %}
  88. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  89. <sa-radio v-model="formData.{{column.column_name}}" dict="{{column.dict_type}}" />
  90. </a-form-item>
  91. {% endif %}
  92. {% if column.view_type == 'checkbox' %}
  93. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  94. <sa-checkbox v-model="formData.{{column.column_name}}" dict="{{column.dict_type}}" />
  95. </a-form-item>
  96. {% endif %}
  97. {% if column.view_type == 'date' %}
  98. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  99. <a-date-picker v-model="formData.{{column.column_name}}" :show-time="{{column.options.showTime|bool}}" mode="{{column.options.mode}}" placeholder="请选择{{column.column_comment}}" />
  100. </a-form-item>
  101. {% endif %}
  102. {% if column.view_type == 'time' %}
  103. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  104. <a-time-picker v-model="formData.{{column.column_name}}" placeholder="请选择{{column.column_comment}}" />
  105. </a-form-item>
  106. {% endif %}
  107. {% if column.view_type == 'rate' %}
  108. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  109. <a-rate v-model="formData.{{column.column_name}}" />
  110. </a-form-item>
  111. {% endif %}
  112. {% if column.view_type == 'cascader' %}
  113. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  114. <a-cascader v-model="formData.{{column.column_name}}" :options="[]" placeholder="请选择{{column.column_comment}}" allow-clear />
  115. </a-form-item>
  116. {% endif %}
  117. {% if column.view_type == 'userSelect' %}
  118. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  119. <sa-user v-model="formData.{{column.column_name}}" :isEcho="true" />
  120. </a-form-item>
  121. {% endif %}
  122. {% if column.view_type == 'cityLinkage' %}
  123. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  124. <ma-cityLinkage v-model="formData.{{column.column_name}}" type="${type}" mode="${mode}" />
  125. </a-form-item>
  126. {% endif %}
  127. {% if column.view_type == 'uploadImage' %}
  128. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  129. <sa-upload-image v-model="formData.{{column.column_name}}" :limit="{{column.options.limit|formatNumber}}" :multiple="{{column.options.multiple|bool}}" />
  130. </a-form-item>
  131. {% endif %}
  132. {% if column.view_type == 'uploadFile' %}
  133. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  134. <sa-upload-file v-model="formData.{{column.column_name}}" :limit="{{column.options.limit|formatNumber}}" :multiple="{{column.options.multiple|bool}}" />
  135. </a-form-item>
  136. {% endif %}
  137. {% if column.view_type == 'wangEditor' %}
  138. <a-form-item label="{{column.column_comment}}" field="{{column.column_name}}">
  139. <ma-wangEditor v-model="formData.{{column.column_name}}" :height="{{column.options.height}}" />
  140. </a-form-item>
  141. {% endif %}
  142. {% endif %}
  143. {% endfor %}
  144. </a-form>
  145. <!-- 表单信息 end -->
  146. </component>
  147. </template>
  148. <script setup>
  149. import { ref, reactive, computed } from 'vue'
  150. import tool from '@/utils/tool'
  151. import { Message, Modal } from '@arco-design/web-vue'
  152. {% if package_name == '' %}
  153. import api from '../api/{{business_name}}'
  154. {% else %}
  155. import api from '../../api/{{package_name}}/{{business_name}}'
  156. {% endif %}
  157. const emit = defineEmits(['success'])
  158. // 引用定义
  159. const visible = ref(false)
  160. const loading = ref(false)
  161. const formRef = ref()
  162. const mode = ref('')
  163. {% if tpl_category == 'tree' %}
  164. const treeData = ref([])
  165. {% endif %}
  166. let title = computed(() => {
  167. return '{{menu_name}}' + (mode.value == 'add' ? '-新增' : '-编辑')
  168. })
  169. // 表单初始值
  170. const initialFormData = {
  171. {% for column in columns %}
  172. {% if column.is_pk == 2 %}
  173. {{column.column_name}}: null,
  174. {% elseif column.is_insert == 2 %}
  175. {% if column.column_type == 'int' or column.column_type == 'smallint' or column.column_type == 'tinyint' %}
  176. {{column.column_name}}: {{column.default_value | parseNumber}},
  177. {% elseif column.view_type == 'inputTag' %}
  178. {{column.column_name}}: [],
  179. {% else %}
  180. {{column.column_name}}: '{{column.default_value}}',
  181. {% endif %}
  182. {% endif %}
  183. {% endfor %}
  184. }
  185. // 表单信息
  186. const formData = reactive({ ...initialFormData })
  187. // 验证规则
  188. const rules = {
  189. {% for column in columns %}
  190. {% if column.is_required == 2 and column.is_pk == 1 %}
  191. {{column.column_name}}: [{ required: true, message: '{{column.column_comment}}必需填写' }],
  192. {% endif %}
  193. {% endfor %}
  194. }
  195. // 打开弹框
  196. const open = async (type = 'add') => {
  197. mode.value = type
  198. // 重置表单数据
  199. Object.assign(formData, initialFormData)
  200. formRef.value.clearValidate()
  201. visible.value = true
  202. await initPage()
  203. }
  204. // 初始化页面数据
  205. {% if tpl_category == 'single' %}
  206. const initPage = async () => {}
  207. {% else %}
  208. const initPage = async () => {
  209. const resp = await api.getPageList()
  210. treeData.value = resp.data
  211. }
  212. {% endif %}
  213. // 设置数据
  214. const setFormData = async (data) => {
  215. for (const key in formData) {
  216. if (data[key] != null && data[key] != undefined) {
  217. formData[key] = data[key]
  218. }
  219. }
  220. }
  221. // 数据保存
  222. const submit = async (done) => {
  223. const validate = await formRef.value?.validate()
  224. if (!validate) {
  225. loading.value = true
  226. let data = { ...formData }
  227. let result = {}
  228. if (mode.value === 'add') {
  229. // 添加数据
  230. data.{{pk}} = undefined
  231. result = await api.save(data)
  232. } else {
  233. // 修改数据
  234. result = await api.update(data.{{pk}}, data)
  235. }
  236. if (result.code === 200) {
  237. Message.success('操作成功')
  238. emit('success')
  239. done(true)
  240. }
  241. // 防止连续点击提交
  242. setTimeout(() => {
  243. loading.value = false
  244. }, 500)
  245. }
  246. done(false)
  247. }
  248. // 关闭弹窗
  249. const close = () => (visible.value = false)
  250. defineExpose({ open, setFormData })
  251. </script>