huangsijun
2025-09-22 a78c011de350b188afb03beb2f26a73f35f71986
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
<template>
    <view></view>
</template>
 
<script>
export default {
    data() {
        return {
            /*需要返回的图片*/
            imageList:[]
        };
    },
    props:['num'],
    onLoad() {},
    mounted() {
        this.chooseImageFunc();
    },
    methods: {
        /*打开相机或者相册,选择图片*/
        chooseImageFunc() {
            let self=this;
            uni.chooseImage({
                count: self.num || 9, //默认9
                sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
                sourceType: ['album','camera'], //从相册选择
                success: function(res) {
                    self.uploadFile(res.tempFilePaths);
                },
                fail:function(res){
                    self.$emit('getImgs',null);
                },
                complete:function(res){
                    
                }
            });
        },
        
        /*上传图片*/
        uploadFile: function(tempList) {
            let self = this;
            let i = 0;
            let img_length=tempList.length;
            let params = {
                token: uni.getStorageSync('token'),
                app_id: self.getAppId()
            };
            uni.showLoading({
                title:'上传中'
            })
            tempList.forEach(function(filePath, fileKey) {
                uni.uploadFile({
                    url: self.websiteUrl + '/index.php?s=/api/file.upload/image',
                    filePath: filePath,
                    name: 'iFile',
                    formData: params,
                    success: function(res) {
                        let result = typeof res.data === 'object' ? res.data : JSON.parse(res.data);
                        if (result.code === 1) {
                            self.imageList.beforeFilePath = filePath;
                            self.imageList.push(result.data);
                        }
                    },
                    complete: function() {
                        i++;
                        if (img_length === i) {
                            uni.hideLoading()
                            // 所有文件上传完成
                            self.$emit('getImgs',self.imageList);
                        }
                    }
                });
            });
        }
    }
};
</script>
 
<style></style>