-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
80 lines (78 loc) · 2.02 KB
/
utils.js
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
export const compress = (path, targetSize, quality, successCallback) => {
if (quality< 50) {
wx.showToast({
title: "[4005] 图片大大,请选择其他图片",
icon: "none"
})
return
}
wx.getFileSystemManager().getFileInfo({
filePath: path,
success: file => {
if (file.size < targetSize) {
successCallback(path)
return
} else {
wx.compressImage({
src: path,
quality: quality,
success: result => {
wx.getFileSystemManager().getFileInfo({
filePath: result.tempFilePath,
success: f => {
console.log("compressed: ", f.size)
if (f.size < targetSize) {
successCallback(result.tempFilePath)
return
} else {
this.compress(path, quality-10, successCallback)
}
}
})
}
})
}
}
})
}
export const saveImageToPhotosAlbum = (file) => {
wx.saveImageToPhotosAlbum({
filePath: file,
success: function(e) {
wx.showToast({
title: "保存成功,可前往【手机相册】中查看",
icon: "none",
duration: 2000
});
},
fail: function(e) {
"saveImageToPhotosAlbum:fail cancel" != e.errMsg ? wx.showModal({
content: "请打开相册权限",
confirmText: "去设置",
success: function(e) {
e.confirm && wx.openSetting()
}
}) : wx.showToast({
title: "[4003] 保存失败",
icon: "none"
});
}
});
}
export const downloadImageToPhotosAlbum = (url) => {
wx.downloadFile({
url: url,
fail: e => {
wx.showToast({
title: "[5004] 保存失败",
icon: "none"
})
},
success: result => {
const {
tempFilePath,
} = result
saveImageToPhotosAlbum(tempFilePath)
}
})
}