forked from llgululu/wechat-wallpaper-mini-program
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.js
More file actions
125 lines (125 loc) · 2.91 KB
/
common.js
File metadata and controls
125 lines (125 loc) · 2.91 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
module.exports = {
BASE_URL: 'https://你的域名/wallpaper',
header: {
'content-type': 'application/json;charset=utf-8'
},
savePosterPath(url) {
uni.showLoading({
title: '正在保存图片...'
});
//获取用户的当前设置。获取相册权限
uni.getSetting({
success: (res) => {
//如果没有相册权限
if (!res.authSetting["scope.writePhotosAlbum"]) {
//向用户发起授权请求
uni.authorize({
scope: "scope.writePhotosAlbum",
success: () => {
//授权成功保存图片到系统相册
uni.saveImageToPhotosAlbum({
//图片路径,不支持网络图片路径
filePath: url,
success: (res) => {
uni.hideLoading();
return uni.showToast({
title: "保存成功!",
});
},
fail: (res) => {
console.log(res.errMsg);
return uni.showToast({
title: res.errMsg,
});
},
complete: (res) => {
uni.hideLoading();
},
});
},
//授权失败
fail: () => {
uni.hideLoading();
uni.showModal({
title: "您已拒绝获取相册权限",
content: "是否进入权限管理,调整授权?",
success: (res) => {
if (res.confirm) {
//调起客户端小程序设置界面,返回用户设置的操作结果。(重新让用户授权)
uni.openSetting({
success: (res) => {
console.log(res
.authSetting
);
},
});
} else if (res.cancel) {
return uni.showToast({
title: "已取消!",
});
}
},
});
},
});
} else {
//如果已有相册权限,直接保存图片到系统相册
uni.saveImageToPhotosAlbum({
filePath: url,
success: (res) => {
uni.hideLoading();
return uni.showToast({
title: "保存成功!",
});
},
fail: (res) => {
uni.hideLoading();
console.log(res.errMsg);
return uni.showToast({
title: res.errMsg,
});
},
//无论成功失败都走的回调
complete: (res) => {
uni.hideLoading();
},
});
}
},
fail: (res) => {},
});
},
_getRequest(option) {
return new Promise((resolve, reject) => {
uni.request({
url: this.BASE_URL + option.url,
method: option.method || 'GET',
header: this.header,
data: option.data,
success(res) {
resolve(res)
},
fail(err) {
reject(err.errMsg)
}
})
})
},
_updateFile(option) {
return new Promise((resolve, reject) => {
uni.uploadFile({
url: this.BASE_URL + option.url,
filePath: option.filePath,
name: option.fileName,
header: this.header,
formData: option.formData,
success(res) {
resolve(res)
},
fail(res) {
reject(res)
}
});
})
}
}