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

194 lines
3.8 KiB
Vue
Raw Normal View History

2023-12-11 09:44:17 +00:00
<template>
2023-12-12 00:50:19 +00:00
<view>
<view v-if="!isSlot" @click="handleChooseFile" class="cam" style="">
<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>
</view>
</view>
<view v-if="isSlot" @click="handleChooseFile">
<slot></slot>
</view>
<view v-for="(item) in list" style="margin-top: 10rpx;">
<view class="cdimg" @click="handleItem" data-index="{{index}}">
<tpx-image v-if="fileType==fileTypeEnum.image" :src="item" :mode="mode" :scale="scale"></tpx-image>
<video v-if="fileType==fileTypeEnum.video" :src="item"></video>
</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 00:50:19 +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-11 09:44:17 +00:00
props: {
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
}
},
scale: {
default () {
return [1, 1] // 宽高比
}
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() {
},
destroyed() {},
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)
},
handlePreview() {
uni.previewImage({
urls: [
...this.list
]
})
},
handleItem(e) {
const that = this
const index = e.currentTarget.dataset.index
let { list, fileType } = this
let itemList = ['更换', '删除', '预览']
if (fileType == fileTypeEnum.video) {
itemList.length--
}
uni.showActionSheet({
itemList,
success: (res) => {
const {
tapIndex
} = res
if (tapIndex == 0) {
// 更换, 只能选择一个文件
that.doChooseFile({
count: 1
}).then((res) => {
list[index] = res[0]
that.triggerChanged(list)
})
}
if (tapIndex == 1) {
list.splice(index, 1)
that.triggerChanged(list)
}
if (tapIndex == 2) {
this.handlePreview()
}
},
fail: (res) => {
console.log(res.errMsg)
}
})
},
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 00:50:19 +00:00
.cam {
height: 168rpx;
width: 168rpx;
border: 2rpx solid #EFEFEF;
border-radius: 6rpx;
background: #F8F8F8;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
2023-12-11 09:44:17 +00:00
</style>