form.js.vm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #set( $listSize = $!{dictColumns.size()})
  2. ##判断是否包含排序号字段
  3. #set( $hasSort = 0)
  4. ##判断是否包含用户选择框的标志位
  5. #set( $hasUser = 0)
  6. ##判断是否包含用户选择框的标志位
  7. #set( $hasDept = 0)
  8. ##循环全部字段进行判断
  9. #foreach($column in $columns)
  10. #if($column.htmlType == "user")
  11. #set( $hasUser = $hasUser + 1)
  12. #elseif($column.htmlType == "dept")
  13. #set( $hasDept = $hasDept + 1)
  14. #end
  15. #end
  16. ##循环编辑的字段们进行判断
  17. #foreach ($column in $editColumns)
  18. #if($column.columnName == "sort")
  19. #set( $hasSort = $hasSort + 1)
  20. #break
  21. #end
  22. #end
  23. import AntModal from '@/components/pt/dialog/AntModal'
  24. import { get${BusinessName}, add${BusinessName}, update${BusinessName}#if($hasSort > 0), findMaxSort#end#foreach($column in $uniqueColumns)#set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)}), check${attrName}Unique#end } from '@/api/${moduleName}/${businessName}'
  25. #if($hasUser > 0)
  26. import SelectUser from '@/components/pt/selectUser/SelectUser'
  27. #end
  28. #if($hasDept > 0)
  29. import SelectDept from '@/components/pt/selectDept/SelectDept'
  30. #end
  31. #if($hasAttach)
  32. import SysUpload from '@/components/pt/uploader'
  33. #end
  34. export default {
  35. name: 'CreateForm',
  36. props: {
  37. #foreach ($column in $dictColumns)
  38. ${column.javaField}Options: {
  39. type: Array,
  40. required: true
  41. }#if($foreach.hasNext),
  42. #end
  43. #end
  44. #if(${isSub} == 'true')
  45. #if($listSize > 0)
  46. ,
  47. #end
  48. ${subTableFkNameUnCap}: {
  49. type: String,
  50. required: true
  51. }
  52. #end
  53. },
  54. components: {
  55. AntModal#if($hasDept > 0 || $hasUser > 0 || $hasAttach),
  56. #end
  57. #if($hasUser > 0)
  58. SelectUser#if($hasDept > 0 || $hasAttach),
  59. #end
  60. #end
  61. #if($hasDept > 0)
  62. SelectDept#if($hasAttach),
  63. #end
  64. #end
  65. #if($hasAttach)
  66. SysUpload
  67. #else
  68. #end
  69. },
  70. data () {
  71. #foreach($column in $uniqueColumns)
  72. #set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
  73. #set($parentheseIndex=$column.columnComment.indexOf("("))
  74. #if($parentheseIndex != -1)
  75. #set($comment=$column.columnComment.substring(0, $parentheseIndex))
  76. #else
  77. #set($comment=$column.columnComment)
  78. #end
  79. const validate${attrName} = (rule, value, callback) => {
  80. if (value === '' || value === undefined || value === null) {
  81. callback(new Error('${comment}不允许为空'))
  82. } else {
  83. check${attrName}Unique(this.form.id, this.form.${column.javaField})
  84. callback()
  85. }
  86. }
  87. #end
  88. return {
  89. open: false,
  90. spinning: false,
  91. delayTime: 100,
  92. labelCol: { span: 4 },
  93. wrapperCol: { span: 14 },
  94. loading: false,
  95. disabled: false,
  96. total: 0,
  97. id: undefined,
  98. formTitle: '添加${functionName}',
  99. // 表单参数
  100. form: {},
  101. #if($hasAttach)
  102. attachmentRefName: 'addUploaderFile', // 标志表单是否含有附件
  103. formId: '',
  104. #end
  105. rules: {
  106. #foreach($column in $requireColumns)
  107. #set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
  108. #set($parentheseIndex=$column.columnComment.indexOf("("))
  109. #if($parentheseIndex != -1)
  110. #set($comment=$column.columnComment.substring(0, $parentheseIndex))
  111. #else
  112. #set($comment=$column.columnComment)
  113. #end
  114. ${column.javaField}: [{ required: true, message: '${comment}不能为空',#foreach($uniqueColumn in $uniqueColumns)#if($column.javaField == $uniqueColumn.javaField) validator: validate${attrName},#break#end#end trigger: 'blur' }]#if($foreach.hasNext),
  115. #else
  116. #end
  117. #end
  118. }
  119. }
  120. },
  121. filters: {},
  122. created () {},
  123. computed: {},
  124. watch: {},
  125. mounted () {},
  126. methods: {
  127. onClose () {
  128. this.open = false
  129. this.reset()
  130. this.$emit('close')
  131. },
  132. // 取消按钮
  133. cancel () {
  134. this.open = false
  135. this.reset()
  136. this.$emit('close')
  137. },
  138. // 表单重置
  139. reset () {
  140. this.form = {
  141. id: undefined,
  142. #foreach ($column in $editColumns)
  143. ${column.javaField}: undefined#if($foreach.hasNext),
  144. #else
  145. #end
  146. #end
  147. }
  148. #if($hasAttach)
  149. if (this.formId) {
  150. // 清空附件的formId
  151. this.formId = ''
  152. }
  153. #end
  154. },
  155. /** 新增按钮操作 */
  156. handleAdd () {
  157. this.reset()
  158. #if($hasSort > 0)
  159. /** 获取最大编号 */
  160. findMaxSort().then(response => {
  161. this.form.sort = response.data
  162. this.open = true
  163. this.formTitle = '添加${functionName}'
  164. })
  165. #else
  166. this.open = true
  167. this.formTitle = '添加${functionName}'
  168. #end
  169. #if(${isSub} == 'true')
  170. this.form.${subTableFkNameUnCap} = this.${subTableFkNameUnCap}
  171. #end
  172. },
  173. /** 修改按钮操作 */
  174. handleUpdate (row) {
  175. this.reset()
  176. this.open = true
  177. this.spinning = !this.spinning
  178. const ${businessName}Id = row.id
  179. get${BusinessName}(${businessName}Id).then(response => {
  180. #foreach($column in $columns)
  181. #set($javaField=$column.javaField)
  182. #if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
  183. if (response.data.$javaField !== null) {
  184. response.data.$javaField = response.data.$!{javaField}.split(',')
  185. } else {
  186. response.data.$javaField = []
  187. }
  188. #elseif($column.htmlType == "user" || $column.htmlType == "dept")
  189. response.data.$javaField = { ids: response.data.$javaField, names: response.data.$!{javaField}Name }
  190. #end
  191. #end
  192. this.form = response.data
  193. #if($hasAttach)
  194. this.formId = response.data.id
  195. #end
  196. this.formTitle = '修改${functionName}'
  197. this.spinning = !this.spinning
  198. })
  199. },
  200. /** 提交按钮 */
  201. #set($refs='$refs')
  202. submitForm: function () {
  203. this.disabled = true
  204. this.${refs}.form.validate(valid => {
  205. if (valid) {
  206. const saveForm = JSON.parse(JSON.stringify(this.form))
  207. #foreach($column in $columns)
  208. #set($javaField=$column.javaField)
  209. #if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
  210. if (saveForm.$javaField) {
  211. saveForm.$javaField = saveForm.$!{javaField}.join(',')
  212. } else {
  213. saveForm.$javaField = null
  214. }
  215. #elseif($column.htmlType == "user" || $column.htmlType == "dept")
  216. if (this.form.$javaField !== undefined) {
  217. saveForm.$javaField = this.form.$!{javaField}.ids
  218. }
  219. #end
  220. #end
  221. if (this.form.id !== undefined) {
  222. update${BusinessName}(saveForm).then(response => {
  223. #if($hasAttach)
  224. if (this.attachmentRefName) {
  225. // 表单含有附件
  226. this.formId = response.data.id // 附件上传formId
  227. this.${refs}[this.attachmentRefName].upload(this.formId) // 附件上传
  228. } else {
  229. this.$message.success('新增成功', 3)
  230. this.open = false
  231. this.$emit('ok')
  232. this.$emit('close')
  233. this.disabled = false
  234. }
  235. #else
  236. this.$message.success('新增成功', 3)
  237. this.open = false
  238. this.$emit('ok')
  239. this.$emit('close')
  240. this.disabled = false
  241. #end
  242. })
  243. } else {
  244. add${BusinessName}(saveForm).then(response => {
  245. #if($hasAttach)
  246. if (this.attachmentRefName) {
  247. // 表单含有附件
  248. this.formId = response.data.id // 附件上传formId
  249. this.${refs}[this.attachmentRefName].upload(this.formId) // 附件上传
  250. } else {
  251. this.$message.success('新增成功', 3)
  252. this.open = false
  253. this.$emit('ok')
  254. this.$emit('close')
  255. this.disabled = false
  256. }
  257. #else
  258. this.$message.success('新增成功', 3)
  259. this.open = false
  260. this.$emit('ok')
  261. this.$emit('close')
  262. this.disabled = false
  263. #end
  264. })
  265. }
  266. } else {
  267. this.disabled = false
  268. return false
  269. }
  270. })
  271. },
  272. back () {
  273. const index = '/${moduleName}/${businessNameLowerCase}/index'
  274. this.$router.push(index)
  275. }#if($hasAttach),
  276. uploadCompleteFile: function (successFile, errorFile) {
  277. if (errorFile.length > 0) {
  278. // 有附件保存失败的处理
  279. this.attachmentUploadStatus = false // 记录附件上传失败
  280. this.${refs}[this.attachmentRefName].$el.scrollIntoView() // 页面滚动到附件位置
  281. this.uploaderButtonStatus = false // 按钮关闭loading状态
  282. } else {
  283. // 所有附件都保存成功的处理
  284. this.attachmentUploadStatus = true // 记录附件上传成功
  285. this.$message.success('保存成功', 3)
  286. this.open = false
  287. this.$emit('ok')
  288. this.$emit('close')
  289. }
  290. this.uploaderButtonStatus = false // 返回按钮可用
  291. }
  292. #else
  293. #end
  294. }
  295. }