refactor(baInput):图片和文件上传组件请求上传接口时可以携带额外数据

This commit is contained in:
妙码生花 2022-06-29 17:22:35 +08:00
parent a2e17afed0
commit e61bc0b50b

View File

@ -23,6 +23,7 @@ import { genFileId, ElButton } from 'element-plus'
import type { UploadInstance, UploadRawFile, UploadFile } from 'element-plus'
import { useI18n } from 'vue-i18n'
import Editor from '/@/components/editor/index.vue'
import _ from 'lodash'
export default defineComponent({
name: 'baInput',
@ -176,6 +177,16 @@ export default defineComponent({
})
}
// /
const formDataAppend = (fd: FormData, data: anyObj = {}) => {
if (data && !_.isEmpty(data)) {
for (const key in data) {
fd.append(key, data[key])
}
}
return fd
}
const buildFun = new Map([
['string', sntp],
['number', sntp],
@ -395,6 +406,7 @@ export default defineComponent({
if (!file || !file.raw) return
let fd = new FormData()
fd.append('file', file.raw!)
fd = formDataAppend(fd, props.attr.data)
fileUpload(fd).then((res) => {
if (res.code == 1) {
state.lastFullUrl = res.data.file.full_url
@ -529,6 +541,7 @@ export default defineComponent({
if (!file || !file.raw) return
let fd = new FormData()
fd.append('file', file.raw!)
fd = formDataAppend(fd, props.attr.data)
fileUpload(fd).then((res) => {
if (res.code == 1) {
urlKey++
@ -633,6 +646,7 @@ export default defineComponent({
if (!file || !file.raw) return
let fd = new FormData()
fd.append('file', file.raw!)
fd = formDataAppend(fd, props.attr.data)
fileUpload(fd).then((res) => {
if (res.code == 1) {
lastFullUrl.value = res.data.file.full_url
@ -754,6 +768,7 @@ export default defineComponent({
if (!file || !file.raw) return
let fd = new FormData()
fd.append('file', file.raw!)
fd = formDataAppend(fd, props.attr.data)
fileUpload(fd).then((res) => {
if (res.code == 1) {
urlKey++