This repository was archived by the owner on Sep 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamic.js
139 lines (136 loc) · 3.44 KB
/
dynamic.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
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
Component({
/**
* 组件的属性列表
*/
properties: {
// 弹窗标题
title: {
type: String,
// 默认值
value: "微信授权"
},
appName: {
type: String,
value: ""
},
// 弹窗确认按钮文字
confirmText: {
type: String,
value: "确定"
}
},
/**
* 组件的初始数据
*/
data: {
popShow: false,
authType: "",
authTxt: ""
},
/**
* 组件的方法列表
*/
methods: {
/**
* 检测用户是否已经授权过某个权限,如果没有授权就调用小程序的授权,如果授权了就返回相应的状态给回调函数
* https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html#scope-列表
*
* scope为具体的某个权限
* cb为回调
*/
isAuthorize(scope, cb) {
let self = this;
switch (scope) {
case 'phoneNumber':
self.setAuthorize(scope, '手机号');
break;
case 'userInfo':
self.setAuthorize(scope, '用户信息');
break;
case 'userLocation':
self.setAuthorize(scope, '地理位置');
break;
case 'userLocationBackground':
self.setAuthorize(scope, '后台定位');
break;
case 'address':
self.setAuthorize(scope, '通讯地址');
break;
case 'invoiceTitle':
self.setAuthorize(scope, '发票抬头');
break;
case 'invoice':
self.setAuthorize(scope, '获取发票');
break;
case 'werun':
self.setAuthorize(scope, '微信运动步数');
break;
case 'record':
self.setAuthorize(scope, '录音功能');
break;
case 'writePhotosAlbum':
self.setAuthorize(scope, '保存到相册');
break;
case 'camera':
self.setAuthorize(scope, '摄像头');
break;
default:
console.log("scope错误,无法授权");
self.popClose();
return;
}
},
/**
* 授权
*/
setAuthorize(scope, authTxt) {
let self = this;
wx.getSetting({
success(res) {
if (res.authSetting["scope." + scope]) {
return typeof cb == "function" && cb();
}
self.setData({
popShow: true,
authType: scope,
authTxt: authTxt
})
// 判断只能使用button按钮授权方式的
if (scope != "userInfo" && scope != "phoneNumber") {
wx.authorize({
scope: 'scope.' + scope,
success(e) {
return typeof cb == "function" && cb();
},
fail(e) {
console.log(e)
}
})
}
}
})
},
/**
* 用户点击允许或拒绝
* triggerEvent 组件之间通信
*/
bindGetAuthorize: function(res) {
this.triggerEvent("bindGetAuthorize", res.detail);
if (res.detail.errMsg.search("ok") != -1) {
this.popClose();
return typeof this.callBack == "function" && this.callBack();
}
},
/**
* 控制 pop 的打开关闭
* 该方法作用有2:
* 1:点击弹窗以外的位置可消失弹窗
* 2:用到弹出或者关闭弹窗的业务逻辑时都可调用
*/
popClose() {
this.setData({
popShow: false
})
},
}
})