fix:富文本多选图片上传失败的问题(同时对 token 过期更友好),对应 https://t.zsxq.com/pKpGk

pull/855/head
YunaiV 2026-01-18 15:09:55 +08:00
parent 08a4b1b289
commit 62404bef25
1 changed files with 35 additions and 102 deletions

View File

@ -6,8 +6,7 @@ import { propTypes } from '@/utils/propTypes'
import { isNumber } from '@/utils/is'
import { ElMessage } from 'element-plus'
import { useLocaleStore } from '@/store/modules/locale'
import { getRefreshToken, getTenantId } from '@/utils/auth'
import { getUploadUrl } from '@/components/UploadFile/src/useUpload'
import { useUpload } from '@/components/UploadFile/src/useUpload'
import merge from 'lodash-es/merge'
defineOptions({ name: 'Editor' })
@ -34,6 +33,10 @@ const props = defineProps({
const emit = defineEmits(['change', 'update:modelValue'])
// 使
const { httpRequest: imageHttpRequest } = useUpload(`${props.directory}-image`)
const { httpRequest: videoHttpRequest } = useUpload(`${props.directory}-video`)
// shallowRef
const editorRef = shallowRef<IDomEditor>()
@ -114,7 +117,6 @@ const editorConfig = computed((): IEditorConfig => {
},
MENU_CONF: {
['uploadImage']: {
server: getUploadUrl(),
// 2M
maxFileSize: 10 * 1024 * 1024,
// 100
@ -122,60 +124,25 @@ const editorConfig = computed((): IEditorConfig => {
// ['image/*'] []
allowedFileTypes: ['image/*'],
// http header
headers: {
Accept: '*',
Authorization: 'Bearer ' + getRefreshToken(), // 使 getRefreshToken() 使 getAccessToken() Editor 便访
'tenant-id': getTenantId()
},
// 10
timeout: 15 * 1000, // 15
// form-data fieldNamewangeditor-uploaded-image
fieldName: 'file',
//
meta: {
directory: `${props.directory}-image`
},
metaWithUrl: false,
// uppy
uppyConfig: {
onBeforeFileAdded: (newFile: any) => {
newFile.id = `${newFile.id}-${Date.now()}`
return newFile
// 使 customUpload httpRequest
async customUpload(file: File, insertFn: InsertFnType) {
try {
const res = await imageHttpRequest({
file: file as any,
onProgress: () => {},
onSuccess: () => {},
onError: () => {}
} as any)
//
const url = (res as any).data?.data || (res as any).data
insertFn(url, 'image', url)
} catch (error: any) {
ElMessage.error(error.msg || '图片上传失败')
console.error('Upload error:', error)
}
},
//
onBeforeUpload(file: File) {
// console.log(file)
return file
},
//
onProgress(progress: number) {
// progress 0-100
console.log('progress', progress)
},
onSuccess(file: File, res: any) {
console.log('onSuccess', file, res)
},
onFailed(file: File, res: any) {
alert(res.message)
console.log('onFailed', file, res)
},
onError(file: File, err: any, res: any) {
alert(err.message)
console.error('onError', file, err, res)
},
//
customInsert(res: any, insertFn: InsertFnType) {
insertFn(res.data, 'image', res.data)
}
},
['uploadVideo']: {
server: getUploadUrl(),
// 10M
maxFileSize: 1024 * 1024 * 1024,
// 100
@ -183,56 +150,22 @@ const editorConfig = computed((): IEditorConfig => {
// ['video/*'] []
allowedFileTypes: ['video/*'],
// http header
headers: {
Accept: '*',
Authorization: 'Bearer ' + getRefreshToken(), // 使 getRefreshToken() 使 getAccessToken() Editor 便访
'tenant-id': getTenantId()
},
// 30
timeout: 15 * 1000, // 15
// form-data fieldNamewangeditor-uploaded-image
fieldName: 'file',
//
meta: {
directory: `${props.directory}-video`
},
metaWithUrl: false,
// uppy
uppyConfig: {
onBeforeFileAdded: (newFile: any) => {
newFile.id = `${newFile.id}-${Date.now()}`
return newFile
// 使 customUpload httpRequest
async customUpload(file: File, insertFn: InsertFnType) {
try {
const res = await videoHttpRequest({
file: file as any,
onProgress: () => {},
onSuccess: () => {},
onError: () => {}
} as any)
//
const url = (res as any).data?.data || (res as any).data
insertFn(url, 'mp4', url)
} catch (error: any) {
ElMessage.error(error.msg || '视频上传失败')
console.error('Upload error:', error)
}
},
//
onBeforeUpload(file: File) {
// console.log(file)
return file
},
//
onProgress(progress: number) {
// progress 0-100
console.log('progress', progress)
},
onSuccess(file: File, res: any) {
console.log('onSuccess', file, res)
},
onFailed(file: File, res: any) {
alert(res.message)
console.log('onFailed', file, res)
},
onError(file: File, err: any, res: any) {
alert(err.message)
console.error('onError', file, err, res)
},
//
customInsert(res: any, insertFn: InsertFnType) {
insertFn(res.data, 'mp4', res.data)
}
}
},