utils.js
5.51 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import qrcode from './qrcode';//用于绘制二维码
// 保存图片
let settingWritePhotosAlbum = false;
export const saveImg = function(url,callback) {
if (url) {
// #ifdef MP-WEIXIN
if (settingWritePhotosAlbum) {
uni.getSetting({
success: res => {
if (res.authSetting['scope.writePhotosAlbum']) {
uni.showLoading({
title: '正在下载'
});
uni.downloadFile({
url: url,
success: data => {
if (data.statusCode == 200) {
uni.saveImageToPhotosAlbum({
filePath: data.tempFilePath,
success: () => {
uni.hideLoading();
callback && callback();
uni.showToast({
title: '保存成功'
});
},
fail(e) {
uni.hideLoading();
tip({
title: '下载失败,错误原因:' + e.errMsg,
icon: "none"
});
}
});
} else {
uni.hideLoading();
uni.showToast({
title: '下载失败',
icon: "none"
});
}
},
fail(e) {
uni.hideLoading();
uni.showToast({
title: '下载失败,错误原因:' + e.errMsg,
icon: "none"
});
}
});
} else {
uni.showModal({
title: '提示',
content: '请先在设置页面打开“保存相册”使用权限',
confirmText: '去设置',
cancelText: '算了',
success: data => {
if (data.confirm) {
uni.openSetting();
}
}
});
}
}
});
} else {
settingWritePhotosAlbum = true;
uni.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
uni.showLoading({
title: '正在下载'
});
uni.downloadFile({
url: url,
success: data => {
if (data.statusCode == 200) {
uni.saveImageToPhotosAlbum({
filePath: data.tempFilePath,
success: () => {
uni.hideLoading();
callback && callback();
uni.showToast({
title: '保存成功'
});
},
fail(e) {
uni.hideLoading();
tip({
title: '下载失败,错误原因:' + e.errMsg,
icon: "none"
});
}
});
} else {
uni.hideLoading();
uni.showToast({
title: '下载失败',
icon: "none"
});
}
},
fail(e) {
uni.hideLoading();
uni.showToast({
title: '下载失败,错误原因:' + e.errMsg,
icon: "none"
});
}
});
}
});
}
// #endif
// #ifdef H5
uni.showLoading({
title: '正在下载'
});
uni.downloadFile({
url: url,
success: data => {
uni.hideLoading();
if (data.statusCode == 200) {
callback && callback();
window.open(data.tempFilePath);
} else {
uni.showToast({
title: '下载失败',
icon: "none"
});
}
},
fail(e) {
uni.hideLoading();
uni.showToast({
title: '下载失败,错误原因:' + e.errMsg,
icon: "none"
});
}
});
// #endif
// #ifdef APP-PLUS || MP-ALIPAY
uni.showLoading({
title: '正在下载'
});
uni.saveImageToPhotosAlbum({
filePath: url,
success: () => {
uni.hideLoading();
callback && callback();
uni.showToast({
title: '保存成功'
});
},
fail(e) {
uni.hideLoading();
uni.showToast({
title: '下载失败,错误原因:' + e.errMsg,
icon: "none"
});
}
});
// #endif
} else {
uni.showToast({
title: '未找到图片',
icon: 'none'
});
}
};
// 绘制二维码
export const qrcodeCanvas = function(id, code, width, height, that) {
qrcode.api.draw(code, {
ctx: uni.createCanvasContext(id, that),
width: convert_length(width),
height: convert_length(height)
})
}
function convert_length(length) {
return Math.round(uni.getSystemInfoSync().windowWidth * length / 750);
}