mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-14 11:05:06 +08:00
修复 form generator 组件上传文件、图片报错的问题
This commit is contained in:
@ -499,7 +499,8 @@ export const selectComponents = [
|
||||
__slot__: {
|
||||
'list-type': true
|
||||
},
|
||||
action: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址
|
||||
// action: process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload", // 请求地址
|
||||
action: '/infra/file/upload', // 请求地址
|
||||
disabled: false,
|
||||
accept: '',
|
||||
name: 'file',
|
||||
|
@ -1,6 +1,7 @@
|
||||
<script>
|
||||
import { deepClone } from '@/utils/index'
|
||||
import render from '@/components/render/render.js'
|
||||
import {getAccessToken} from "@/utils/auth";
|
||||
|
||||
const ruleTrigger = {
|
||||
'el-input': 'blur',
|
||||
@ -79,10 +80,51 @@ function formBtns(h) {
|
||||
}
|
||||
|
||||
function renderFormItem(h, elementList) {
|
||||
const that = this
|
||||
const data = this[this.formConf.formModel]
|
||||
// const formRef = that.$refs[that.formConf.formRef] // 这里直接添加有问题,此时还找不到表单 $refs
|
||||
return elementList.map(scheme => {
|
||||
const config = scheme.__config__
|
||||
const layout = layouts[config.layout]
|
||||
|
||||
// edit by 芋道源码,解决 el-upload 上传的问题
|
||||
// 参考 https://github.com/JakHuang/form-generator/blob/master/src/components/parser/example/Index.vue 实现
|
||||
const vModel = scheme.__vModel__
|
||||
const val = data[vModel]
|
||||
if (scheme.__config__.tag === 'el-upload') {
|
||||
// 回显图片
|
||||
scheme['file-list'] = (val || []).map(url => ({ name: url, url }))
|
||||
// 上传地址 + 请求头
|
||||
scheme.action = process.env.VUE_APP_BASE_API + "/admin-api/infra/file/upload"
|
||||
scheme.headers = { Authorization: "Bearer " + getAccessToken() }
|
||||
// 注意 on-success 不能绑定箭头函数!!!
|
||||
scheme['on-success'] = function (response, file, fileList) {
|
||||
if (response.code !== 0) {
|
||||
return;
|
||||
}
|
||||
// 添加到 data 中
|
||||
const prev = data[vModel] || []
|
||||
this.$set(data, vModel, [
|
||||
...prev,
|
||||
response.data
|
||||
])
|
||||
// 发起表单校验
|
||||
that.$refs[that.formConf.formRef].validateField(vModel)
|
||||
}
|
||||
// 注意 on-remove 不能绑定箭头函数!!!
|
||||
scheme['on-remove'] = function (file, fileList) {
|
||||
// 移除从 data 中
|
||||
const prev = data[vModel] || []
|
||||
const index = prev.indexOf(file.response.data)
|
||||
if (index === -1) {
|
||||
return
|
||||
}
|
||||
prev.splice(index, 1) // 直接移除即可,无需重复 set,因为 array 是引用
|
||||
// 发起表单校验
|
||||
that.$refs[that.formConf.formRef].validateField(vModel)
|
||||
}
|
||||
}
|
||||
|
||||
if (layout) {
|
||||
return layout.call(this, h, scheme)
|
||||
}
|
||||
|
Reference in New Issue
Block a user