移动系统liao
2025-02-17 557c2711a3e103ebc3d0492344eca9730d5e92b2
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
<template>
    <view class="compress__canvas">
        <!-- #ifndef H5 -->
        <canvas canvas-id="compress_canvas" :style="{width: width + 'px', height: height + 'px'}"></canvas>
        <!-- #endif -->
    </view>
</template>
 
<script>
/**
 * 使用方法
 * import WCompress from '@/components/w-compress/compress.js'
 * components: { WCompress }
 * <w-compress ref='wCompress' />
 * this.$refs.wCompress.start(res.tempFilePaths[0]).then().catch()
 * this.$refs.wCompress.start(res.tempFilePaths).then().catch()
 */
import compress from './compress.js'
export default {
    name: 'wCompress',
    data() {
        return {
            width: 0,
            height: 0
        }
    },
    methods: {
        start(imgUrl, options={}) {
            return new Promise(async (resolve, reject) => {
                if(imgUrl instanceof Array) {
                    try{
                        let arr = []
                        for(let i=0; i<imgUrl.length; i++) {
                            let url = await compress(imgUrl[i], this, options)
                            arr.push(url)
                        }
                        
                        resolve(arr)
                    }catch(e){
                        reject(e)
                    }
                    
                    /* let arr = []
                    arr = imgUrl.map(async c => {
                        return await compress(c, this, options)
                    })
                    resolve(arr) */
                    
                    /* let arr = imgUrl.map(c => {
                        return compress(c, this, options)
                    })
                    
                    Promise.all(arr)
                        .then(resolve)
                        .catch(reject) */
                } else {
                    compress(imgUrl, this, options)
                        .then(resolve)
                        .catch(reject)
                }
            })
        }
    }
}
</script>
 
<style>
.compress__canvas {
    position: absolute;
    left: 10000px;
    visibility: hidden;
    height: 0;
    overflow: hidden;
}
</style>