2021-11-15 10:29:05 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="ai-uploader">
|
|
|
|
|
|
<div class="fileList">
|
|
|
|
|
|
<div class="item" v-for="(item, i) in fileList" :key="i">
|
|
|
|
|
|
<template v-if="type == 'image'">
|
|
|
|
|
|
<ai-image :src="item.url" :preview="preview"/>
|
2023-04-19 17:13:30 +08:00
|
|
|
|
<u-icon v-if="!disabled" class="delBtn" color="#f46" name="close-circle-fill" size="40" @click="remove(i)"/>
|
2021-11-15 10:29:05 +08:00
|
|
|
|
</template>
|
2023-04-19 17:13:30 +08:00
|
|
|
|
<template v-else-if="type == 'video'">
|
|
|
|
|
|
<video :src="item.url" :poster="item.thumb"/>
|
|
|
|
|
|
<u-icon v-if="!disabled" class="delBtn" color="#f46" name="close-circle-fill" size="40" @click="remove(i)"/>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
<div class="file" v-else>
|
2021-11-15 10:29:05 +08:00
|
|
|
|
<ai-image :preview="preview" :file="item"/>
|
|
|
|
|
|
<div class="info">
|
|
|
|
|
|
<span>{{ item.name }} </span>
|
|
|
|
|
|
<i>{{ item.fileSizeStr }}</i>
|
|
|
|
|
|
</div>
|
2023-04-19 17:13:30 +08:00
|
|
|
|
<template v-if="!disabled">
|
|
|
|
|
|
<div btn @tap="handleReUpload(i)">
|
|
|
|
|
|
重新上传
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<div btn @tap="remove(i)">
|
|
|
|
|
|
删除
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</div>
|
2021-11-15 10:29:05 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<div v-if="!disabled&&(fileList.length == 0 || (multiple && fileList.length < limit))" class="default"
|
|
|
|
|
|
@click="upload">
|
|
|
|
|
|
<i class="iconfont iconfont-iconAdd"/>
|
|
|
|
|
|
<span>{{ placeholder }}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
import {mapState} from 'vuex'
|
|
|
|
|
|
import AiImage from './AiImage'
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
name: 'AiUploader',
|
|
|
|
|
|
components: {AiImage},
|
|
|
|
|
|
props: {
|
|
|
|
|
|
limit: {default: 1}, //数量
|
|
|
|
|
|
placeholder: {default: '添加图片'}, // 文字提示
|
|
|
|
|
|
type: {default: 'image'}, // 文件类型,image还是file
|
|
|
|
|
|
multiple: {
|
|
|
|
|
|
type: Boolean,
|
|
|
|
|
|
default: false,
|
|
|
|
|
|
},
|
|
|
|
|
|
fileId: String,
|
|
|
|
|
|
mediaId: String,
|
|
|
|
|
|
def: {default: () => []},
|
2023-04-19 17:13:30 +08:00
|
|
|
|
action: String,
|
2021-11-15 10:29:05 +08:00
|
|
|
|
preview: Boolean,
|
2021-12-09 17:08:51 +08:00
|
|
|
|
size: {default: 10 * 1024 * 1024},
|
2022-02-10 16:24:29 +08:00
|
|
|
|
disabled: Boolean,
|
2024-09-23 17:41:02 +08:00
|
|
|
|
sourceType: {default: () => ['album', 'camera']},
|
2024-10-31 11:06:38 +08:00
|
|
|
|
withoutToken: {
|
|
|
|
|
|
type: Number,
|
|
|
|
|
|
default: 0,
|
|
|
|
|
|
},
|
2021-11-15 10:29:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
computed: {
|
2023-04-19 17:13:30 +08:00
|
|
|
|
...mapState(['token']),
|
|
|
|
|
|
errorImage: v => v.$cdn + 'file.png',
|
|
|
|
|
|
api() {
|
|
|
|
|
|
if (this.action) return this.action
|
|
|
|
|
|
else return {
|
|
|
|
|
|
image: '/admin/file/add',
|
|
|
|
|
|
file: '/admin/file/add',
|
|
|
|
|
|
video: '/admin/file/addVideo',
|
|
|
|
|
|
}[this.type]
|
|
|
|
|
|
}
|
2021-11-15 10:29:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
watch: {
|
|
|
|
|
|
def: {
|
|
|
|
|
|
handler(v) {
|
2021-12-01 18:09:58 +08:00
|
|
|
|
if (!!v?.toString()) {
|
2021-11-15 10:29:05 +08:00
|
|
|
|
if (this.multiple) {
|
|
|
|
|
|
this.fileList = v
|
2021-12-01 18:09:58 +08:00
|
|
|
|
} else if (v?.url) {
|
2021-11-15 10:29:05 +08:00
|
|
|
|
this.fileList = [v]
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
immediate: true,
|
2023-04-19 17:13:30 +08:00
|
|
|
|
deep: true
|
2021-11-15 10:29:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
data() {
|
|
|
|
|
|
return {
|
|
|
|
|
|
fileList: [],
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
remove(index) {
|
|
|
|
|
|
this.fileList.splice(index, 1)
|
|
|
|
|
|
this.$emit('list', this.fileList)
|
|
|
|
|
|
},
|
|
|
|
|
|
upload(wait) {
|
2022-04-28 09:51:46 +08:00
|
|
|
|
typeof wait == 'function' && wait()
|
2023-04-19 17:13:30 +08:00
|
|
|
|
let count = this.limit - (this.fileList?.length || 0)
|
|
|
|
|
|
if (count > 0) {
|
2022-04-27 14:41:35 +08:00
|
|
|
|
let params = {
|
|
|
|
|
|
count,
|
|
|
|
|
|
sizeType: ['compressed'],
|
|
|
|
|
|
sourceType: [this.sourceType].flat(),
|
|
|
|
|
|
success: (res) => {
|
|
|
|
|
|
let count = this.fileList?.length + (res.tempFiles?.length || res.tempFile ? 1 : 0)
|
|
|
|
|
|
if (count > this.limit && this.limit !== 1) {
|
|
|
|
|
|
return this.$u.toast(`不能超过${this.limit}个`)
|
|
|
|
|
|
}
|
2023-04-19 17:13:30 +08:00
|
|
|
|
if (res.tempFiles?.length > 0) {
|
|
|
|
|
|
res.tempFiles.map(this.uploadFile)
|
2022-04-27 14:41:35 +08:00
|
|
|
|
} else if (res?.tempFile) {
|
|
|
|
|
|
this.uploadFile(res.tempFile)
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
if (this.type == 'image') {
|
|
|
|
|
|
uni.chooseImage(params)
|
|
|
|
|
|
} else if (this.type == 'video') {
|
|
|
|
|
|
uni.chooseVideo(params)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
uni.chooseFile(params)
|
|
|
|
|
|
}
|
2023-04-19 17:13:30 +08:00
|
|
|
|
} else {
|
2022-04-27 14:41:35 +08:00
|
|
|
|
this.$u.toast(`不能超过${this.limit}个`)
|
2021-11-15 10:29:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
uploadFile(img) {
|
|
|
|
|
|
if (this.size > 0 && img.size > this.size) {
|
|
|
|
|
|
return this.$u.toast(`不能超过${Math.ceil(this.size / 1024 / 1024)}MB`)
|
|
|
|
|
|
}
|
|
|
|
|
|
uni.showLoading({title: '上传中'})
|
|
|
|
|
|
let formData = new FormData()
|
|
|
|
|
|
formData.append('file', img)
|
2022-02-10 16:24:29 +08:00
|
|
|
|
if (this.manual) {
|
|
|
|
|
|
this.$emit('manual', img)
|
2021-11-15 10:29:05 +08:00
|
|
|
|
uni.hideLoading()
|
2022-02-10 16:24:29 +08:00
|
|
|
|
} else {
|
2023-04-19 17:13:30 +08:00
|
|
|
|
this.$http.post(this.api, formData, {
|
2024-10-31 11:06:38 +08:00
|
|
|
|
params: {type: this.type},
|
|
|
|
|
|
withoutToken: this.withoutToken == 1 ? true : false,
|
2022-02-10 16:24:29 +08:00
|
|
|
|
}).then((res) => {
|
|
|
|
|
|
if (res?.data) {
|
|
|
|
|
|
this.$emit('data', res.data)
|
|
|
|
|
|
this.$u.toast('上传成功!')
|
2023-04-19 17:13:30 +08:00
|
|
|
|
if (!this.action) {
|
|
|
|
|
|
if (this.type == 'image') {
|
|
|
|
|
|
const [image = "",] = res.data
|
|
|
|
|
|
this.fileList.push({url: image.split(";")[0], id: image.split(";")?.[1]})
|
|
|
|
|
|
} else if (this.type == 'video') {
|
|
|
|
|
|
const [video = "", thumb = ""] = res.data
|
|
|
|
|
|
this.fileList.push({url: video.split(";")[0], id: video.split(";")?.[1], thumb: thumb.split(";")[0]})
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if (this.api == '/app/wxcp/upload/uploadFile') {
|
2022-02-10 16:24:29 +08:00
|
|
|
|
this.$emit('update:mediaId', res.data?.media?.mediaId)
|
|
|
|
|
|
this.$emit('update:fileId', res.data.file.id)
|
|
|
|
|
|
this.fileList.push(res.data.file)
|
2023-04-19 17:13:30 +08:00
|
|
|
|
} else if (this.api == '/admin/file/add2') {
|
2022-02-10 16:24:29 +08:00
|
|
|
|
let info = res.data
|
|
|
|
|
|
this.$emit('update:fileId', info?.id)
|
|
|
|
|
|
this.fileList.push(res.data)
|
2023-04-19 17:13:30 +08:00
|
|
|
|
} else if (this.api == '/admin/file/add-portrait') {
|
2022-02-10 16:24:29 +08:00
|
|
|
|
this.fileList.push({url: res.data?.split(";")?.[0], id: res.data?.split(";")?.[1]})
|
|
|
|
|
|
}
|
|
|
|
|
|
this.$emit("update:def", this.fileList)
|
|
|
|
|
|
this.$emit("list", this.fileList)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.$u.toast(res.msg)
|
2021-11-15 10:29:05 +08:00
|
|
|
|
}
|
2022-02-10 16:24:29 +08:00
|
|
|
|
}).catch(err => {
|
|
|
|
|
|
this.$u.toast(err)
|
2023-04-19 17:13:30 +08:00
|
|
|
|
}).finally(() => uni.hideLoading())
|
2022-02-10 16:24:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-15 10:29:05 +08:00
|
|
|
|
},
|
|
|
|
|
|
handleReUpload(i) {
|
|
|
|
|
|
this.upload(() => this.remove(i))
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
|
.ai-uploader {
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
line-height: normal;
|
|
|
|
|
|
margin-bottom: 16px;
|
|
|
|
|
|
|
2023-04-19 17:13:30 +08:00
|
|
|
|
::v-deep.fileList {
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
|
|
2021-11-15 10:29:05 +08:00
|
|
|
|
.item {
|
2023-04-19 17:13:30 +08:00
|
|
|
|
width: initial;
|
|
|
|
|
|
padding: 0 10px 10px 0;
|
|
|
|
|
|
position: relative;
|
2021-11-15 10:29:05 +08:00
|
|
|
|
|
2023-04-19 17:13:30 +08:00
|
|
|
|
.delBtn {
|
|
|
|
|
|
position: absolute;
|
|
|
|
|
|
right: -8px;
|
|
|
|
|
|
top: -8px;
|
|
|
|
|
|
z-index: 2;
|
|
|
|
|
|
border-radius: 50%;
|
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
background-color: #fff;
|
|
|
|
|
|
height: 40px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
image, video {
|
|
|
|
|
|
width: 29vw;
|
|
|
|
|
|
height: 29vw;
|
2021-11-15 10:29:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
i {
|
|
|
|
|
|
font-style: normal;
|
|
|
|
|
|
color: #9b9b9b;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.info {
|
|
|
|
|
|
flex: 1;
|
|
|
|
|
|
min-width: 0;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
align-items: flex-start;
|
2021-12-17 10:36:07 +08:00
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
text-overflow: ellipsis;
|
2021-11-15 10:29:05 +08:00
|
|
|
|
|
|
|
|
|
|
& > span {
|
2021-12-17 09:47:33 +08:00
|
|
|
|
flex: 1;
|
|
|
|
|
|
min-width: 0;
|
2021-11-15 10:29:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
div[btn] {
|
|
|
|
|
|
color: $uni-color-primary;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
div:nth-child(4) {
|
|
|
|
|
|
color: #f72c27;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
& > * + * {
|
|
|
|
|
|
margin-left: 20px;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.default {
|
2023-04-19 17:13:30 +08:00
|
|
|
|
width: 30vw;
|
|
|
|
|
|
height: 30vw;
|
2021-11-15 10:29:05 +08:00
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
|
border-radius: 8px;
|
|
|
|
|
|
background: #f3f4f7;
|
|
|
|
|
|
color: #89b;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
justify-content: center;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
|
|
|
|
.iconfont-iconAdd {
|
|
|
|
|
|
font-size: 64px;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
span {
|
|
|
|
|
|
display: block;
|
|
|
|
|
|
text-align: center;
|
|
|
|
|
|
font-size: 28px;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-04-19 17:13:30 +08:00
|
|
|
|
|
|
|
|
|
|
.file {
|
|
|
|
|
|
width: 100vw;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
}
|
2021-11-15 10:29:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|