demoForm2.vue 11 KB

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