update 优化文件上传、图片上传组件 文件列表展示文件原名便于后续处理, 完善组件删除功能

2.X
疯狂的狮子Li 3 years ago
parent fa3b642d63
commit 06b59e404c

@ -75,7 +75,8 @@ public class SysOssController extends BaseController {
SysOss oss = iSysOssService.upload(file); SysOss oss = iSysOssService.upload(file);
Map<String, String> map = new HashMap<>(2); Map<String, String> map = new HashMap<>(2);
map.put("url", oss.getUrl()); map.put("url", oss.getUrl());
map.put("fileName", oss.getFileName()); map.put("fileName", oss.getOriginalName());
map.put("ossId", oss.getOssId().toString());
return R.ok(map); return R.ok(map);
} }

@ -41,6 +41,7 @@
<script> <script>
import { getToken } from "@/utils/auth"; import { getToken } from "@/utils/auth";
import { delOss } from "@/api/system/oss";
export default { export default {
name: "FileUpload", name: "FileUpload",
@ -88,9 +89,7 @@ export default {
const list = Array.isArray(val) ? val : this.value.split(','); const list = Array.isArray(val) ? val : this.value.split(',');
// //
this.fileList = list.map(item => { this.fileList = list.map(item => {
if (typeof item === "string") { item = { name: item.name, url: item.url, ossId: item.ossId };
item = { name: item, url: item };
}
item.uid = item.uid || new Date().getTime() + temp++; item.uid = item.uid || new Date().getTime() + temp++;
return item; return item;
}); });
@ -152,12 +151,12 @@ export default {
// //
handleUploadSuccess(res) { handleUploadSuccess(res) {
if (res.code === 200) { if (res.code === 200) {
this.uploadList.push({ name: res.data.fileName, url: res.data.url }); this.uploadList.push({ name: res.data.fileName, url: res.data.url, ossId: res.data.ossId });
if (this.uploadList.length === this.number) { if (this.uploadList.length === this.number) {
this.fileList = this.fileList.concat(this.uploadList); this.fileList = this.fileList.concat(this.uploadList);
this.uploadList = []; this.uploadList = [];
this.number = 0; this.number = 0;
this.$emit("input", this.listToString(this.fileList)); this.$emit("input", this.fileList);
this.$modal.closeLoading(); this.$modal.closeLoading();
} }
} else { } else {
@ -167,26 +166,19 @@ export default {
}, },
// //
handleDelete(index) { handleDelete(index) {
let ossId = this.fileList[index].ossId;
delOss(ossId);
this.fileList.splice(index, 1); this.fileList.splice(index, 1);
this.$emit("input", this.listToString(this.fileList)); this.$emit("input", this.fileList);
}, },
// //
getFileName(name) { getFileName(name) {
if (name.lastIndexOf("/") > -1) { if (name.lastIndexOf("/") > -1) {
return name.slice(name.lastIndexOf("/") + 1); return name.slice(name.lastIndexOf("/") + 1);
} else { } else {
return ""; return name;
} }
}, },
//
listToString(list, separator) {
let strs = "";
separator = separator || ",";
for (let i in list) {
strs += list[i].url + separator;
}
return strs != "" ? strs.substr(0, strs.length - 1) : "";
}
} }
}; };
</script> </script>

@ -44,6 +44,7 @@
<script> <script>
import { getToken } from "@/utils/auth"; import { getToken } from "@/utils/auth";
import { delOss } from "@/api/system/oss";
export default { export default {
props: { props: {
@ -91,9 +92,8 @@ export default {
const list = Array.isArray(val) ? val : this.value.split(','); const list = Array.isArray(val) ? val : this.value.split(',');
// //
this.fileList = list.map(item => { this.fileList = list.map(item => {
if (typeof item === "string") { // name使ossId
item = { name: item, url: item }; item = { name: item.ossId, url: item.url, ossId: item.ossId };
}
return item; return item;
}); });
} else { } else {
@ -116,19 +116,21 @@ export default {
handleRemove(file, fileList) { handleRemove(file, fileList) {
const findex = this.fileList.map(f => f.name).indexOf(file.name); const findex = this.fileList.map(f => f.name).indexOf(file.name);
if(findex > -1) { if(findex > -1) {
let ossId = this.fileList[findex].ossId;
delOss(ossId);
this.fileList.splice(findex, 1); this.fileList.splice(findex, 1);
this.$emit("input", this.listToString(this.fileList)); this.$emit("input", this.fileList);
} }
}, },
// //
handleUploadSuccess(res) { handleUploadSuccess(res) {
if (res.code == 200) { if (res.code == 200) {
this.uploadList.push({ name: res.data.fileName, url: res.data.url }); this.uploadList.push({ name: res.data.fileName, url: res.data.url, ossId: res.data.ossId });
if (this.uploadList.length === this.number) { if (this.uploadList.length === this.number) {
this.fileList = this.fileList.concat(this.uploadList); this.fileList = this.fileList.concat(this.uploadList);
this.uploadList = []; this.uploadList = [];
this.number = 0; this.number = 0;
this.$emit("input", this.listToString(this.fileList)); this.$emit("input", this.fileList);
this.$modal.closeLoading(); this.$modal.closeLoading();
} }
} else { } else {
@ -181,15 +183,6 @@ export default {
this.dialogImageUrl = file.url; this.dialogImageUrl = file.url;
this.dialogVisible = true; this.dialogVisible = true;
}, },
//
listToString(list, separator) {
let strs = "";
separator = separator || ",";
for (let i in list) {
strs += list[i].url + separator;
}
return strs != '' ? strs.substr(0, strs.length - 1) : '';
}
} }
}; };
</script> </script>

Loading…
Cancel
Save