mp.js
2.4 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
/**
* 小程序相关权限
* @param {String} scopeValue - 权限种类,isMessage为订阅消息设置,默认false
*
*/
const TIPS_MAP = {
'userInfo': '用户信息',
'userLocation': '地理位置', //需要在manifest中配置permission。
'address': '通信地址',
'userLocationBackground': '后台定位',
'record': '录音功能',
'writePhotosAlbum': '保存到相册',
'camera': '摄像头',
'invoice': '获取发票',
'invoiceTitle': '发票抬头',
'werun': '微信步数',
'message': '订阅消息'
}
export default class MpAuth {
constructor(scopeValue) {
this.scopeValue = scopeValue
this.isMessage = scopeValue === 'message' ? true : false
}
// 检测当前请求权限是否可用。
checkAuth() {
const that = this;
return new Promise((resolve, reject) => {
uni.getSetting({
withSubscriptions: that.isMessage, //是否获取用户订阅消息的订阅状态
success: res => {
if (!that.isMessage) { // 非订阅消息
if (!res.authSetting[
`scope.${this.scopeValue}`]) { //用户未请求过此权限,后者用户拒绝了此权限
uni.authorize({ //如果没请求过,会弹窗询问。同意过,直接success。此前拒绝,直接fai
scope: `scope.${this.scopeValue}`,
success: res => { //用户同意
console.log(
`%c用户同意${TIPS_MAP[this.scopeValue]}权限1`,
'color:green;background:yellow');
resolve(1)
},
fail: err => { //用户拒绝
console.log(
`%c用户拒绝${TIPS_MAP[this.scopeValue]}权限`,
'color:green;background:yellow');
resolve(0)
that.setAuth()
}
})
} else { //用户同意授权此权限,直接调用接口
console.log(`%c用户同意${TIPS_MAP[this.scopeValue]}权限2`,
'color:green;background:yellow');
resolve(1)
}
} else { //订阅消息 TODO
}
},
fail: err => {
console.log(`%cuni.getSetting失败:`, 'color:green;background:yellow');
console.log(err);
}
})
})
}
// 引导用户开启权限
setAuth() {
uni.showModal({
title: '设置权限',
content: `当前功能需要${TIPS_MAP[this.scopeValue]}权限,是否开启?`,
confirmText: '立即授权',
success: res => {
res.confirm && uni.openSetting()
},
fail: err => {
console.log(`%cuni.showModal失败:`, 'color:green;background:yellow');
}
})
}
}