|
@@ -2,7 +2,7 @@
|
|
|
const props = withDefaults(
|
|
const props = withDefaults(
|
|
|
defineProps<{
|
|
defineProps<{
|
|
|
modelValue: string | any[]
|
|
modelValue: string | any[]
|
|
|
- prefixUrl?: string
|
|
|
|
|
|
|
+ previewPrefixUrl?: string
|
|
|
uploadType?: "image" | "file"
|
|
uploadType?: "image" | "file"
|
|
|
uploadUrl?: string
|
|
uploadUrl?: string
|
|
|
limit?: number // 图片数量限制
|
|
limit?: number // 图片数量限制
|
|
@@ -12,7 +12,8 @@ const props = withDefaults(
|
|
|
}>(),
|
|
}>(),
|
|
|
{
|
|
{
|
|
|
uploadType: "image",
|
|
uploadType: "image",
|
|
|
- prefixUrl: "/resource/oss/",
|
|
|
|
|
|
|
+ uploadUrl: "resource/oss/upload",
|
|
|
|
|
+ previewPrefixUrl: "resource/oss",
|
|
|
limit: 5,
|
|
limit: 5,
|
|
|
fileSize: 5,
|
|
fileSize: 5,
|
|
|
fileType: () => ["png", "jpg", "jpeg"],
|
|
fileType: () => ["png", "jpg", "jpeg"],
|
|
@@ -30,8 +31,10 @@ const uploadList = ref<any[]>([])
|
|
|
const showViewer = ref(false)
|
|
const showViewer = ref(false)
|
|
|
const viewerInitialIndex = ref(0)
|
|
const viewerInitialIndex = ref(0)
|
|
|
const baseUrl = import.meta.env.VITE_APP_BASE_API
|
|
const baseUrl = import.meta.env.VITE_APP_BASE_API
|
|
|
-const uploadImgUrl = ref(
|
|
|
|
|
- import.meta.env.VITE_APP_BASE_API + (props.uploadUrl ? props.uploadUrl : "/resource/oss/upload")
|
|
|
|
|
|
|
+const uploadImgFileUrl = ref(
|
|
|
|
|
+ import.meta.env.VITE_APP_BASE_API +
|
|
|
|
|
+ "/" +
|
|
|
|
|
+ (props.uploadUrl ? props.uploadUrl : "resource/oss/upload")
|
|
|
) // 上传的图片服务器地址
|
|
) // 上传的图片服务器地址
|
|
|
const headers = ref({
|
|
const headers = ref({
|
|
|
Authorization: "Bearer " + getToken(),
|
|
Authorization: "Bearer " + getToken(),
|
|
@@ -92,20 +95,22 @@ function handleUploadError() {
|
|
|
|
|
|
|
|
// 上传成功回调
|
|
// 上传成功回调
|
|
|
function handleUploadSuccess(res: any, file: any) {
|
|
function handleUploadSuccess(res: any, file: any) {
|
|
|
- //console.log("RES", res, file)
|
|
|
|
|
|
|
+ console.log("RES", res, file)
|
|
|
if (res.code === 200 && res.data.url) {
|
|
if (res.code === 200 && res.data.url) {
|
|
|
- let url = `${baseUrl}/${
|
|
|
|
|
- props.prefixUrl
|
|
|
|
|
- ? props.prefixUrl.lastIndexOf("/") == props.prefixUrl.length - 1
|
|
|
|
|
- ? props.prefixUrl
|
|
|
|
|
- : props.prefixUrl + "/"
|
|
|
|
|
- : ""
|
|
|
|
|
- }${res.data.url}`
|
|
|
|
|
|
|
+ let url = `${baseUrl}/${props.previewPrefixUrl ? props.previewPrefixUrl : ""}/${res.data.url}`
|
|
|
|
|
+ url = url
|
|
|
|
|
+ .replace("https://", "$$$$")
|
|
|
|
|
+ .replace("http://", "@@@@")
|
|
|
|
|
+ .replace(/\/\//g, "/")
|
|
|
|
|
+ .replace("$$$$", "https://")
|
|
|
|
|
+ .replace("@@@@", "http://")
|
|
|
|
|
+ console.log("uploadList", res.data)
|
|
|
uploadList.value.push({
|
|
uploadList.value.push({
|
|
|
- id: res.data.ossId,
|
|
|
|
|
|
|
+ url: url,
|
|
|
name: res.data.url,
|
|
name: res.data.url,
|
|
|
- realUrl: url,
|
|
|
|
|
- url: file.url
|
|
|
|
|
|
|
+ fileName: res.data.fileName,
|
|
|
|
|
+ fileId: res.data.fileId,
|
|
|
|
|
+ ossId: res.data.ossId
|
|
|
})
|
|
})
|
|
|
uploadedSuccessfully()
|
|
uploadedSuccessfully()
|
|
|
} else {
|
|
} else {
|
|
@@ -156,22 +161,13 @@ function handlePictureCardPreview(file: any) {
|
|
|
showViewer.value = true
|
|
showViewer.value = true
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// 获取文件名称
|
|
|
|
|
-function getFileName(name: string) {
|
|
|
|
|
- if (name.lastIndexOf("/") > -1) {
|
|
|
|
|
- return name.slice(name.lastIndexOf("/") + 1)
|
|
|
|
|
- } else {
|
|
|
|
|
- return ""
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
// 对象转成指定字符串分隔
|
|
// 对象转成指定字符串分隔
|
|
|
function listToString(list: any[], separator?: string) {
|
|
function listToString(list: any[], separator?: string) {
|
|
|
let str = ""
|
|
let str = ""
|
|
|
separator = separator ?? ","
|
|
separator = separator ?? ","
|
|
|
list.forEach((v) => {
|
|
list.forEach((v) => {
|
|
|
if (undefined !== v.url && v.url.indexOf("blob:") !== 0) {
|
|
if (undefined !== v.url && v.url.indexOf("blob:") !== 0) {
|
|
|
- str += v.name + separator
|
|
|
|
|
|
|
+ str += `${v.fileId}$${v.fileName}$${v.ossId}` + separator
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
return str != "" ? str.substring(0, str.length - 1) : ""
|
|
return str != "" ? str.substring(0, str.length - 1) : ""
|
|
@@ -179,67 +175,59 @@ function listToString(list: any[], separator?: string) {
|
|
|
|
|
|
|
|
function initFile(files: any) {
|
|
function initFile(files: any) {
|
|
|
if (files) {
|
|
if (files) {
|
|
|
|
|
+ let temp = 0
|
|
|
// 首先将值转为数组
|
|
// 首先将值转为数组
|
|
|
let list = Array.isArray(files) ? files : (files as string)?.split(",")
|
|
let list = Array.isArray(files) ? files : (files as string)?.split(",")
|
|
|
// 然后将数组转为对象数组
|
|
// 然后将数组转为对象数组
|
|
|
- list = list.map((item) => {
|
|
|
|
|
|
|
+ fileList.value = list.map((item) => {
|
|
|
if (typeof item === "string") {
|
|
if (typeof item === "string") {
|
|
|
- if (!item.includes(baseUrl)) {
|
|
|
|
|
- let url = `${baseUrl}/${
|
|
|
|
|
- props.prefixUrl
|
|
|
|
|
- ? props.prefixUrl.lastIndexOf("/") == props.prefixUrl.length - 1
|
|
|
|
|
- ? props.prefixUrl
|
|
|
|
|
- : props.prefixUrl + "/"
|
|
|
|
|
- : ""
|
|
|
|
|
- }${item}`
|
|
|
|
|
- item = {
|
|
|
|
|
- name: item,
|
|
|
|
|
- realUrl: url
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ const itemArr = item.split("$")
|
|
|
|
|
+ if (itemArr.length == 1) {
|
|
|
|
|
+ itemArr.push("01.pdf")
|
|
|
|
|
+ }
|
|
|
|
|
+ if (itemArr.length == 2) {
|
|
|
|
|
+ itemArr.push("0")
|
|
|
|
|
+ }
|
|
|
|
|
+ item = {
|
|
|
|
|
+ fileId: itemArr[0],
|
|
|
|
|
+ fileName: itemArr[1],
|
|
|
|
|
+ ossId: itemArr[2],
|
|
|
|
|
+ index: temp
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!itemArr[0].includes(baseUrl)) {
|
|
|
|
|
+ let url = `${baseUrl}/${props.previewPrefixUrl ? props.previewPrefixUrl : ""}/${itemArr[0]}`
|
|
|
|
|
+ url = url
|
|
|
|
|
+ .replace("https://", "$$$$")
|
|
|
|
|
+ .replace("http://", "@@@@")
|
|
|
|
|
+ .replace(/\/\//g, "/")
|
|
|
|
|
+ .replace("$$$$", "https://")
|
|
|
|
|
+ .replace("@@@@", "http://")
|
|
|
|
|
+ item.url = url
|
|
|
|
|
+ item.name = itemArr[0]
|
|
|
} else {
|
|
} else {
|
|
|
- item = { name: item, url: item }
|
|
|
|
|
|
|
+ item.url = itemArr[0]
|
|
|
|
|
+ item.name = itemArr[0]
|
|
|
}
|
|
}
|
|
|
|
|
+ item.uid = item.uid || new Date().getTime() + temp++
|
|
|
}
|
|
}
|
|
|
return item
|
|
return item
|
|
|
})
|
|
})
|
|
|
- fileList.value = []
|
|
|
|
|
- list.forEach((v: any) => {
|
|
|
|
|
- if (v.url) {
|
|
|
|
|
- fileList.value.push({
|
|
|
|
|
- name: v.name,
|
|
|
|
|
- realUrl: v.realUrl,
|
|
|
|
|
- url: v.url
|
|
|
|
|
- })
|
|
|
|
|
- } else {
|
|
|
|
|
- RequestService.get({ url: v.realUrl, loading: false, responseType: "blob" }).then((res) => {
|
|
|
|
|
- if (res.status === 200) {
|
|
|
|
|
- const blobUrl = URL.createObjectURL(res.data)
|
|
|
|
|
- fileList.value.push({
|
|
|
|
|
- name: v.name,
|
|
|
|
|
- url: blobUrl,
|
|
|
|
|
- realUrl: v.realUrl
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
- }
|
|
|
|
|
- })
|
|
|
|
|
} else {
|
|
} else {
|
|
|
fileList.value = []
|
|
fileList.value = []
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-function init() {
|
|
|
|
|
- initFile(props.modelValue)
|
|
|
|
|
-}
|
|
|
|
|
|
|
+function init() {}
|
|
|
|
|
|
|
|
onMounted(init)
|
|
onMounted(init)
|
|
|
|
|
+watch(() => props.modelValue, initFile)
|
|
|
</script>
|
|
</script>
|
|
|
<template>
|
|
<template>
|
|
|
<div class="component-upload-image">
|
|
<div class="component-upload-image">
|
|
|
<el-upload
|
|
<el-upload
|
|
|
multiple
|
|
multiple
|
|
|
ref="imageUploadRef"
|
|
ref="imageUploadRef"
|
|
|
- :action="uploadImgUrl"
|
|
|
|
|
|
|
+ :action="uploadImgFileUrl"
|
|
|
:list-type="isImage ? 'picture-card' : undefined"
|
|
:list-type="isImage ? 'picture-card' : undefined"
|
|
|
:file-list="fileList"
|
|
:file-list="fileList"
|
|
|
:limit="limit"
|
|
:limit="limit"
|
|
@@ -291,8 +279,8 @@ onMounted(init)
|
|
|
:key="file.uid"
|
|
:key="file.uid"
|
|
|
class="el-upload-list__item ele-upload-list__item-content"
|
|
class="el-upload-list__item ele-upload-list__item-content"
|
|
|
v-for="(file, index) in fileList">
|
|
v-for="(file, index) in fileList">
|
|
|
- <el-link :href="`${baseUrl}${file.url}`" :underline="false" target="_blank">
|
|
|
|
|
- <span class="el-icon-document">{{ getFileName(file.name) }}</span>
|
|
|
|
|
|
|
+ <el-link :href="`${file.url}`" :underline="false" target="_blank">
|
|
|
|
|
+ <span class="el-icon-document">{{ file.fileName }}</span>
|
|
|
</el-link>
|
|
</el-link>
|
|
|
<div class="ele-upload-list__item-content-action">
|
|
<div class="ele-upload-list__item-content-action">
|
|
|
<el-link :underline="false" @click="handleDeleteFile(index)" type="danger">删除</el-link>
|
|
<el-link :underline="false" @click="handleDeleteFile(index)" type="danger">删除</el-link>
|
|
@@ -307,6 +295,14 @@ onMounted(init)
|
|
|
:deep(.hide .el-upload--picture-card) {
|
|
:deep(.hide .el-upload--picture-card) {
|
|
|
display: none;
|
|
display: none;
|
|
|
}
|
|
}
|
|
|
|
|
+:deep(.el-link) {
|
|
|
|
|
+ width: 100%;
|
|
|
|
|
+ justify-content: flex-start;
|
|
|
|
|
+ .el-link__inner {
|
|
|
|
|
+ padding: 0 10px;
|
|
|
|
|
+ color: var(--bs-primary);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
.upload-file-uploader {
|
|
.upload-file-uploader {
|
|
|
margin-bottom: 5px;
|
|
margin-bottom: 5px;
|