form.js.vm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. ##判断数据字典
  2. #set( $dictCount = 0)
  3. #foreach ($column in $columns)
  4. #if(${column.dictType} != '')
  5. #set( $dictCount = $dictCount + 1)
  6. #end
  7. #end
  8. #set( $listSize = $!{dictColumns.size()})
  9. ##判断是否包含排序号字段
  10. #set( $hasUser = 0)
  11. ##判断是否包含用户选择框的标志位
  12. #set( $hasDept = 0)
  13. ##循环全部字段进行判断
  14. #foreach($column in $columns)
  15. #if($column.htmlType == "user")
  16. #set( $hasUser = $hasUser + 1)
  17. #elseif($column.htmlType == "dept")
  18. #set( $hasDept = $hasDept + 1)
  19. #end
  20. #end
  21. import AntModal from '@/components/pt/dialog/AntModal'
  22. import { get${BusinessName}, add${BusinessName}, update${BusinessName}, findMaxSort#foreach($column in $uniqueColumns)#set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)}), check${attrName}Unique#end } from '@/api/${moduleName}/${businessName}'
  23. #if($hasUser > 0)
  24. import SelectUser from '@/components/pt/selectUser/SelectUser'
  25. #end
  26. #if($hasDept > 0)
  27. import SelectDept from '@/components/pt/selectDept/SelectDept'
  28. #end
  29. #if($hasAttach)
  30. import SysUpload from '@/components/pt/uploader'
  31. #end
  32. export default {
  33. name: 'CreateForm',
  34. props: {
  35. #foreach ($column in $columns)
  36. #if(${column.dictType} != '')
  37. ${column.javaField}Options: {
  38. type: Array,
  39. required: true
  40. },
  41. #end
  42. #end
  43. ${businessName}Options: {
  44. type: Array,
  45. required: true
  46. }
  47. },
  48. components: {
  49. AntModal#if($hasDept > 0 || $hasUser > 0 || $hasAttach),
  50. #end
  51. #if($hasUser > 0)
  52. SelectUser#if($hasDept > 0 || $hasAttach),
  53. #end
  54. #end
  55. #if($hasDept > 0)
  56. SelectDept#if($hasAttach),
  57. #end
  58. #end
  59. #if($hasAttach)
  60. SysUpload
  61. #else
  62. #end
  63. },
  64. data () {
  65. #foreach($column in $uniqueColumns)
  66. #set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
  67. #set($parentheseIndex=$column.columnComment.indexOf("("))
  68. #if($parentheseIndex != -1)
  69. #set($comment=$column.columnComment.substring(0, $parentheseIndex))
  70. #else
  71. #set($comment=$column.columnComment)
  72. #end
  73. const validate${attrName} = (rule, value, callback) => {
  74. if (value === '' || value === undefined || value === null) {
  75. callback(new Error('${comment}不允许为空'))
  76. } else {
  77. check${attrName}Unique(this.form.id, this.form.${column.javaField})
  78. callback()
  79. }
  80. }
  81. #end
  82. return {
  83. open: false,
  84. closeDialog: true,
  85. spinning: false,
  86. delayTime: 100,
  87. labelCol: { span: 4 },
  88. wrapperCol: { span: 14 },
  89. loading: false,
  90. total: 0,
  91. disabled: false,
  92. id: undefined,
  93. formTitle: '添加${functionName}',
  94. currentRow: undefined,
  95. oldParentId: '',
  96. // 表单参数
  97. form: {
  98. id: undefined,
  99. parentId: undefined
  100. },
  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. #set($javaFieldValue = "undefined")
  144. #if($column.javaField == "status")
  145. #set($javaFieldValue = "'0'")
  146. #else
  147. #set($javaFieldValue = "undefined")
  148. #end
  149. ${column.javaField}: ${javaFieldValue}#if($foreach.hasNext),
  150. #else
  151. #end
  152. #end
  153. }
  154. #if($hasAttach)
  155. if (this.formId) {
  156. // 清空附件的formId
  157. this.formId = ''
  158. }
  159. #end
  160. #if($hasAttach)
  161. if (this.formId) {
  162. // 清空附件的formId
  163. this.formId = ''
  164. }
  165. #end
  166. },
  167. /** 新增按钮操作 */
  168. handleAdd (row) {
  169. this.reset()
  170. this.$emit('select-tree')
  171. this.oldParentId = ''
  172. if (row != null && row.id) {
  173. this.currentRow = row
  174. this.oldParentId = row.id
  175. this.form.parentId = row.id
  176. } else {
  177. this.form.parentId = 0
  178. }
  179. /** 获取最大编号 */
  180. findMaxSort(this.form.parentId).then(response => {
  181. this.form.treeSort = response.data
  182. this.open = true
  183. this.formTitle = '添加${functionName}'
  184. })
  185. #if(${isSub} == 'true')
  186. this.form.${subTableFkNameUnCap} = this.${subTableFkNameUnCap}
  187. #end
  188. },
  189. #if($hasCopyRecordOption)
  190. /** 修改按钮操作 */
  191. handleCopy (row) {
  192. this.formTitle = '复制${functionName}'
  193. this.reset()
  194. this.$emit('select-tree', row)
  195. this.open = true
  196. this.spinning = !this.spinning
  197. this.currentRow = row
  198. const ${businessName}Id = row.id
  199. get${BusinessName}(${businessName}Id).then(response => {
  200. #foreach($column in $columns)
  201. #set($javaField=$column.javaField)
  202. #if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
  203. if (response.data.$javaField !== null) {
  204. response.data.$javaField = response.data.$!{javaField}.split(',')
  205. } else {
  206. response.data.$javaField = []
  207. }
  208. #elseif($column.htmlType == "user" || $column.htmlType == "dept")
  209. response.data.$javaField = { ids: response.data.$javaField, names: response.data.$!{javaField}Name }
  210. #end
  211. #end
  212. this.form = response.data
  213. this.form.id = undefined
  214. /** 获取最大编号 */
  215. findMaxSort(this.form.parentId).then(response => {
  216. this.form.treeSort = response.data
  217. })
  218. this.oldParentId = response.data.parentId
  219. this.spinning = !this.spinning
  220. })
  221. },
  222. #end
  223. /** 修改按钮操作 */
  224. handleUpdate (row) {
  225. this.reset()
  226. this.$emit('select-tree', row)
  227. this.open = true
  228. this.spinning = !this.spinning
  229. this.currentRow = row
  230. const ${businessName}Id = row.id
  231. get${BusinessName}(${businessName}Id).then(response => {
  232. #foreach($column in $columns)
  233. #set($javaField=$column.javaField)
  234. #if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
  235. if (response.data.$javaField !== null) {
  236. response.data.$javaField = response.data.$!{javaField}.split(',')
  237. } else {
  238. response.data.$javaField = []
  239. }
  240. #elseif($column.htmlType == "user" || $column.htmlType == "dept")
  241. response.data.$javaField = { ids: response.data.$javaField, names: response.data.$!{javaField}Name }
  242. #end
  243. #end
  244. this.form = response.data
  245. #if($hasAttach)
  246. this.formId = response.data.id
  247. #end
  248. this.oldParentId = response.data.parentId
  249. this.formTitle = '修改${functionName}'
  250. this.spinning = !this.spinning
  251. })
  252. },
  253. /** 提交按钮 */
  254. #set($refs='$refs')
  255. submitForm: function (closeDialog) {
  256. this.closeDialog = closeDialog
  257. this.disabled = true
  258. this.${refs}.form.validate(valid => {
  259. if (valid) {
  260. const saveForm = JSON.parse(JSON.stringify(this.form))
  261. this.spinning = !this.spinning
  262. #foreach($column in $columns)
  263. #set($javaField=$column.javaField)
  264. #if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
  265. if (saveForm.$javaField) {
  266. saveForm.$javaField = saveForm.$!{javaField}.join(',')
  267. } else {
  268. saveForm.$javaField = null
  269. }
  270. #elseif($column.htmlType == "user" || $column.htmlType == "dept")
  271. if (this.form.$javaField !== undefined) {
  272. saveForm.$javaField = this.form.$!{javaField}.ids
  273. }
  274. #end
  275. #end
  276. if (this.form.id !== undefined) {
  277. update${BusinessName}(saveForm).then(response => {
  278. #if($hasAttach)
  279. if (this.attachmentRefName) {
  280. // 表单含有附件
  281. this.formId = response.data.id // 附件上传formId
  282. this.${refs}[this.attachmentRefName].upload(this.formId) // 附件上传
  283. } else {
  284. this.$message.success('更新成功', 3)
  285. if (this.oldParentId !== this.form.parentId) {
  286. // 如果修改父节点则刷新树
  287. this.$emit('ok')
  288. } else {
  289. this.setNodeData(response.data)
  290. }
  291. this.disabled = false
  292. this.cancel()
  293. }
  294. #else
  295. this.$message.success('更新成功', 3)
  296. if (this.oldParentId !== this.form.parentId) {
  297. // 如果修改父节点则刷新树
  298. this.$emit('ok')
  299. } else {
  300. this.setNodeData(response.data)
  301. }
  302. this.disabled = false
  303. this.cancel()
  304. #end
  305. })
  306. } else {
  307. add${BusinessName}(saveForm).then(response => {
  308. #if($hasAttach)
  309. if (this.attachmentRefName) {
  310. // 表单含有附件
  311. this.formId = response.data.id // 附件上传formId
  312. this.${refs}[this.attachmentRefName].upload(this.formId) // 附件上传
  313. } else {
  314. this.$message.success('新增成功', 3)
  315. if (this.oldParentId !== this.form.parentId) {
  316. // 如果修改父节点则刷新树
  317. this.$emit('ok')
  318. } else {
  319. this.appendTreeNode(this.currentRow, response.data)
  320. }
  321. this.disabled = false
  322. if (this.closeDialog) {
  323. this.cancel()
  324. } else {
  325. this.reset()
  326. this.$emit('select-tree')
  327. /** 获取最大编号 */
  328. findMaxSort(this.form.parentId).then(response => {
  329. this.form.treeSort = response.data
  330. })
  331. }
  332. }
  333. #else
  334. this.$message.success('新增成功', 3)
  335. if (this.oldParentId !== this.form.parentId) {
  336. // 如果修改父节点则刷新树
  337. this.$emit('ok')
  338. } else {
  339. this.appendTreeNode(this.currentRow, response.data)
  340. }
  341. this.disabled = false
  342. if (this.closeDialog) {
  343. this.cancel()
  344. } else {
  345. this.reset()
  346. this.$emit('select-tree')
  347. /** 获取最大编号 */
  348. findMaxSort(this.form.parentId).then(response => {
  349. this.form.treeSort = response.data
  350. })
  351. }
  352. #end
  353. })
  354. }
  355. } else {
  356. this.disabled = false
  357. return false
  358. }
  359. })
  360. },
  361. back () {
  362. const index = '/${moduleName}/${businessNameLowerCase}/index'
  363. this.$router.push(index)
  364. },
  365. setNodeData (data) {
  366. #foreach($column in $editColumns)
  367. #set($javaField=$column.javaField)
  368. this.currentRow.$javaField = data.$javaField
  369. #end
  370. }#if($hasAttach),
  371. uploadCompleteFile: function (successFile, errorFile) {
  372. if (errorFile.length > 0) {
  373. // 有附件保存失败的处理
  374. this.attachmentUploadStatus = false // 记录附件上传失败
  375. this.${refs}[this.attachmentRefName].$el.scrollIntoView() // 页面滚动到附件位置
  376. this.uploaderButtonStatus = false // 按钮关闭loading状态
  377. } else {
  378. // 所有附件都保存成功的处理
  379. this.attachmentUploadStatus = true // 记录附件上传成功
  380. this.$message.success('保存成功', 3)
  381. this.open = false
  382. this.$emit('ok')
  383. this.$emit('close')
  384. }
  385. this.uploaderButtonStatus = false // 返回按钮可用
  386. }
  387. #end
  388. }
  389. }