demoForm2.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. import { postAction } from '@/api/manage'
  57. export default {
  58. name: 'demoForm',
  59. props: {
  60. /*全局禁用,可表示查看*/
  61. disabled: {
  62. type: Boolean,
  63. default: false,
  64. required: false
  65. },
  66. /*流程数据*/
  67. processData: {
  68. type: Object,
  69. default: () => {
  70. return {}
  71. },
  72. required: false
  73. },
  74. /*是否新增*/
  75. isNew: { type: Boolean, default: false, required: false }
  76. },
  77. components: {
  78. todoManageOperation
  79. },
  80. data() {
  81. return {
  82. todoManageOperationObject: {
  83. isSave: false,
  84. formData: {}
  85. },
  86. disabled2: false, //用来取决于表单时不同流程节点填写不同表单信息使用
  87. jsonData: {},
  88. url: {
  89. getForm: '/actBusiness/getForm',
  90. addApply: '/actBusiness/add',
  91. editForm: '/actBusiness/editForm',
  92. leaveType: '/sys/dict/getDictItems'
  93. },
  94. description: '流程表单demo,按例开发表单。需在 activitiMixin.js 中加入写好的表单',
  95. // form
  96. form: this.$form.createForm(this),
  97. /*表单回显数据*/
  98. data: {},
  99. btndisabled: false,
  100. ceshi_file: null,
  101. dynamicData: {
  102. typeData: [], //请假类型下拉数据
  103. reimbursementTypeList: [], //报销类型下拉数据
  104. invoiceTypeList: [] //发票类型下拉数据
  105. }
  106. }
  107. },
  108. created() {
  109. console.log('流程数据', this.processData)
  110. //加载一些下拉选择数据等
  111. // this.getDataList()
  112. let businessTable = ''
  113. //如果没有值则在另一个属性上有值
  114. if (!this.processData.businessTable) {
  115. this.processData.businessTable = this.processData.tableName
  116. }
  117. this.getAction('/tbTableInfo/query', {
  118. businessTable: this.processData.businessTable,
  119. taskNodeId: this.processData.key
  120. }).then(res => {
  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. var jsonString = JSON.stringify(res.result.jsonContent)
  126. // var jsonString='123d"dynamicKey":"666"'
  127. var index = 1
  128. var dynamicKeyValueList = []
  129. for (var index = 1; index !== -1; ) {
  130. //动态字符位置
  131. index = jsonString.indexOf('"dynamicKey"', index)
  132. if (index !== -1) {
  133. //从指定字符的周后一个字符后开始找(")
  134. var ihStart = jsonString.indexOf('"', index + 12)
  135. //再从 (") 后面位置还是找下一个 (")
  136. var ihEnd = jsonString.indexOf('"', ihStart + 1)
  137. //获取引号之间的字符
  138. var dynamicKeyValue = jsonString.substring(ihStart + 1, ihEnd)
  139. //如果动态表示不为空则获取
  140. if (dynamicKeyValue != '') {
  141. dynamicKeyValueList.push(dynamicKeyValue)
  142. }
  143. index = ihEnd + 1
  144. }
  145. }
  146. //循环动态数据源表示获取数据
  147. // if(dynamicKeyValueList.length>0){
  148. // dynamicKeyValueList.forEach(async element => {
  149. // await this.getDataListByName(element);
  150. // });
  151. // }
  152. console.log(dynamicKeyValueList)
  153. if (dynamicKeyValueList.length > 0) {
  154. //获取数据字典数据源
  155. postAction('/sys/dict/getDictItems/getDictList', dynamicKeyValueList).then(dictData => {
  156. if (dictData.success) {
  157. this.dynamicData = dictData.result.dicList
  158. //获取用户下拉数据
  159. this.getAction('/sys/user/list',{pageSize:20000}).then(userData => {
  160. if (res.success) {
  161. userData.result.records.forEach(user => {
  162. user.label = user.realname
  163. user.value = user.username
  164. })
  165. //存到动态数据源中
  166. this.dynamicData.userList = userData.result.records
  167. //获取json
  168. this.jsonData = res.result.jsonContent
  169. if (!this.isNew) {
  170. this.init()
  171. }
  172. } else {
  173. this.$message.error(userData.message)
  174. }
  175. })
  176. } else {
  177. this.$message.error(dictData.message)
  178. }
  179. })
  180. } else {
  181. //获取json
  182. this.jsonData = res.result.jsonContent
  183. if (!this.isNew) {
  184. this.init()
  185. }
  186. }
  187. //后台检测到流程节点有关联表单且前台处于我的待办界面打开表单详情
  188. if (res.result.isSave && this.processData.isSuspended != undefined) {
  189. this.disabled = false
  190. this.btndisabled = false
  191. }
  192. })
  193. },
  194. methods: {
  195. //根据字典名字获取数据源
  196. getDataListByName(name) {
  197. this.getAction(this.url.leaveType + '/' + name).then(res => {
  198. if (res.success) {
  199. this.dynamicData[name] = res.result
  200. console.log(111)
  201. } else {
  202. this.$message.error(res.message)
  203. }
  204. })
  205. },
  206. //获取获取下拉数据等
  207. getDataList() {
  208. //获取请假类型下拉数据
  209. this.getAction(this.url.leaveType + '/leave_type').then(res => {
  210. if (res.success) {
  211. this.dynamicData.typeData = res.result
  212. } else {
  213. this.$message.error(res.message)
  214. }
  215. })
  216. //获取报销类型下拉数据
  217. this.getAction(this.url.leaveType + '/reimbursement_type').then(res => {
  218. if (res.success) {
  219. this.dynamicData.reimbursementTypeList = res.result
  220. } else {
  221. this.$message.error(res.message)
  222. }
  223. })
  224. //获取发票类型下拉数据
  225. this.getAction(this.url.leaveType + '/invoice_type').then(res => {
  226. if (res.success) {
  227. this.dynamicData.invoiceTypeList = res.result
  228. } else {
  229. this.$message.error(res.message)
  230. }
  231. })
  232. },
  233. /*回显数据*/
  234. init() {
  235. this.btndisabled = true
  236. var r = this.processData
  237. this.getAction(this.url.getForm, {
  238. tableId: r.tableId,
  239. tableName: r.tableName
  240. }).then(res => {
  241. if (res.success) {
  242. let formData = res.result
  243. formData.tableName = r.tableName
  244. this.data = formData
  245. console.log('表单回显数据', this.data)
  246. console.log(this.data)
  247. this.$refs.KFB.setData(this.data)
  248. // this.$nextTick(() => {
  249. // this.$refs.KFB.form.setFieldsValue(pick(this.data, 'name'))
  250. // })
  251. this.btndisabled = false
  252. } else {
  253. this.$message.error(res.message)
  254. }
  255. })
  256. },
  257. //表单字段数值发生改变事件
  258. handleChange(value, key) {
  259. // 数据变化时触发
  260. //如果开始时间发生变化
  261. if (key == 'start_time') {
  262. //判断结束时间是否存在
  263. if (this.$refs.KFB.form.getFieldValue('end_time')) {
  264. //获取时间差
  265. let duration = startEndFun(value, this.$refs.KFB.form.getFieldValue('end_time'))
  266. if (duration) {
  267. // 使用k-form-design组件的form属性修改表单数据
  268. this.$refs.KFB.setData({
  269. duration: duration
  270. })
  271. }
  272. }
  273. }
  274. //如果结束时间发生变化
  275. if (key == 'end_time') {
  276. //判断结束时间是否存在
  277. if (this.$refs.KFB.form.getFieldValue('start_time')) {
  278. //获取时间差
  279. let duration = startEndFun(this.$refs.KFB.form.getFieldValue('start_time'), value)
  280. if (duration) {
  281. // 使用k-form-design组件的form属性修改表单数据
  282. this.$refs.KFB.setData({
  283. duration: duration
  284. })
  285. }
  286. }
  287. }
  288. },
  289. // handler
  290. handleSubmit(e) {
  291. // return new Promise((resolve)=>{
  292. //通过函数获取数据
  293. this.$refs.KFB.getData()
  294. .then(res => {
  295. //清除为空的表单数据
  296. _.keys(res).forEach(r => {
  297. if (!res[r]) {
  298. delete res[r]
  299. }
  300. })
  301. // 获取数据成功
  302. let formData = JSON.parse(JSON.stringify(res))
  303. console.log(formData)
  304. formData.id = this.data.id
  305. formData.procDefId = this.processData.id
  306. formData.procDeTitle = this.processData.name
  307. if (!formData.tableName) formData.tableName = this.processData.businessTable
  308. formData.filedNames = _.keys(res).join(',')
  309. formData.filedNames = getFiledNames(formData) //获取主表字段,排除子表标识
  310. var url = this.url.addApply
  311. if (!this.isNew) {
  312. url = this.url.editForm
  313. }
  314. //是否存在子表
  315. let ischild = false
  316. //循环表单字段属性判断是否属性中包含数组对象
  317. let i = 0 //子表数量
  318. _.keys(res)
  319. .join(',')
  320. .split(',')
  321. .forEach(element => {
  322. if (formData[element] instanceof Array) {
  323. //判断是否符合子表命名规则
  324. if (element.indexOf('&') != -1) {
  325. let tableChildNameList = element.split('&')
  326. //判断是否能拆分两个字符,子表标识和子表数据库名称
  327. if (tableChildNameList.length == 2) {
  328. i++
  329. let childName = tableChildNameList[1] //子表数据库名称
  330. if (!formData.table_name_children) {
  331. //第一次拼接
  332. formData.table_name_children = childName //表名
  333. } else {
  334. //后面的表明拼接用“,”隔开
  335. formData.table_name_children = formData.table_name_children + ',' + childName //表名
  336. }
  337. //定义属性名和值
  338. formData['childFiledNames' + i] = _.keys(formData[element][0]).join(',') //子表字段名
  339. formData['childList' + i] = JSON.stringify(formData[element]) //子表数据
  340. ischild = true
  341. }
  342. } else {
  343. //如果不是子表则是上传文件,则转字符串
  344. formData[element] = JSON.stringify(formData[element])
  345. }
  346. }
  347. // formData[element];
  348. })
  349. //判断需不需要存子表
  350. if (ischild) {
  351. formData.filedNames = formData.filedNames + ',table_name_children'
  352. }
  353. console.log(formData)
  354. //子表数据
  355. //调用保存接口
  356. this.btndisabled = true
  357. this.todoManageOperationObject.formData = formData
  358. this.todoManageOperationObject.url = url
  359. //非我的待办节点打卡表单界面
  360. this.postFormAction(url, formData)
  361. .then(res => {
  362. if (res.success) {
  363. this.todoManageOperationObject.isSave = true
  364. this.$message.success('保存成功!')
  365. console.log('123')
  366. this.$emit('afterSubmit', formData)
  367. this.$emit('close')
  368. // resolve(true);
  369. } else {
  370. this.$message.error(res.message)
  371. // resolve(false);
  372. }
  373. })
  374. .finally(() => {
  375. this.btndisabled = false
  376. // resolve(false);
  377. })
  378. })
  379. .catch(err => {
  380. console.log(err, '校验失败')
  381. // resolve(false);
  382. })
  383. // })
  384. },
  385. //我的待办点击通过保存专用
  386. handleSubmit2(e) {
  387. // return new Promise((resolve)=>{
  388. //通过函数获取数据
  389. this.$refs.KFB.getData()
  390. .then(res => {
  391. // 获取数据成功
  392. let formData = JSON.parse(JSON.stringify(res))
  393. console.log(formData)
  394. formData.id = this.data.id
  395. formData.procDefId = this.processData.id
  396. formData.procDeTitle = this.processData.name
  397. if (!formData.tableName) formData.tableName = this.processData.businessTable
  398. formData.filedNames = _.keys(res).join(',')
  399. formData.filedNames = getFiledNames(formData) //获取主表字段,排除子表标识
  400. var url = this.url.addApply
  401. if (!this.isNew) {
  402. url = this.url.editForm
  403. }
  404. //是否存在子表
  405. let ischild = false
  406. //循环表单字段属性判断是否属性中包含数组对象
  407. let i = 0 //子表数量
  408. _.keys(res)
  409. .join(',')
  410. .split(',')
  411. .forEach(element => {
  412. if (formData[element] instanceof Array) {
  413. //判断是否符合子表命名规则
  414. if (element.indexOf('&') != -1) {
  415. let tableChildNameList = element.split('&')
  416. //判断是否能拆分两个字符,子表标识和子表数据库名称
  417. if (tableChildNameList.length == 2) {
  418. i++
  419. let childName = tableChildNameList[1] //子表数据库名称
  420. if (!formData.table_name_children) {
  421. //第一次拼接
  422. formData.table_name_children = childName //表名
  423. } else {
  424. //后面的表明拼接用“,”隔开
  425. formData.table_name_children = formData.table_name_children + ',' + childName //表名
  426. }
  427. //定义属性名和值
  428. formData['childFiledNames' + i] = _.keys(formData[element][0]).join(',') //子表字段名
  429. formData['childList' + i] = JSON.stringify(formData[element]) //子表数据
  430. ischild = true
  431. }
  432. } else {
  433. //如果不是子表则是上传文件,则转字符串
  434. formData[element] = JSON.stringify(formData[element])
  435. }
  436. }
  437. // formData[element];
  438. })
  439. //判断需不需要存子表
  440. if (ischild) {
  441. formData.filedNames = formData.filedNames + ',table_name_children'
  442. }
  443. console.log(formData)
  444. //子表数据
  445. //调用保存接口
  446. this.btndisabled = true
  447. this.todoManageOperationObject.formData = formData
  448. this.todoManageOperationObject.url = url
  449. })
  450. .catch(err => {
  451. console.log(err, '校验失败')
  452. // resolve(false);
  453. })
  454. // })
  455. },
  456. close() {
  457. this.$emit('close')
  458. },
  459. getData() {
  460. // 通过函数获取数据
  461. this.$refs.KFB.getData()
  462. .then(res => {
  463. // 获取数据成功
  464. alert(JSON.stringify(res))
  465. })
  466. .catch(err => {
  467. console.log(err, '校验失败')
  468. })
  469. },
  470. //刷新待办列表
  471. refreshToDo() {
  472. this.close()
  473. this.$emit('getDataList')
  474. }
  475. }
  476. }
  477. //获取主表字段,排除子表标识
  478. function getFiledNames(formData) {
  479. let filedNames = ''
  480. let list = formData.filedNames.split(',') //获取表单属性名集合
  481. list.forEach(element => {
  482. if (element.indexOf('&') == -1) {
  483. //查询是否有这个字段
  484. if (filedNames == '') {
  485. //第一次则直接等于
  486. filedNames = element
  487. } else {
  488. //后面用逗号隔开
  489. filedNames = filedNames + ',' + element
  490. }
  491. }
  492. })
  493. return filedNames
  494. }
  495. //根据开始时间 结束时间计算天数
  496. function startEndFun(start, end) {
  497. if (start && end) {
  498. let startTime = new Date(start) // 开始时间
  499. let endTime = new Date(end) // 结束时间
  500. let usedTime = endTime - startTime // 相差的毫秒数
  501. let days = (usedTime / 1000 / 60 / 60).toFixed(2) // 计算出天数
  502. return days
  503. } else {
  504. return null
  505. }
  506. }
  507. //格式化时间
  508. function formatDate(date, fmt) {
  509. if (/(y+)/.test(fmt)) {
  510. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  511. }
  512. let o = {
  513. 'M+': date.getMonth() + 1,
  514. 'd+': date.getDate(),
  515. 'h+': date.getHours(),
  516. 'm+': date.getMinutes(),
  517. 's+': date.getSeconds()
  518. }
  519. for (let k in o) {
  520. if (new RegExp(`(${k})`).test(fmt)) {
  521. let str = o[k] + ''
  522. fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
  523. }
  524. }
  525. return fmt
  526. }
  527. function padLeftZero(str) {
  528. return ('00' + str).substr(str.length)
  529. }
  530. </script>
  531. <style lang="less" scoped>
  532. .form-main {
  533. }
  534. </style>