guoshilong
2022-10-14 d8e0af2ce345f3b40581ad05c596f965ae729c01
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
/**
 * 获取文件扩展名
 */
export function getFileSuffix(str) {
    if (str) return str.substring(str.lastIndexOf('.') + 1, str.length)
    return ''
}
 
/**
 * 文件大小单位转换
 * from:原数据
 *  to :类型 M kb b
 * */
export function getFileUnitConversion(byte, from = 'B', to = 'M') {
    from = from
        .toString()
        .toUpperCase()
        .trim();
    to = to
        .toString()
        .toUpperCase()
        .trim();
    switch (to) {
        case 'M':
            if (from == 'B') return (byte / Math.pow(1024, 2)).toFixed(2);
            if (from == 'KB') return (byte / 1024).toFixed(2);
            return (0).toFixed(2);
        case 'KB':
            if (from == 'M') return (byte * 1024).toFixed(2);
            if (from == 'B') return (byte / 1024).toFixed(2);
            return (0).toFixed(2);
        case 'B':
            if (from == 'M') return (byte * Math.pow(1024, 2)).toFixed(2);
            if (from == 'KB') return (byte * 1024).toFixed(2);
            return (0).toFixed(2);
        default:
            return (0).toFixed(2);
    }
};
 
/**
 *   图片预览
 * @param {Number} current = [value]  索引
 * @param {Array} images = [value]  图片数组
 * @param {String} oUrlKey = [value]  图片的原图地址Key
 */
export function previewImages(current, images, oUrlKey = 'url') {
    const urls = images.map(it => {
        return it[oUrlKey];
    });
    uni.previewImage({
        current,
        urls,
        loop: true,
        indicator: 'number',
        longPressActions: {
            itemList: ['保存图片'],
            success: res => {
                uni.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: res => {
                        console.log('图片保存成功');
                    }
                });
            }
        }
    });
}