perf: 优化发送文件预览

This commit is contained in:
kuaifan 2024-03-13 10:58:46 +09:00
parent d40028340c
commit bc25f5dfdf
4 changed files with 94 additions and 10 deletions

View File

@ -2473,13 +2473,18 @@ class Base
*/ */
public static function extIcon($ext) public static function extIcon($ext)
{ {
return match ($ext) { if ($ext == "docx") {
"docx" => 'images/ext/doc.png', $ext = 'doc';
"xlsx" => 'images/ext/xls.png', } elseif ($ext == "xlsx") {
"pptx" => 'images/ext/ppt.png', $ext = 'xls';
"ai", "avi", "bmp", "cdr", "doc", "eps", "gif", "mov", "mp3", "mp4", "pdf", "ppt", "pr", "psd", "rar", "svg", "tif", "txt", "xls", "zip" => 'images/ext/' . $ext . '.png', } elseif ($ext == "pptx") {
default => 'images/ext/file.png', $ext = 'ppt';
}; }
if (in_array($ext, ["ai", "avi", "bmp", "cdr", "doc", "eps", "gif", "mov", "mp3", "mp4", "pdf", "ppt", "pr", "psd", "rar", "svg", "tif", "txt", "xls", "zip"])) {
return 'images/ext/' . $ext . '.png';
} else {
return 'images/ext/file.png';
}
} }
/** /**

View File

@ -9,6 +9,7 @@
:format="uploadFormat" :format="uploadFormat"
:show-upload-list="false" :show-upload-list="false"
:max-size="maxSize" :max-size="maxSize"
:before-upload="handleBeforeUpload"
:on-progress="handleProgress" :on-progress="handleProgress"
:on-success="handleSuccess" :on-success="handleSuccess"
:on-format-error="handleFormatError" :on-format-error="handleFormatError"
@ -34,6 +35,7 @@ export default {
data() { data() {
return { return {
fileMsgCaches: {}, //
uploadFormat: [], // uploadFormat: [], //
actionUrl: $A.apiUrl('dialog/msg/sendfile'), actionUrl: $A.apiUrl('dialog/msg/sendfile'),
} }
@ -62,6 +64,67 @@ export default {
}, },
methods: { methods: {
fileMsgName(file) {
return `${file.name}::${file.size}`
},
fileMsgData(file, data = undefined) {
const cacheName = this.fileMsgName(file)
if ($A.isJson(data)) {
this.fileMsgCaches[cacheName] = Object.assign(this.fileMsgCaches[cacheName] || {}, data)
return
}
data = {
type: 'file',
thumb: null,
width: -1,
height: -1,
name: file.name,
size: file.size,
ext: file.name.split('.').pop(),
}
let {ext} = data
if (ext === 'docx') {
ext = 'doc'
} else if (ext === 'xlsx') {
ext = 'xls'
} else if (ext === 'pptx') {
ext = 'ppt'
}
if (["ai", "avi", "bmp", "cdr", "doc", "eps", "gif", "mov", "mp3", "mp4", "pdf", "ppt", "pr", "psd", "rar", "svg", "tif", "txt", "xls", "zip"].includes(ext)) {
data.thumb = $A.apiUrl(`../images/ext/${ext}.png`)
} else {
data.thumb = $A.apiUrl(`../images/ext/file.png`)
}
this.fileMsgCaches[cacheName] = data
},
handleBeforeUpload(file) {
//
return new Promise((resolve) => {
this.fileMsgData(file)
if (/\.(jpe?g|webp|png|gif)$/i.test(file.name)) {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = ({target}) => {
const image = new Image();
image.onload = () => {
this.fileMsgData(file, {
type: 'img',
thumb: target.result,
width: image.width,
height: image.height,
})
resolve();
};
image.src = target.result;
}
return
}
resolve();
});
},
handleProgress(event, file) { handleProgress(event, file) {
// //
if (file.tempId === undefined) { if (file.tempId === undefined) {
@ -70,6 +133,13 @@ export default {
} else { } else {
file.tempId = $A.randNum(1000000000, 9999999999) file.tempId = $A.randNum(1000000000, 9999999999)
} }
//
file.msg = {}
const msgName = this.fileMsgName(file)
if (this.fileMsgCaches[msgName]) {
file.msg = this.fileMsgCaches[msgName]
delete this.fileMsgCaches[msgName]
}
this.$emit('on-progress', file) this.$emit('on-progress', file)
} }
}, },

View File

@ -536,6 +536,9 @@ export default {
if (this.operateVisible) { if (this.operateVisible) {
return return
} }
if (!this.msgData.created_at) {
return;
}
this.$store.dispatch("audioPlay", this.msgData.msg.path) this.$store.dispatch("audioPlay", this.msgData.msg.path)
}, },
@ -571,10 +574,16 @@ export default {
}, },
viewFile() { viewFile() {
if (!this.msgData.created_at) {
return;
}
this.$emit("on-view-file", this.msgData) this.$emit("on-view-file", this.msgData)
}, },
downFile() { downFile() {
if (!this.msgData.created_at) {
return;
}
this.$emit("on-down-file", this.msgData) this.$emit("on-down-file", this.msgData)
}, },

View File

@ -1524,7 +1524,7 @@ export default {
dialog_id: this.dialogData.id, dialog_id: this.dialogData.id,
reply_id: this.quoteId, reply_id: this.quoteId,
reply_data: this.quoteData, reply_data: this.quoteData,
type: 'loading', type: 'record',
userid: this.userId, userid: this.userId,
msg, msg,
} }
@ -1987,9 +1987,9 @@ export default {
id: file.tempId, id: file.tempId,
dialog_id: this.dialogData.id, dialog_id: this.dialogData.id,
reply_id: this.quoteId, reply_id: this.quoteId,
type: 'loading', type: 'file',
userid: this.userId, userid: this.userId,
msg: { }, msg: file.msg || {},
} }
this.tempMsgs.push(tempMsg) this.tempMsgs.push(tempMsg)
this.msgType = '' this.msgType = ''