demoForm2.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. <template>
  2. <div class="form-main">
  3. <a-card :body-style="{padding: '24px 32px'}" :bordered="false">
  4. <!-- <a-form @submit="handleSubmit" :form="form">
  5. <a-form-item
  6. label="标题"
  7. :labelCol="{lg: {span: 7}, sm: {span: 7}}"
  8. :wrapperCol="{lg: {span: 10}, sm: {span: 17} }">
  9. <a-input :disabled="disabled"
  10. v-decorator="[
  11. 'name',
  12. {rules: [{ required: true, message: '请输入标题' }]}
  13. ]"
  14. name="name"
  15. placeholder="给目标起个名字" />
  16. </a-form-item>
  17. <a-form-item v-if="!disabled"
  18. :wrapperCol="{ span: 24 }"
  19. style="text-align: center"
  20. >
  21. <a-button htmlType="submit" type="primary" :disabled="disabled||btndisabled" @click="handleSubmit">保存</a-button>
  22. <a-button style="margin-left: 8px" :disabled="disabled" @click="close">取消</a-button>
  23. </a-form-item>
  24. </a-form>-->
  25. <k-form-build
  26. :value="jsonData"
  27. @change="handleChange"
  28. :dynamicData="dynamicData"
  29. ref="KFB"
  30. @submit="handleSubmit"
  31. />
  32. <div style="text-align:center">
  33. <a-button
  34. htmlType="submit"
  35. type="primary"
  36. :disabled="disabled||btndisabled"
  37. @click="handleSubmit"
  38. >保存</a-button>
  39. <todoManageOperation
  40. v-if="processData.operationType&&processData.operationType=='1'"
  41. :processData="processData"
  42. :todoManageOperationObject="todoManageOperationObject"
  43. :disabled="disabled"
  44. :btndisabled="btndisabled"
  45. @refreshToDo="refreshToDo"
  46. @handleSubmit2="handleSubmit2"
  47. ></todoManageOperation>
  48. <a-button style="margin-left: 8px;" :disabled="disabled||disabled2" @click="close">取消</a-button>
  49. </div>
  50. </a-card>
  51. </div>
  52. </template>
  53. <script>
  54. import pick from 'lodash.pick'
  55. import todoManageOperation from '../operation/todoManageOperation'
  56. export default {
  57. name: 'demoForm',
  58. props: {
  59. /*全局禁用,可表示查看*/
  60. disabled: {
  61. type: Boolean,
  62. default: false,
  63. required: false
  64. },
  65. /*流程数据*/
  66. processData: {
  67. type: Object,
  68. default: () => {
  69. return {}
  70. },
  71. required: false
  72. },
  73. /*是否新增*/
  74. isNew: { type: Boolean, default: false, required: false }
  75. },
  76. components: {
  77. todoManageOperation
  78. },
  79. data() {
  80. return {
  81. todoManageOperationObject: {
  82. isSave: false,
  83. formData: {}
  84. },
  85. disabled2: false, //用来取决于表单时不同流程节点填写不同表单信息使用
  86. jsonData: {},
  87. url: {
  88. getForm: '/actBusiness/getForm',
  89. addApply: '/actBusiness/add',
  90. editForm: '/actBusiness/editForm',
  91. leaveType: '/sys/dict/getDictItems'
  92. },
  93. description: '流程表单demo,按例开发表单。需在 activitiMixin.js 中加入写好的表单',
  94. // form
  95. form: this.$form.createForm(this),
  96. /*表单回显数据*/
  97. data: {},
  98. btndisabled: false,
  99. ceshi_file: null,
  100. dynamicData: {
  101. typeData: [], //请假类型下拉数据
  102. reimbursementTypeList: [], //报销类型下拉数据
  103. invoiceTypeList: [] //发票类型下拉数据
  104. }
  105. }
  106. },
  107. created() {
  108. console.log('流程数据', this.processData)
  109. //加载一些下拉选择数据等
  110. this.getDataList()
  111. let businessTable = ''
  112. //如果没有值则在另一个属性上有值
  113. if (!this.processData.businessTable) {
  114. this.processData.businessTable = this.processData.tableName
  115. }
  116. this.getAction('/tbTableInfo/query', {
  117. businessTable: this.processData.businessTable,
  118. taskNodeId: this.processData.key
  119. }).then(res => {
  120. this.jsonData = res.result.jsonContent
  121. // this.$refs.KFB.setData({aaa:null,bbb:null,name:"123"})
  122. // this.$refs.KFB.getData().then(res => {
  123. // console.log('aaa',res)
  124. // })
  125. //后台检测到流程节点有关联表单且前台处于我的待办界面打开表单详情
  126. if (res.result.isSave && this.processData.isSuspended != undefined) {
  127. this.disabled = false
  128. this.btndisabled = false
  129. }
  130. if (!this.isNew) {
  131. this.init()
  132. }
  133. })
  134. },
  135. methods: {
  136. //获取获取下拉数据等
  137. getDataList() {
  138. //获取请假类型下拉数据
  139. this.getAction(this.url.leaveType + '/leave_type').then(res => {
  140. if (res.success) {
  141. this.dynamicData.typeData = res.result
  142. } else {
  143. this.$message.error(res.message)
  144. }
  145. })
  146. //获取报销类型下拉数据
  147. this.getAction(this.url.leaveType + '/reimbursement_type').then(res => {
  148. if (res.success) {
  149. this.dynamicData.reimbursementTypeList = res.result
  150. } else {
  151. this.$message.error(res.message)
  152. }
  153. })
  154. //获取发票类型下拉数据
  155. this.getAction(this.url.leaveType + '/invoice_type').then(res => {
  156. if (res.success) {
  157. this.dynamicData.invoiceTypeList = res.result
  158. } else {
  159. this.$message.error(res.message)
  160. }
  161. })
  162. },
  163. /*回显数据*/
  164. init() {
  165. this.btndisabled = true
  166. var r = this.processData
  167. this.getAction(this.url.getForm, {
  168. tableId: r.tableId,
  169. tableName: r.tableName
  170. }).then(res => {
  171. if (res.success) {
  172. let formData = res.result
  173. formData.tableName = r.tableName
  174. this.data = formData
  175. console.log('表单回显数据', this.data)
  176. console.log(this.data)
  177. this.$refs.KFB.setData(this.data)
  178. // this.$nextTick(() => {
  179. // this.$refs.KFB.form.setFieldsValue(pick(this.data, 'name'))
  180. // })
  181. this.btndisabled = false
  182. } else {
  183. this.$message.error(res.message)
  184. }
  185. })
  186. },
  187. //表单字段数值发生改变事件
  188. handleChange(value, key) {
  189. // 数据变化时触发
  190. //如果开始时间发生变化
  191. if (key == 'start_time') {
  192. //判断结束时间是否存在
  193. if (this.$refs.KFB.form.getFieldValue('end_time')) {
  194. //获取时间差
  195. let duration = startEndFun(value, this.$refs.KFB.form.getFieldValue('end_time'))
  196. if (duration) {
  197. // 使用k-form-design组件的form属性修改表单数据
  198. this.$refs.KFB.setData({
  199. duration: duration
  200. })
  201. }
  202. }
  203. }
  204. //如果结束时间发生变化
  205. if (key == 'end_time') {
  206. //判断结束时间是否存在
  207. if (this.$refs.KFB.form.getFieldValue('start_time')) {
  208. //获取时间差
  209. let duration = startEndFun(this.$refs.KFB.form.getFieldValue('start_time'), value)
  210. if (duration) {
  211. // 使用k-form-design组件的form属性修改表单数据
  212. this.$refs.KFB.setData({
  213. duration: duration
  214. })
  215. }
  216. }
  217. }
  218. },
  219. // handler
  220. handleSubmit(e) {
  221. // return new Promise((resolve)=>{
  222. //通过函数获取数据
  223. this.$refs.KFB.getData()
  224. .then(res => {
  225. // 获取数据成功
  226. let formData = JSON.parse(JSON.stringify(res))
  227. console.log(formData)
  228. formData.id = this.data.id
  229. formData.procDefId = this.processData.id
  230. formData.procDeTitle = this.processData.name
  231. if (!formData.tableName) formData.tableName = this.processData.businessTable
  232. formData.filedNames = _.keys(res).join(',')
  233. formData.filedNames = getFiledNames(formData) //获取主表字段,排除子表标识
  234. var url = this.url.addApply
  235. if (!this.isNew) {
  236. url = this.url.editForm
  237. }
  238. //是否存在子表
  239. let ischild = false
  240. //循环表单字段属性判断是否属性中包含数组对象
  241. let i = 0 //子表数量
  242. _.keys(res)
  243. .join(',')
  244. .split(',')
  245. .forEach(element => {
  246. if (formData[element] instanceof Array) {
  247. //判断是否符合子表命名规则
  248. if (element.indexOf('&') != -1) {
  249. let tableChildNameList = element.split('&')
  250. //判断是否能拆分两个字符,子表标识和子表数据库名称
  251. if (tableChildNameList.length == 2) {
  252. i++
  253. let childName = tableChildNameList[1] //子表数据库名称
  254. if (!formData.table_name_children) {
  255. //第一次拼接
  256. formData.table_name_children = childName //表名
  257. } else {
  258. //后面的表明拼接用“,”隔开
  259. formData.table_name_children = formData.table_name_children + ',' + childName //表名
  260. }
  261. //定义属性名和值
  262. formData['childFiledNames' + i] = _.keys(formData[element][0]).join(',') //子表字段名
  263. formData['childList' + i] = JSON.stringify(formData[element]) //子表数据
  264. ischild = true
  265. }
  266. } else {
  267. //如果不是子表则是上传文件,则转字符串
  268. formData[element] = JSON.stringify(formData[element])
  269. }
  270. }
  271. // formData[element];
  272. })
  273. //判断需不需要存子表
  274. if (ischild) {
  275. formData.filedNames = formData.filedNames + ',table_name_children'
  276. }
  277. console.log(formData)
  278. //子表数据
  279. //调用保存接口
  280. this.btndisabled = true
  281. this.todoManageOperationObject.formData = formData
  282. this.todoManageOperationObject.url = url
  283. //非我的待办节点打卡表单界面
  284. this.postFormAction(url, formData)
  285. .then(res => {
  286. if (res.success) {
  287. this.todoManageOperationObject.isSave = true
  288. this.$message.success('保存成功!')
  289. console.log('123')
  290. this.$emit('afterSubmit', formData)
  291. this.$emit('close')
  292. // resolve(true);
  293. } else {
  294. this.$message.error(res.message)
  295. // resolve(false);
  296. }
  297. })
  298. .finally(() => {
  299. this.btndisabled = false
  300. // resolve(false);
  301. })
  302. })
  303. .catch(err => {
  304. console.log(err, '校验失败')
  305. // resolve(false);
  306. })
  307. // })
  308. },
  309. //我的待办点击通过保存专用
  310. handleSubmit2(e) {
  311. // return new Promise((resolve)=>{
  312. //通过函数获取数据
  313. this.$refs.KFB.getData()
  314. .then(res => {
  315. // 获取数据成功
  316. let formData = JSON.parse(JSON.stringify(res))
  317. console.log(formData)
  318. formData.id = this.data.id
  319. formData.procDefId = this.processData.id
  320. formData.procDeTitle = this.processData.name
  321. if (!formData.tableName) formData.tableName = this.processData.businessTable
  322. formData.filedNames = _.keys(res).join(',')
  323. formData.filedNames = getFiledNames(formData) //获取主表字段,排除子表标识
  324. var url = this.url.addApply
  325. if (!this.isNew) {
  326. url = this.url.editForm
  327. }
  328. //是否存在子表
  329. let ischild = false
  330. //循环表单字段属性判断是否属性中包含数组对象
  331. let i = 0 //子表数量
  332. _.keys(res)
  333. .join(',')
  334. .split(',')
  335. .forEach(element => {
  336. if (formData[element] instanceof Array) {
  337. //判断是否符合子表命名规则
  338. if (element.indexOf('&') != -1) {
  339. let tableChildNameList = element.split('&')
  340. //判断是否能拆分两个字符,子表标识和子表数据库名称
  341. if (tableChildNameList.length == 2) {
  342. i++
  343. let childName = tableChildNameList[1] //子表数据库名称
  344. if (!formData.table_name_children) {
  345. //第一次拼接
  346. formData.table_name_children = childName //表名
  347. } else {
  348. //后面的表明拼接用“,”隔开
  349. formData.table_name_children = formData.table_name_children + ',' + childName //表名
  350. }
  351. //定义属性名和值
  352. formData['childFiledNames' + i] = _.keys(formData[element][0]).join(',') //子表字段名
  353. formData['childList' + i] = JSON.stringify(formData[element]) //子表数据
  354. ischild = true
  355. }
  356. } else {
  357. //如果不是子表则是上传文件,则转字符串
  358. formData[element] = JSON.stringify(formData[element])
  359. }
  360. }
  361. // formData[element];
  362. })
  363. //判断需不需要存子表
  364. if (ischild) {
  365. formData.filedNames = formData.filedNames + ',table_name_children'
  366. }
  367. console.log(formData)
  368. //子表数据
  369. //调用保存接口
  370. this.btndisabled = true
  371. this.todoManageOperationObject.formData = formData
  372. this.todoManageOperationObject.url = url
  373. })
  374. .catch(err => {
  375. console.log(err, '校验失败')
  376. // resolve(false);
  377. })
  378. // })
  379. },
  380. close() {
  381. this.$emit('close')
  382. },
  383. getData() {
  384. // 通过函数获取数据
  385. this.$refs.KFB.getData()
  386. .then(res => {
  387. // 获取数据成功
  388. alert(JSON.stringify(res))
  389. })
  390. .catch(err => {
  391. console.log(err, '校验失败')
  392. })
  393. },
  394. //刷新待办列表
  395. refreshToDo() {
  396. this.close()
  397. this.$emit('getDataList')
  398. }
  399. }
  400. }
  401. //获取主表字段,排除子表标识
  402. function getFiledNames(formData) {
  403. let filedNames = ''
  404. let list = formData.filedNames.split(',') //获取表单属性名集合
  405. list.forEach(element => {
  406. if (element.indexOf('&') == -1) {
  407. //查询是否有这个字段
  408. if (filedNames == '') {
  409. //第一次则直接等于
  410. filedNames = element
  411. } else {
  412. //后面用逗号隔开
  413. filedNames = filedNames + ',' + element
  414. }
  415. }
  416. })
  417. return filedNames
  418. }
  419. //根据开始时间 结束时间计算天数
  420. function startEndFun(start, end) {
  421. if (start && end) {
  422. let startTime = new Date(start) // 开始时间
  423. let endTime = new Date(end) // 结束时间
  424. let usedTime = endTime - startTime // 相差的毫秒数
  425. let days = (usedTime / 1000 / 60 / 60).toFixed(2) // 计算出天数
  426. return days
  427. } else {
  428. return null
  429. }
  430. }
  431. //格式化时间
  432. function formatDate(date, fmt) {
  433. if (/(y+)/.test(fmt)) {
  434. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  435. }
  436. let o = {
  437. 'M+': date.getMonth() + 1,
  438. 'd+': date.getDate(),
  439. 'h+': date.getHours(),
  440. 'm+': date.getMinutes(),
  441. 's+': date.getSeconds()
  442. }
  443. for (let k in o) {
  444. if (new RegExp(`(${k})`).test(fmt)) {
  445. let str = o[k] + ''
  446. fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
  447. }
  448. }
  449. return fmt
  450. }
  451. function padLeftZero(str) {
  452. return ('00' + str).substr(str.length)
  453. }
  454. </script>
  455. <style lang="less" scoped>
  456. .form-main {
  457. }
  458. </style>