form.js.vm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 || ${isSub} == 'true'),
  42. #end
  43. #end
  44. #if(${isSub} == 'true')
  45. ${subTableFkNameUnCap}: {
  46. type: String,
  47. required: true
  48. }
  49. },
  50. #else
  51. },
  52. #end
  53. components: {
  54. AntModal#if($hasDept > 0 || $hasUser > 0 || $hasAttach),
  55. #end
  56. #if($hasUser > 0)
  57. SelectUser#if($hasDept > 0 || $hasAttach),
  58. #end
  59. #end
  60. #if($hasDept > 0)
  61. SelectDept#if($hasAttach),
  62. #end
  63. #end
  64. #if($hasAttach)
  65. SysUpload
  66. #else
  67. #end
  68. },
  69. data () {
  70. #foreach($column in $uniqueColumns)
  71. #set($attrName = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
  72. #set($parentheseIndex=$column.columnComment.indexOf("("))
  73. #if($parentheseIndex != -1)
  74. #set($comment=$column.columnComment.substring(0, $parentheseIndex))
  75. #else
  76. #set($comment=$column.columnComment)
  77. #end
  78. const validate${attrName} = (rule, value, callback) => {
  79. if (value === '' || value === undefined || value === null) {
  80. callback(new Error('${comment}不允许为空'))
  81. } else {
  82. check${attrName}Unique(this.form.id, this.form.${column.javaField})
  83. callback()
  84. }
  85. }
  86. #end
  87. return {
  88. open: false,
  89. closeDialog: true,
  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. #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. },
  161. /** 新增按钮操作 */
  162. handleAdd () {
  163. this.reset()
  164. #if($hasSort > 0)
  165. /** 获取最大编号 */
  166. findMaxSort().then(response => {
  167. this.form.sort = response.data
  168. this.open = true
  169. this.formTitle = '添加${functionName}'
  170. })
  171. #else
  172. this.open = true
  173. this.formTitle = '添加${functionName}'
  174. #end
  175. #if(${isSub} == 'true')
  176. this.form.${subTableFkNameUnCap} = this.${subTableFkNameUnCap}
  177. #end
  178. },
  179. #if($hasCopyRecordOption)
  180. /** 复制按钮操作 */
  181. handleCopy (id) {
  182. this.formTitle = '复制${functionName}'
  183. this.reset()
  184. this.open = true
  185. this.spinning = !this.spinning
  186. const ${businessName}Id = id
  187. get${BusinessName}(${businessName}Id).then(response => {
  188. #foreach($column in $columns)
  189. #set($javaField=$column.javaField)
  190. #if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
  191. if (response.data.$javaField !== null) {
  192. response.data.$javaField = response.data.$!{javaField}.split(',')
  193. } else {
  194. response.data.$javaField = []
  195. }
  196. #elseif($column.htmlType == "user" || $column.htmlType == "dept")
  197. response.data.$javaField = { ids: response.data.$javaField, names: response.data.$!{javaField}Name }
  198. #end
  199. #end
  200. this.form = response.data
  201. this.form.id = undefined
  202. this.spinning = !this.spinning
  203. #if($hasSort > 0)
  204. /** 获取最大编号 */
  205. findMaxSort().then(response => {
  206. this.form.sort = response.data
  207. })
  208. #end
  209. })
  210. },
  211. #end
  212. /** 修改按钮操作 */
  213. handleUpdate (row) {
  214. this.reset()
  215. this.open = true
  216. this.spinning = !this.spinning
  217. const ${businessName}Id = row.id
  218. get${BusinessName}(${businessName}Id).then(response => {
  219. #foreach($column in $columns)
  220. #set($javaField=$column.javaField)
  221. #if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
  222. if (response.data.$javaField !== null) {
  223. response.data.$javaField = response.data.$!{javaField}.split(',')
  224. } else {
  225. response.data.$javaField = []
  226. }
  227. #elseif($column.htmlType == "user" || $column.htmlType == "dept")
  228. response.data.$javaField = { ids: response.data.$javaField, names: response.data.$!{javaField}Name }
  229. #end
  230. #end
  231. this.form = response.data
  232. #if($hasAttach)
  233. this.formId = response.data.id
  234. #end
  235. this.formTitle = '修改${functionName}'
  236. this.spinning = !this.spinning
  237. })
  238. },
  239. /** 提交按钮 */
  240. #set($refs='$refs')
  241. submitForm: function (closeDialog) {
  242. this.closeDialog = closeDialog
  243. this.disabled = true
  244. this.${refs}.form.validate(valid => {
  245. if (valid) {
  246. const saveForm = JSON.parse(JSON.stringify(this.form))
  247. #foreach($column in $columns)
  248. #set($javaField=$column.javaField)
  249. #if($column.htmlType == "selectMultiple" || $column.htmlType == "checkbox")
  250. if (saveForm.$javaField) {
  251. saveForm.$javaField = saveForm.$!{javaField}.join(',')
  252. } else {
  253. saveForm.$javaField = null
  254. }
  255. #elseif($column.htmlType == "user" || $column.htmlType == "dept")
  256. if (this.form.$javaField !== undefined) {
  257. saveForm.$javaField = this.form.$!{javaField}.ids
  258. }
  259. #end
  260. #end
  261. if (this.form.id !== undefined) {
  262. update${BusinessName}(saveForm).then(response => {
  263. #if($hasAttach)
  264. if (this.attachmentRefName) {
  265. // 表单含有附件
  266. this.formId = response.data.id // 附件上传formId
  267. this.${refs}[this.attachmentRefName].upload(this.formId) // 附件上传
  268. } else {
  269. this.$message.success('更新成功', 3)
  270. this.open = false
  271. this.$emit('ok')
  272. this.$emit('close')
  273. this.disabled = false
  274. }
  275. #else
  276. this.$message.success('更新成功', 3)
  277. this.open = false
  278. this.$emit('ok')
  279. this.$emit('close')
  280. this.disabled = false
  281. #end
  282. })
  283. } else {
  284. add${BusinessName}(saveForm).then(response => {
  285. #if($hasAttach)
  286. if (this.attachmentRefName) {
  287. // 表单含有附件
  288. this.formId = response.data.id // 附件上传formId
  289. this.${refs}[this.attachmentRefName].upload(this.formId) // 附件上传
  290. } else {
  291. this.$message.success('新增成功', 3)
  292. this.$emit('ok')
  293. #if($hasSaveAndAddOption)
  294. if (this.closeDialog) {
  295. this.open = false
  296. this.$emit('close')
  297. this.disabled = false
  298. } else {
  299. this.reset()
  300. #if($hasSort > 0)
  301. /** 获取最大编号 */
  302. findMaxSort().then(response => {
  303. this.form.sort = response.data
  304. this.disabled = false
  305. })
  306. #else
  307. this.disabled = false
  308. #end
  309. }
  310. #else
  311. this.open = false
  312. this.$emit('ok')
  313. this.$emit('close')
  314. this.disabled = false
  315. #end
  316. }
  317. #else
  318. #if($hasSaveAndAddOption)
  319. if (this.closeDialog) {
  320. this.$message.success('新增成功', 3)
  321. this.open = false
  322. this.$emit('ok')
  323. this.$emit('close')
  324. this.disabled = false
  325. } else {
  326. this.$emit('ok')
  327. this.reset()
  328. #if($hasSort > 0)
  329. /** 获取最大编号 */
  330. findMaxSort().then(response => {
  331. this.form.sort = response.data
  332. this.disabled = false
  333. })
  334. #else
  335. this.disabled = false
  336. #end
  337. }
  338. #else
  339. this.$message.success('新增成功', 3)
  340. this.open = false
  341. this.$emit('ok')
  342. this.$emit('close')
  343. this.disabled = false
  344. #end
  345. #end
  346. })
  347. }
  348. } else {
  349. this.disabled = false
  350. return false
  351. }
  352. })
  353. },
  354. back () {
  355. const index = '/${moduleName}/${businessNameLowerCase}/index'
  356. this.$router.push(index)
  357. }#if($hasAttach),
  358. uploadCompleteFile: function (successFile, errorFile) {
  359. if (errorFile.length > 0) {
  360. // 有附件保存失败的处理
  361. this.attachmentUploadStatus = false // 记录附件上传失败
  362. this.${refs}[this.attachmentRefName].$el.scrollIntoView() // 页面滚动到附件位置
  363. this.uploaderButtonStatus = false // 按钮关闭loading状态
  364. } else {
  365. // 所有附件都保存成功的处理
  366. this.attachmentUploadStatus = true // 记录附件上传成功
  367. this.$message.success('保存成功', 3)
  368. #if($hasSaveAndAddOption)
  369. if (this.closeDialog) {
  370. this.open = false
  371. this.$emit('ok')
  372. this.$emit('close')
  373. } else {
  374. this.$emit('ok')
  375. this.reset()
  376. #if($hasSort > 0)
  377. /** 获取最大编号 */
  378. findMaxSort().then(response => {
  379. this.form.sort = response.data
  380. this.disabled = false
  381. })
  382. #else
  383. this.disabled = false
  384. #end
  385. }
  386. #else
  387. this.open = false
  388. this.$emit('ok')
  389. this.$emit('close')
  390. #end
  391. }
  392. this.uploaderButtonStatus = false // 返回按钮可用
  393. }
  394. #else
  395. #end
  396. }
  397. }