demoForm22.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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/list2',{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. formData.filedNames=formData.filedNames+',is_leaders'
  361. formData.is_leaders=this.$store.getters.userInfo.identity
  362. //非我的待办节点打卡表单界面
  363. this.postFormAction(url, formData)
  364. .then(res => {
  365. if (res.success) {
  366. this.todoManageOperationObject.isSave = true
  367. this.$message.success('保存成功!')
  368. console.log('123')
  369. this.$emit('afterSubmit', formData)
  370. this.$emit('close')
  371. // resolve(true);
  372. } else {
  373. this.$message.error(res.message)
  374. // resolve(false);
  375. }
  376. })
  377. .finally(() => {
  378. this.btndisabled = false
  379. // resolve(false);
  380. })
  381. })
  382. .catch(err => {
  383. console.log(err, '校验失败')
  384. // resolve(false);
  385. })
  386. // })
  387. },
  388. //我的待办点击通过保存专用
  389. handleSubmit2(e) {
  390. // return new Promise((resolve)=>{
  391. //通过函数获取数据
  392. this.$refs.KFB.getData()
  393. .then(res => {
  394. // 获取数据成功
  395. let formData = JSON.parse(JSON.stringify(res))
  396. console.log(formData)
  397. formData.id = this.data.id
  398. formData.procDefId = this.processData.id
  399. formData.procDeTitle = this.processData.name
  400. if (!formData.tableName) formData.tableName = this.processData.businessTable
  401. formData.filedNames = _.keys(res).join(',')
  402. formData.filedNames = getFiledNames(formData) //获取主表字段,排除子表标识
  403. var url = this.url.addApply
  404. if (!this.isNew) {
  405. url = this.url.editForm
  406. }
  407. //是否存在子表
  408. let ischild = false
  409. //循环表单字段属性判断是否属性中包含数组对象
  410. let i = 0 //子表数量
  411. _.keys(res)
  412. .join(',')
  413. .split(',')
  414. .forEach(element => {
  415. if (formData[element] instanceof Array) {
  416. //判断是否符合子表命名规则
  417. if (element.indexOf('&') != -1) {
  418. let tableChildNameList = element.split('&')
  419. //判断是否能拆分两个字符,子表标识和子表数据库名称
  420. if (tableChildNameList.length == 2) {
  421. i++
  422. let childName = tableChildNameList[1] //子表数据库名称
  423. if (!formData.table_name_children) {
  424. //第一次拼接
  425. formData.table_name_children = childName //表名
  426. } else {
  427. //后面的表明拼接用“,”隔开
  428. formData.table_name_children = formData.table_name_children + ',' + childName //表名
  429. }
  430. //定义属性名和值
  431. formData['childFiledNames' + i] = _.keys(formData[element][0]).join(',') //子表字段名
  432. formData['childList' + i] = JSON.stringify(formData[element]) //子表数据
  433. ischild = true
  434. }
  435. } else {
  436. //如果不是子表则是上传文件,则转字符串
  437. formData[element] = JSON.stringify(formData[element])
  438. }
  439. }
  440. // formData[element];
  441. })
  442. //判断需不需要存子表
  443. if (ischild) {
  444. formData.filedNames = formData.filedNames + ',table_name_children'
  445. }
  446. console.log(formData)
  447. //子表数据
  448. //调用保存接口
  449. this.btndisabled = true
  450. this.todoManageOperationObject.formData = formData
  451. this.todoManageOperationObject.url = url
  452. })
  453. .catch(err => {
  454. console.log(err, '校验失败')
  455. // resolve(false);
  456. })
  457. // })
  458. },
  459. close() {
  460. this.$emit('close')
  461. },
  462. getData() {
  463. // 通过函数获取数据
  464. this.$refs.KFB.getData()
  465. .then(res => {
  466. // 获取数据成功
  467. alert(JSON.stringify(res))
  468. })
  469. .catch(err => {
  470. console.log(err, '校验失败')
  471. })
  472. },
  473. //刷新待办列表
  474. refreshToDo() {
  475. this.close()
  476. this.$emit('getDataList')
  477. }
  478. }
  479. }
  480. //获取主表字段,排除子表标识
  481. function getFiledNames(formData) {
  482. let filedNames = ''
  483. let list = formData.filedNames.split(',') //获取表单属性名集合
  484. list.forEach(element => {
  485. if (element.indexOf('&') == -1) {
  486. //查询是否有这个字段
  487. if (filedNames == '') {
  488. //第一次则直接等于
  489. filedNames = element
  490. } else {
  491. //后面用逗号隔开
  492. filedNames = filedNames + ',' + element
  493. }
  494. }
  495. })
  496. return filedNames
  497. }
  498. //根据开始时间 结束时间计算天数
  499. function startEndFun(start, end) {
  500. let startTime=new Date(start)
  501. let endTime=new Date(end)
  502. if (startTime >= endTime) return 0;
  503. //1,分钟取整
  504. startTime = carryTime(startTime);
  505. endTime = carryTime(endTime);
  506. //2,计算总天数
  507. var totalTime = 0;//工时,天数
  508. if (startTime.getDay() == 6 || startTime.getDay() == 0) {
  509. totalTime = endTime.getDate() - startTime.getDate();
  510. } else {
  511. totalTime = Math.floor(((endTime - startTime) / (3600 * 1000)) / 24);
  512. }
  513. //3,拿初始值赋值给一个临时变量
  514. var tempStartTime = new Date();
  515. tempStartTime.setTime(startTime.getTime());
  516. //4,计算出总天数
  517. while (tempStartTime.getDate() < endTime.getDate()) {
  518. if (tempStartTime.getDay() == 6 || tempStartTime.getDay() == 0) {//周六或者周日减去
  519. totalTime--;
  520. }
  521. tempStartTime.setDate(tempStartTime.getDate() + 1);
  522. }
  523. //5,计算出总小时数
  524. var temp = 0;//工时,小时
  525. do {
  526. if (startTime.getDay() == 6 || startTime.getDay() == 0) {//周六周日
  527. startTime.setDate(startTime.getDate() + 1);
  528. //*********周六周日直接跳过,初始化为早晨9点
  529. startTime.setHours(9);
  530. startTime.setMinutes(0);
  531. continue;
  532. }
  533. if (endTime.getDay() == 6 || endTime.getDay() == 0) {//周六周日
  534. endTime.setDate(endTime.getDate() + 1);
  535. //*********周六周日直接跳过,初始化为早晨9点
  536. endTime.setHours(9);
  537. endTime.setMinutes(0);
  538. continue;
  539. }
  540. let tempMinutes = startTime.getHours() * 60 + startTime.getMinutes();
  541. //上午9点到12点半,算工时
  542. if (tempMinutes >= 9 * 60 && tempMinutes < (12 * 60 )) {
  543. temp += 0.05;
  544. }
  545. //上午14点到18点半,算工时
  546. if (tempMinutes >= 13 * 60 && tempMinutes < (18 * 60 )) {
  547. temp += 0.05;
  548. }
  549. startTime.setTime(startTime.getTime() + 0.5 * 3600 * 1000);//每次增加半个小时
  550. } while (startTime.getHours() * 60 + startTime.getMinutes() != endTime.getHours() * 60 + endTime.getMinutes())
  551. {
  552. totalTime += Math.floor(temp / 0.8);
  553. totalTime += temp % 0.8;
  554. totalTime = Math.round(totalTime * 100) / 100
  555. }
  556. var days = Math.floor(totalTime);
  557. var hours = Math.round((totalTime - days) * 100) / 10;
  558. console.log(days + '天', hours + '小时');
  559. return days * 8 + hours;
  560. // if (start && end) {
  561. // let startTime = new Date(start) // 开始时间
  562. // let endTime = new Date(end) // 结束时间
  563. // let usedTime = endTime - startTime // 相差的毫秒数
  564. // let days = (usedTime / 1000 / 60 / 60).toFixed(2) // 计算出天数
  565. // return days
  566. // } else {
  567. // return null
  568. // }
  569. }
  570. //格式化时间
  571. function formatDate(date, fmt) {
  572. if (/(y+)/.test(fmt)) {
  573. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  574. }
  575. let o = {
  576. 'M+': date.getMonth() + 1,
  577. 'd+': date.getDate(),
  578. 'h+': date.getHours(),
  579. 'm+': date.getMinutes(),
  580. 's+': date.getSeconds()
  581. }
  582. for (let k in o) {
  583. if (new RegExp(`(${k})`).test(fmt)) {
  584. let str = o[k] + ''
  585. fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
  586. }
  587. }
  588. return fmt
  589. }
  590. function padLeftZero(str) {
  591. return ('00' + str).substr(str.length)
  592. }
  593. /*
  594. * 0-15分不算工时
  595. * 15-45算半个小时
  596. * 45-60算一个小时
  597. * */
  598. function carryTime(date) {
  599. if (date.getMinutes() > 0 && date.getMinutes() < 15) {
  600. date.setMinutes(0);
  601. }
  602. if (date.getMinutes() >= 15 && date.getMinutes() < 30) {
  603. date.setMinutes(30);
  604. }
  605. if (date.getMinutes() > 30 && date.getMinutes() < 45) {
  606. date.setMinutes(30);
  607. }
  608. if (date.getMinutes() >= 45) {
  609. date.setHours(date.getHours() + 1);
  610. date.setMinutes(0);
  611. }
  612. return date;
  613. }
  614. </script>
  615. <style lang="less" scoped>
  616. .form-main {
  617. }
  618. </style>