emis-sapp/components/tpx-upload/tpx-upload.vue

236 lines
4.6 KiB
Vue
Raw Normal View History

2023-12-11 09:44:17 +00:00
<template>
2023-12-12 07:00:16 +00:00
<view :style="`${customstyle};flex-direction: ${direction};`" class="up-cont">
2024-01-27 09:56:52 +00:00
<view v-for="(item, index) in list" :style="`${compItemstyle}`">
<view class="cdimg" @click="handleItem(index)">
<tpx-image v-if="fileType==fileTypeEnum.image" :src="item" :height="height" :width="width"></tpx-image>
<video v-if="fileType==fileTypeEnum.video" :src="item"></video>
<view v-if="fileType==fileTypeEnum.all">{{item}}</view>
<view v-if="!disabled" @click.stop="handleDel(index)" class="delIcon">
<u-icon name="close-circle-fill" size="32" color=""></u-icon>
</view>
</view>
</view>
2024-01-31 09:16:38 +00:00
<view v-if="list.length < maxLen">
2023-12-12 07:00:16 +00:00
<view v-if="!isSlot" @click="handleChooseFile" class="camer-btn" :style="`width:${width}rpx;height:${height}rpx;`">
<template v-if="btnText">
<u-icon name="plus" size="40"></u-icon>
<text class="pt-10">{{btnText}}</text>
</template>
<template v-else>
<view class="">
<u-icon name="camera" size="50"></u-icon>
</view>
<view class="font-22">
<text>上传</text>
<text v-if="fileType==fileTypeEnum.image">图片</text>
<text v-if="fileType==fileTypeEnum.video">视频</text>
<text v-if="fileType==fileTypeEnum.all">文件</text>
</view>
</template >
2023-12-12 00:50:19 +00:00
</view>
2023-12-18 07:32:21 +00:00
<view v-if="isSlot" @click="handleChooseFile" style="width: 100%;">
2023-12-12 05:36:04 +00:00
<slot></slot>
2023-12-12 00:50:19 +00:00
</view>
</view>
</view>
2023-12-11 09:44:17 +00:00
</template>
<script>
2023-12-12 00:50:19 +00:00
import { coreMultiUploadFile } from "@/common/upload"
import { showToast } from "@/common/untool"
2023-12-11 09:44:17 +00:00
const fileTypeEnum = {
2023-12-12 05:36:04 +00:00
all: "all",
2023-12-11 09:44:17 +00:00
image: "image",
video: "video",
}
2023-12-12 00:50:19 +00:00
2023-12-11 09:44:17 +00:00
export default {
2023-12-12 00:50:19 +00:00
computed: {
list() {
let val = this.value
let res = []
if (typeof val == "object") {
res = val
}
if (typeof val == 'string' && val !== '') {
res = val.split(",")
}
return res
},
2023-12-12 07:00:16 +00:00
compItemstyle(){
2024-01-27 09:56:52 +00:00
let style = `margin-bottom: 16rpx;margin-right: 16rpx;`
2023-12-12 07:00:16 +00:00
if(this.direction == "row") {
// style += `width:${this.width}rpx;height:${this.height}rpx;`
}
return `${this.itemstyle};${style};`
}
2023-12-12 00:50:19 +00:00
},
2023-12-11 09:44:17 +00:00
props: {
2023-12-12 05:36:04 +00:00
customstyle: {
default () {
return ''
}
},
itemstyle: {
default () {
return ''
}
},
2023-12-12 07:00:16 +00:00
width: {
default () {
return 168
}
},
height: {
default () {
return 168
}
},
direction: {
default () {
return 'row'
}
},
btnText: {
2023-12-12 05:36:04 +00:00
default () {
return ''
}
},
2023-12-11 09:44:17 +00:00
value: {
default () {
return ''
}
},
fileType: {
default () {
2023-12-12 00:50:19 +00:00
return fileTypeEnum.image
2023-12-11 09:44:17 +00:00
}
},
2023-12-12 00:50:19 +00:00
pickMaxLen: { // 一次最多选择个数
default () {
return 5
}
},
maxLen: { // 最大个数
default () {
2023-12-11 09:44:17 +00:00
return 1
2023-12-12 00:50:19 +00:00
}
2023-12-11 09:44:17 +00:00
},
2023-12-12 00:50:19 +00:00
minLen: { // 最小个数
default () {
2023-12-11 09:44:17 +00:00
return 1
}
},
2024-01-27 09:56:52 +00:00
disabled: {
default () {
return false
}
},
2023-12-12 00:50:19 +00:00
isSlot: {
default () {
return false
}
2023-12-11 09:44:17 +00:00
}
},
data() {
return {
fileTypeEnum: Object.assign({}, fileTypeEnum),
}
},
created() {
},
onHide() {
},
2024-01-30 03:09:49 +00:00
unmounted() {},
2023-12-12 00:50:19 +00:00
2023-12-11 09:44:17 +00:00
methods: {
2023-12-12 00:50:19 +00:00
doChooseFile(opt = {}) {
const that = this
const actMap = {
image: "chooseImage",
video: "chooseVideo",
}
return coreMultiUploadFile(actMap[this.fileType], opt)
},
2024-01-27 09:56:52 +00:00
handlePreview(index) {
2023-12-12 00:50:19 +00:00
uni.previewImage({
2024-01-27 09:56:52 +00:00
current: index,
2023-12-12 00:50:19 +00:00
urls: [
...this.list
]
})
},
2024-01-27 09:56:52 +00:00
handleDel(index){
this.list.splice(index, 1)
this.triggerChanged(this.list)
},
handleItem(index) {
this.handlePreview(index)
2023-12-12 00:50:19 +00:00
},
handleChooseFile: function() {
let that = this
let { maxLen, pickMaxLen, list } = this
let count = pickMaxLen
if (list.length + pickMaxLen > maxLen) {
count = maxLen - list.length
}
if (count <= 0) {
return showToast(`最多上传${maxLen}个`)
}
this.doChooseFile({
count
}).then((res) => {
list = list.concat(res)
that.triggerChanged(list)
})
},
triggerChanged(list) {
let val = list
if(this.maxLen == 1) {
val = list[0] || ''
}
2023-12-11 09:44:17 +00:00
this.$emit('change', val)
2023-12-12 00:50:19 +00:00
this.$emit('update:value', val)
2023-12-11 09:44:17 +00:00
}
}
}
</script>
<style scoped lang="scss">
2023-12-12 07:00:16 +00:00
.up-cont{
2023-12-18 07:32:21 +00:00
width: 100%;
2023-12-12 07:00:16 +00:00
display: flex;
flex-wrap: wrap;
}
.camer-btn {
2023-12-12 00:50:19 +00:00
border: 2rpx solid #EFEFEF;
border-radius: 6rpx;
background: #F8F8F8;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
2023-12-12 07:00:16 +00:00
margin-bottom: 20rpx;
2023-12-12 00:50:19 +00:00
}
2024-01-27 09:56:52 +00:00
.cdimg {
position: relative;
.delIcon{
position: absolute;
height: 46rpx;
width: 46rpx;
right: -10rpx;
top: -10rpx;
.u-icon{
justify-content: end;
}
}
}
2023-12-11 09:44:17 +00:00
</style>