perf: 增强文件上传过程,通过改进的参数处理和状态管理
This commit is contained in:
parent
9809322f61
commit
06986db74f
@ -2329,17 +2329,11 @@ export default {
|
|||||||
},
|
},
|
||||||
upload() {
|
upload() {
|
||||||
// 打开文件选择框
|
// 打开文件选择框
|
||||||
this.$store.dispatch('updateMessage', {
|
this.$store.dispatch('updateMessage', {event: 'openUploader'})
|
||||||
event: 'openUploader',
|
|
||||||
data: this.getUploadParams(),
|
|
||||||
})
|
|
||||||
},
|
},
|
||||||
uploadFolder() {
|
uploadFolder() {
|
||||||
if (window.uploader.supportDirectory) {
|
if (window.uploader.supportDirectory) {
|
||||||
this.$store.dispatch('updateMessage', {
|
this.$store.dispatch('updateMessage', {event: 'uploadFolder'})
|
||||||
event: 'uploadFolder',
|
|
||||||
data: this.getUploadParams(),
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
this.$message({
|
this.$message({
|
||||||
message: '该浏览器不支持上传文件夹',
|
message: '该浏览器不支持上传文件夹',
|
||||||
|
|||||||
@ -192,6 +192,7 @@ export default {
|
|||||||
successMsg: null,
|
successMsg: null,
|
||||||
enableDragUpload: true,// 是否启用拖拽上传
|
enableDragUpload: true,// 是否启用拖拽上传
|
||||||
uploader: null,
|
uploader: null,
|
||||||
|
resolveFilesAddedPromise: null, // 用于等待获取上传参数
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
@ -218,19 +219,21 @@ export default {
|
|||||||
}
|
}
|
||||||
break
|
break
|
||||||
case 'openUploader':
|
case 'openUploader':
|
||||||
this.params = msg.data || {}
|
|
||||||
if (this.$refs.uploadBtn) {
|
if (this.$refs.uploadBtn) {
|
||||||
$('#global-uploader-btn').click()
|
$('#global-uploader-btn').click()
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case 'uploadFolder':
|
case 'uploadFolder':
|
||||||
this.params = msg.data || {}
|
|
||||||
if (this.$refs.folderBtn) {
|
if (this.$refs.folderBtn) {
|
||||||
$('#folder-uploader-btn').click()
|
$('#folder-uploader-btn').click()
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case 'onUploadParams':
|
case 'onUploadParams':
|
||||||
this.params = msg.data || {}
|
this.params = msg.data || {}
|
||||||
|
if (this.resolveFilesAddedPromise) {
|
||||||
|
this.resolveFilesAddedPromise();
|
||||||
|
this.resolveFilesAddedPromise = null;
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -311,21 +314,33 @@ export default {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
onDragenter(e) {
|
onDragenter() {
|
||||||
this.$store.dispatch('updateMessage', {event: 'getUploadParams'})
|
|
||||||
},
|
},
|
||||||
isPath(str) {
|
isPath(str) {
|
||||||
// 这个正则表达式检测路径中的斜杠字符
|
// 这个正则表达式检测路径中的斜杠字符
|
||||||
return /[/\\]/.test(str);
|
return /[/\\]/.test(str);
|
||||||
},
|
},
|
||||||
async onFilesAdded(files) {
|
async onFilesAdded(files) {
|
||||||
// 等待50ms
|
if (files.length === 0) {
|
||||||
setTimeout(async () => await this.doFilesAdded(files), 50)
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const waitForParams = new Promise(resolve => {
|
||||||
|
this.resolveFilesAddedPromise = resolve;
|
||||||
|
});
|
||||||
|
|
||||||
|
await this.$store.dispatch('updateMessage', { event: 'getUploadParams' });
|
||||||
|
|
||||||
|
await waitForParams;
|
||||||
|
|
||||||
|
await this.doFilesAdded(files);
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to add files:", error);
|
||||||
|
this.uploaderCancel();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
async doFilesAdded(files) {
|
async doFilesAdded(files) {
|
||||||
if (files.length === 0) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
let filenames = files.map(file => file.name)
|
let filenames = files.map(file => file.name)
|
||||||
const paths = Object.keys(this.uploader.filePaths)
|
const paths = Object.keys(this.uploader.filePaths)
|
||||||
paths.forEach(path => {
|
paths.forEach(path => {
|
||||||
@ -339,7 +354,6 @@ export default {
|
|||||||
filenames: filenames,
|
filenames: filenames,
|
||||||
...this.params
|
...this.params
|
||||||
}
|
}
|
||||||
console.log('上传文件', query)
|
|
||||||
const res = await api.checkExist(query)
|
const res = await api.checkExist(query)
|
||||||
if (res.data.exist) {
|
if (res.data.exist) {
|
||||||
this.$confirm('文件已存在,是否覆盖?', '提示', {
|
this.$confirm('文件已存在,是否覆盖?', '提示', {
|
||||||
|
|||||||
@ -273,7 +273,7 @@
|
|||||||
</template>
|
</template>
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
import {mapGetters} from 'vuex'
|
import { mapGetters, mapState } from 'vuex'
|
||||||
import {formatSize, formatTime} from '@/utils/number'
|
import {formatSize, formatTime} from '@/utils/number'
|
||||||
import {getElementToPageLeft, getElementToPageTop} from '@/utils/dom'
|
import {getElementToPageLeft, getElementToPageTop} from '@/utils/dom'
|
||||||
import api from '@/api/file-api'
|
import api from '@/api/file-api'
|
||||||
@ -294,6 +294,7 @@ export default {
|
|||||||
ImageViewer, IconFile,
|
ImageViewer, IconFile,
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
...mapState(['message']),
|
||||||
...mapGetters([
|
...mapGetters([
|
||||||
'name',
|
'name',
|
||||||
'avatar'
|
'avatar'
|
||||||
@ -403,6 +404,18 @@ export default {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
watch: {
|
||||||
|
message(msg) {
|
||||||
|
switch (msg.event) {
|
||||||
|
case 'getUploadParams':
|
||||||
|
this.$store.dispatch('updateMessage', {
|
||||||
|
event: 'onUploadParams',
|
||||||
|
data: this.getUploadParams(),
|
||||||
|
})
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
// 加载布局
|
// 加载布局
|
||||||
if (this.$route.query.vmode) {
|
if (this.$route.query.vmode) {
|
||||||
@ -1255,24 +1268,21 @@ export default {
|
|||||||
// 上传文件
|
// 上传文件
|
||||||
if (action.name === '上传文件') {
|
if (action.name === '上传文件') {
|
||||||
// 打开文件选择框
|
// 打开文件选择框
|
||||||
this.$store.dispatch('updateMessage', { event: 'openUploader', data: {
|
this.$store.dispatch('updateMessage', { event: 'openUploader'})
|
||||||
// 传入的参数
|
|
||||||
folder: this.$route.query.folder,
|
|
||||||
currentDirectory: encodeURI(this.path),
|
|
||||||
username: this.$store.state.user.name,
|
|
||||||
userId: this.$store.state.user.userId
|
|
||||||
}})
|
|
||||||
}
|
}
|
||||||
// 上传文件夹
|
// 上传文件夹
|
||||||
if (action.name === '上传文件夹') {
|
if (action.name === '上传文件夹') {
|
||||||
// 打开文件夹选择框
|
// 打开文件夹选择框
|
||||||
this.$store.dispatch('updateMessage', { event: 'uploadFolder', data: {
|
this.$store.dispatch('updateMessage', { event: 'uploadFolder'})
|
||||||
// 传入的参数
|
}
|
||||||
folder: this.$route.query.folder,
|
},
|
||||||
currentDirectory: encodeURI(this.path),
|
getUploadParams() {
|
||||||
username: this.$store.state.user.name,
|
return {
|
||||||
userId: this.$store.state.user.userId
|
// 传入的参数
|
||||||
}})
|
folder: this.$route.query.folder,
|
||||||
|
currentDirectory: encodeURI(this.path),
|
||||||
|
username: this.$store.state.user.name,
|
||||||
|
userId: this.$store.state.user.userId
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// 新建文件夹
|
// 新建文件夹
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user