Skip to content

Commit dbb9a38

Browse files
committed
修复 form generator 组件上传文件、图片报错的问题
1 parent 9a63219 commit dbb9a38

File tree

5 files changed

+50
-7
lines changed

5 files changed

+50
-7
lines changed

src/components/generator/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,8 @@ export const selectComponents = [
499499
__slot__: {
500500
'list-type': true
501501
},
502-
action: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址
502+
// action: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址
503+
action: '/infra/file/upload', // 请求地址
503504
disabled: false,
504505
accept: '',
505506
name: 'file',

src/components/parser/Parser.vue

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script>
22
import { deepClone } from '@/utils/index'
33
import render from '@/components/render/render.js'
4+
import {getAccessToken} from "@/utils/auth";
45
56
const ruleTrigger = {
67
'el-input': 'blur',
@@ -79,10 +80,51 @@ function formBtns(h) {
7980
}
8081
8182
function renderFormItem(h, elementList) {
83+
const that = this
84+
const data = this[this.formConf.formModel]
85+
// const formRef = that.$refs[that.formConf.formRef] // 这里直接添加有问题,此时还找不到表单 $refs
8286
return elementList.map(scheme => {
8387
const config = scheme.__config__
8488
const layout = layouts[config.layout]
8589
90+
// edit by 芋道源码,解决 el-upload 上传的问题
91+
// 参考 https://github.com/JakHuang/form-generator/blob/master/src/components/parser/example/Index.vue 实现
92+
const vModel = scheme.__vModel__
93+
const val = data[vModel]
94+
if (scheme.__config__.tag === 'el-upload') {
95+
// 回显图片
96+
scheme['file-list'] = (val || []).map(url => ({ name: url, url }))
97+
// 上传地址 + 请求头
98+
scheme.action = process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload"
99+
scheme.headers = { Authorization: "Bearer " + getAccessToken() }
100+
// 注意 on-success 不能绑定箭头函数!!!
101+
scheme['on-success'] = function (response, file, fileList) {
102+
if (response.code !== 0) {
103+
return;
104+
}
105+
// 添加到 data 中
106+
const prev = data[vModel] || []
107+
this.$set(data, vModel, [
108+
...prev,
109+
response.data
110+
])
111+
// 发起表单校验
112+
that.$refs[that.formConf.formRef].validateField(vModel)
113+
}
114+
// 注意 on-remove 不能绑定箭头函数!!!
115+
scheme['on-remove'] = function (file, fileList) {
116+
// 移除从 data 中
117+
const prev = data[vModel] || []
118+
const index = prev.indexOf(file.response.data)
119+
if (index === -1) {
120+
return
121+
}
122+
prev.splice(index, 1) // 直接移除即可,无需重复 set,因为 array 是引用
123+
// 发起表单校验
124+
that.$refs[that.formConf.formRef].validateField(vModel)
125+
}
126+
}
127+
86128
if (layout) {
87129
return layout.call(this, h, scheme)
88130
}

src/main.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ import "bpmn-js/dist/assets/bpmn-font/css/bpmn-embedded.css";
6969
import Tinymce from '@/components/tinymce/index.vue'
7070
Vue.component('tinymce', Tinymce)
7171
import '@/icons'
72-
import axios from 'axios'
73-
Vue.prototype.$axios = axios
72+
import request from "@/utils/request" // 实现 form generator 使用自己定义的 axios request 对象
73+
console.log(request)
74+
Vue.prototype.$axios = request
7475
import '@/styles/index.scss'
7576

7677
/**

src/views/bpm/model/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ export default {
429429
this.$modal.confirm('是否删除该流程!!').then(function() {
430430
deleteModel(row.id).then(response => {
431431
that.getList();
432-
that.msgSuccess("删除成功");
432+
that.$modal.msgSuccess("删除成功");
433433
})
434434
}).catch(() => {});
435435
},
@@ -439,7 +439,7 @@ export default {
439439
this.$modal.confirm('是否部署该流程!!').then(function() {
440440
deployModel(row.id).then(response => {
441441
that.getList();
442-
that.msgSuccess("部署成功");
442+
that.$modal.msgSuccess("部署成功");
443443
})
444444
}).catch(() => {});
445445
},

src/views/bpm/processInstance/create.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@
5555
<script>
5656
import {getProcessDefinitionBpmnXML, getProcessDefinitionList} from "@/api/bpm/definition";
5757
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
58-
import {getForm} from "@/api/bpm/form";
5958
import {decodeFields} from "@/utils/formGenerator";
6059
import Parser from '@/components/parser/Parser'
61-
import {createProcessInstance, getMyProcessInstancePage} from "@/api/bpm/processInstance";
60+
import {createProcessInstance} from "@/api/bpm/processInstance";
6261
6362
// 流程实例的发起
6463
export default {

0 commit comments

Comments
 (0)