Houjie
2025-04-11 1bf977929dd324f3ac64b70debd8a79443c54392
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<template>
    <view class="margin-top">
        <view class="cu-bar bg-white ">
            <view class="action">
                {{label}}
            </view>
            <view class="action">
                {{imgList.length}}/{{maxImg}}
            </view>
        </view>
        <view class="cu-form-group">
            <view class="grid col-4 grid-square flex-sub">
                <view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
                    <image :src="imgList[index]" mode="aspectFill"></image>
                    <view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
                        <text class='cuIcon-close'></text>
                    </view>
                </view>
                <view class="solids" @tap="ChooseImage" v-if="imgList.length<maxImg">
                    <text class='cuIcon-cameraadd'></text>
                </view>
            </view>
        </view>
    </view>
</template>
 
<script>
    
    export default {
        name: 'MyImageUpoad',
        props: {
            value: {type:String,default:''},
            label:{type:String,default:'图片上传'},
            maxImg: {
                type: Number,
                default: 3
            },
 
        },
        mounted:function(){
            if (this.value.split(',')!=''){
                this.value.split(',').forEach(res=>{
                    this.imgList.push(baseurl+res)
                })
            }
        },
        data() {
            return {
                imgList: [],
                pathlist:[],
            }
        },
        methods: {
            ChooseImage() {
                uni.chooseImage({
                    count: this.maxImg, //默认9
                    sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
                    sourceType: ['album','camera'], //从相册选择
                    success: (res) => {
                        uni.uploadFile({
                            url: `${baseurl}systemController/filedeal.do?isup=1`,
                            filePath: res.tempFilePaths[0],
                            name: 'file',
                            success: (uploadFileRes) => {
                                let path = JSON.parse(uploadFileRes.data).obj
                                this.pathlist.push(path);
                                this.$emit('input',this.pathlist.join(','))
                                if (this.imgList.length != 0) {
                                    this.imgList = this.imgList.concat(res.tempFilePaths)
                                } else {
                                    this.imgList = res.tempFilePaths
                                }
                            }
                        })
 
                    }
                });
            },
            ViewImage(e) {
                uni.previewImage({
                    urls: this.imgList,
                    current: e.currentTarget.dataset.url
                });
            },
            DelImg(e) {
                uni.showModal({
                    title: '提示',
                    content: '确认要删除吗',
                    cancelText: '取消',
                    confirmText: '确认',
                    success: res => {
                        if (res.confirm) {
                            this.pathlist.splice(e.currentTarget.dataset.index,1)
                            this.imgList.splice(e.currentTarget.dataset.index, 1)
                            this.$emit('input',this.pathlist.join(','))
                        }
                    }
                })
            },
        }
    }
</script>
 
<style>
 
</style>