Browse Source

修复token过期导致文件上传失败的问题

master
xie.bx 2 years ago
parent
commit
84ad493bb7
  1. 8
      im-platform/src/main/resources/application.yml
  2. 2
      im-ui/src/api/httpRequest.js
  3. 25
      im-ui/src/components/chat/ChatBox.vue
  4. 58
      im-ui/src/components/common/FileUpload.vue
  5. 2
      im-ui/src/components/setting/Setting.vue
  6. 2
      im-ui/src/view/Group.vue

8
im-platform/src/main/resources/application.yml

@ -32,11 +32,11 @@ mybatis-plus:
# *.xml的具体路径 # *.xml的具体路径
- classpath*:mapper/*.xml - classpath*:mapper/*.xml
minio: minio:
endpoint: http://42.194.187.243:9001 #内网地址 endpoint: http://127.0.0.1:9001 #内网地址
public: http://42.194.187.243:9001 #外网访问地址 public: http://127.0.0.1:9001 #外网访问地址
accessKey: admin accessKey: admin
secretKey: admin123456 secretKey: 12345678
bucketName: box-im2 bucketName: box-im
imagePath: image imagePath: image
filePath: file filePath: file

2
im-ui/src/api/httpRequest.js

@ -54,7 +54,9 @@ http.interceptors.response.use(async response => {
sessionStorage.setItem("accessToken", data.accessToken); sessionStorage.setItem("accessToken", data.accessToken);
sessionStorage.setItem("refreshToken", data.refreshToken); sessionStorage.setItem("refreshToken", data.refreshToken);
// 这里需要把headers清掉,否则请求时会报错,原因暂不详... // 这里需要把headers清掉,否则请求时会报错,原因暂不详...
if(typeof response.config.data != 'object'){
response.config.headers=undefined; response.config.headers=undefined;
}
// 重新发送刚才的请求 // 重新发送刚才的请求
return http(response.config) return http(response.config)
} else { } else {

25
im-ui/src/components/chat/ChatBox.vue

@ -27,14 +27,14 @@
@click.stop="showEmotionBox()"> @click.stop="showEmotionBox()">
</div> </div>
<div title="发送图片"> <div title="发送图片">
<file-upload :action="imageAction" :maxSize="5*1024*1024" <file-upload :action="'/image/upload'" :maxSize="5*1024*1024"
:fileTypes="['image/jpeg', 'image/png', 'image/jpg', 'image/webp','image/gif']" :fileTypes="['image/jpeg', 'image/png', 'image/jpg', 'image/webp','image/gif']"
@before="onImageBefore" @success="onImageSuccess" @fail="onImageFail"> @before="onImageBefore" @success="onImageSuccess" @fail="onImageFail">
<i class="el-icon-picture-outline"></i> <i class="el-icon-picture-outline"></i>
</file-upload> </file-upload>
</div> </div>
<div title="发送文件"> <div title="发送文件">
<file-upload :action="fileAction" :maxSize="10*1024*1024" @before="onFileBefore" <file-upload :action="'/file/upload'" :maxSize="10*1024*1024" @before="onFileBefore"
@success="onFileSuccess" @fail="onFileFail"> @success="onFileSuccess" @fail="onFileFail">
<i class="el-icon-wallet"></i> <i class="el-icon-wallet"></i>
</file-upload> </file-upload>
@ -264,7 +264,7 @@
this.sendImageFile = null; this.sendImageFile = null;
}, },
onImageSuccess(data, file) { onImageSuccess(data, file) {
let msgInfo = JSON.parse(JSON.stringify(file.msgInfo || file.raw.msgInfo)); let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
msgInfo.content = JSON.stringify(data); msgInfo.content = JSON.stringify(data);
this.$http({ this.$http({
url: this.messageAction, url: this.messageAction,
@ -277,7 +277,7 @@
}) })
}, },
onImageFail(e, file) { onImageFail(e, file) {
let msgInfo = JSON.parse(JSON.stringify(file.msgInfo || file.raw.msgInfo)); let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
msgInfo.loadStatus = 'fail'; msgInfo.loadStatus = 'fail';
this.$store.commit("insertMessage", msgInfo); this.$store.commit("insertMessage", msgInfo);
}, },
@ -313,7 +313,7 @@
size: file.size, size: file.size,
url: url url: url
} }
let msgInfo = JSON.parse(JSON.stringify(file.raw.msgInfo)); let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
msgInfo.content = JSON.stringify(data); msgInfo.content = JSON.stringify(data);
this.$http({ this.$http({
url: this.messageAction, url: this.messageAction,
@ -326,7 +326,7 @@
}) })
}, },
onFileFail(e, file) { onFileFail(e, file) {
let msgInfo = JSON.parse(JSON.stringify(file.raw.msgInfo)); let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
msgInfo.loadStatus = 'fail'; msgInfo.loadStatus = 'fail';
this.$store.commit("insertMessage", msgInfo); this.$store.commit("insertMessage", msgInfo);
}, },
@ -462,7 +462,7 @@
let file = this.sendImageFile; let file = this.sendImageFile;
this.onImageBefore(this.sendImageFile); this.onImageBefore(this.sendImageFile);
let formData = new FormData() let formData = new FormData()
formData.append('file', file.raw || file) formData.append('file', file)
this.$http.post("/image/upload", formData, { this.$http.post("/image/upload", formData, {
headers: { headers: {
'Content-Type': 'multipart/form-data' 'Content-Type': 'multipart/form-data'
@ -546,6 +546,7 @@
if(this.chat.unreadCount==0){ if(this.chat.unreadCount==0){
return; return;
} }
this.$store.commit("resetUnreadCount", this.chat)
if (this.chat.type == "GROUP") { if (this.chat.type == "GROUP") {
var url = `/message/group/readed?groupId=${this.chat.targetId}` var url = `/message/group/readed?groupId=${this.chat.targetId}`
} else { } else {
@ -554,9 +555,7 @@
this.$http({ this.$http({
url: url, url: url,
method: 'put' method: 'put'
}).then(() => { }).then(() => {})
this.$store.commit("resetUnreadCount", this.chat)
})
}, },
loadGroup(groupId) { loadGroup(groupId) {
this.$http({ this.$http({
@ -631,12 +630,6 @@
} }
return title; return title;
}, },
imageAction() {
return `${process.env.VUE_APP_BASE_API}/image/upload`;
},
fileAction() {
return `${process.env.VUE_APP_BASE_API}/file/upload`;
},
messageAction() { messageAction() {
return `/message/${this.chat.type.toLowerCase()}/send`; return `/message/${this.chat.type.toLowerCase()}/send`;
}, },

58
im-ui/src/components/common/FileUpload.vue

@ -1,6 +1,6 @@
<template> <template>
<el-upload :action="action" :headers="uploadHeaders" :accept="fileTypes==null?'':fileTypes.join(',')" <el-upload :action="'#'" :http-request="onFileUpload" :accept="fileTypes==null?'':fileTypes.join(',')" :show-file-list="false"
:show-file-list="false" :on-success="onSuccess" :on-error="onError" :disabled="disabled" :before-upload="beforeUpload"> :disabled="disabled" :before-upload="beforeUpload">
<slot></slot> <slot></slot>
</el-upload> </el-upload>
</template> </template>
@ -11,13 +11,15 @@
data() { data() {
return { return {
loading: null, loading: null,
uploadHeaders: {"accessToken":sessionStorage.getItem('accessToken')} uploadHeaders: {
"accessToken": sessionStorage.getItem('accessToken')
}
} }
}, },
props: { props: {
action: { action: {
type: String, type: String,
required: true required: false
}, },
fileTypes: { fileTypes: {
type: Array, type: Array,
@ -37,17 +39,33 @@
} }
}, },
methods: { methods: {
onSuccess(res, file) { onFileUpload(file) {
this.loading && this.loading.close(); //
if (res.code == 200) { if (this.showLoading) {
this.$emit("success", res.data, file); this.loading = this.$loading({
} else { lock: true,
this.$message.error(res.message); text: '正在上传...',
this.$emit("fail", res, file); spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
});
} }
},
onError(err, file) { let formData = new FormData()
this.$emit("fail", err, file); formData.append('file', file.file)
this.$http({
url: this.action,
data: formData,
method: 'post',
headers: {
'Content-Type': 'multipart/form-data'
}
}).then((data) => {
this.$emit("success", data, file.file);
}).catch((e) => {
this.$emit("fail", e, file.file);
}).finally(() => {
this.loading && this.loading.close();
})
}, },
beforeUpload(file) { beforeUpload(file) {
// //
@ -64,20 +82,10 @@
this.$message.error(`文件大小不能超过 ${this.fileSizeStr}!`); this.$message.error(`文件大小不能超过 ${this.fileSizeStr}!`);
return false; return false;
} }
//
if (this.showLoading) {
this.loading = this.$loading({
lock: true,
text: '正在上传...',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
});
}
this.$emit("before", file); this.$emit("before", file);
return true; return true;
} }
}, },
computed: { computed: {
fileSizeStr() { fileSizeStr() {

2
im-ui/src/components/setting/Setting.vue

@ -93,7 +93,7 @@
}, },
computed:{ computed:{
imageAction(){ imageAction(){
return `${process.env.VUE_APP_BASE_API}/image/upload`; return `/image/upload`;
} }
}, },
watch: { watch: {

2
im-ui/src/view/Group.vue

@ -267,7 +267,7 @@
return this.activeGroup.ownerId == this.$store.state.userStore.userInfo.id; return this.activeGroup.ownerId == this.$store.state.userStore.userInfo.id;
}, },
imageAction() { imageAction() {
return `${process.env.VUE_APP_BASE_API}/image/upload`; return `/image/upload`;
} }
}, },
mounted() { mounted() {

Loading…
Cancel
Save