DocImageUpload.vue 5.3 KB
<template>
    <div class="doc-image-upload">
        <div class="text-12 description">{{ description }}</div>
        <div class="mt-2 py-3 text-center upload-btn" @click="fileSelect">
            <doc-icon type="doc-upload" class="mr-2 text-16"/>
            <span>{{ btText }}</span>
        </div>
        <div class="flex flex-wrap mt-4 image-box">
            <div v-for="item in innerImage.filter(e => e.fileType != 'pdf')" :key="item.id"
                class="item-img">
                <DocImage :src="item.url" remove/>
            </div>
            <div v-for="item in innerImage.filter(e => e.fileType == 'pdf')" :key="item.id"
                class="item-pdf">
                <DocImage isPdf :src="item.url" name="pdf文件名称" remove/>
            </div>
        </div>
        <input type="file"
            accept=".jpg,.jpeg,.png,.pdf"
            ref="fileElem"
            style="display: none;"
            @change="handleFiles"
            :key="inputKey">
    </div>
</template>

<script>
import DocImage from './DocImage.vue'
import { showToast } from 'vant'

const testUrl = 'https://beta-tumour.zmnyjk.com/chronic-admin/file-proxy/chronic/20241125/1732502450600434289.png?e=1732610606&token=yrkyCAltqk1WVrw1ZNWUl5F5gLxE0O8LJ0Vq4hzi:RYbssXLQIEX6KTcVk5u4tWaxb9o='

export default {
    name: 'DocImageUpload',
    components:{
        DocImage
    },
    props: {
        // 上传描述
        description: { default: '温馨提示:请上传JPG、PNG格式图片,文件大小不超过5M' },
        // 传入的img数据 { id, trueDownloadUrl }
        imageData: { default: () => [] },
        // 上传按键的文字
        btText: { default: '上传图片' },
        // 图片框大小
        size: String,
        // 上传最大数量
        maxLength: { default: 1 },
        lengthMessage: { default: '已达到图片上限,请移除后再上传' }
    },
    data() {
        return {
            // 内部图片对象
            innerImage: [],
            file: null,
            spinning: false,
            inputKey: '1',
            // viewer显示
            visible: false
        }
    },
    computed: {
        fileElem() {
            return this.$refs['fileElem']
        },
        ids() {
            return this.innerImage.map(e => e.id).join(',')
        }
    },
    methods:{
        init() {
            if (this.imageData?.length) {
                this.innerImage = [...this.imageData]
            } else {
                this.innerImage = [
                    { fileType: 'pdf', id: 1, url: testUrl },
                    { fileType: 'png', id: 2, url: testUrl },
                    { fileType: 'png', id: 3, url: testUrl },
                    { fileType: 'png', id: 4, url: testUrl },
                ]
            }
        },
        fileSelect() {
            console.log(this.innerImage, this.maxLength)
            if (this.innerImage?.length >= this.maxLength) {
                showToast(this.lengthMessage)
                return
            }
            const dom = this.$refs['fileElem']
            if (dom) {
                dom.click()
            }
        },
        upload() {
            let formData = new FormData()
            formData.append('file', this.file)
            this.spinning = true
            fileUpload(formData).then(res => {
                // this.$message.success('上传成功')
                let result = res.data || {}
                this.$emit('upload', result)
                this.addImage(result)
                this.inputKey = Math.random().toString(16).substring(2, 6)
                this.$emit('change', this.ids, this.innerImage)
                this.formItemContext.onFieldChange()
            }).finally(() => {
                this.spinning = false
            })
        },
        handleFiles() {
            let files = this.$refs['fileElem'].files
            if (files.length <= 0) {
                showToast('未选中文件,请尝试重新选择')
                return
            }
            if (files[0].size / 1024 / 1024 > 5) {
                showToast('图片大小不能超过5M')
                return
            }
            this.file = files[0]
            this.upload()
        },
        addImage(data = {}) {
            if (!data.id) {
                console.warn('addImage 文件为空')
                return
            }
            if (!this.innerImage.find(e => e.id === data.id)) {
                this.innerImage.push(data)
            }
        },
        removeImage(item) {
            if (!item) return
            this.innerImage = this.innerImage.filter(e => e.id != item.id)
            this.$emit('change', this.ids, this.innerImage)
        }
    },
    watch: {
        imageData: {
            handler(value) {
                this.init()
            },
            immediate: true
        }
    }
}
</script>

<style lang="less" scoped>
.description {
    color: #A5AEBE;
}
.upload-btn {
    color: var(--van-primary-color);
    border: 1px solid #eee;
    background-color: #FAFAFA;
    border-radius: 8px;
}
.image-box {
    row-gap: 8px;
    column-gap: 12px;
    .item-img {
        width: calc(33.3vw - 8px);
    }
    .item-pdf {
        width: 100%;
    }
    >div {
        width: calc(33.3vw - 8px);
        position: relative;
        border-radius: 2px;
        img {
            object-fit: contain;
        }
    }
}
</style>