uni-auth 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "uni-auth",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
4
4
  "main": "src/index.js",
5
5
  "type": "module",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -7,7 +7,7 @@ export async function ifUserAuth(code) {
7
7
  return await appAuth(code)
8
8
  // #endif
9
9
  // #ifdef MP-WEIXIN
10
- // return await wxAuth(code)
10
+ return await wxAuth(code)
11
11
  // #endif
12
12
  return true
13
13
  }
package/src/util/wx.js CHANGED
@@ -1,59 +1,108 @@
1
- import { hintOpenAuth } from '../common/index.js'
2
-
1
+ // 修正后的权限映射
3
2
  const weixin = {
4
3
  相册: 'scope.writePhotosAlbum',
5
- 相机: 'scope.writePhotosAlbum',
4
+ 相机: 'scope.camera',
6
5
  麦克风: 'scope.record',
6
+ 位置: 'scope.userLocation',
7
+ 通讯录: 'scope.addressBook',
7
8
  }
8
9
 
9
- function wxAuth(auth = '相册') {
10
+ /**
11
+ * 微信权限申请统一方法
12
+ * @param {string} auth - 权限类型:'相册'|'相机'|'麦克风'|'位置'|'通讯录'
13
+ * @returns {Promise<boolean>} - true: 有权限, false: 无权限
14
+ */
15
+ export function wxAuth(auth = '相册') {
10
16
  const param = weixin[auth]
11
- return new Promise((resolve, reject) => {
17
+
18
+ // 参数校验
19
+ if (!param) {
20
+ console.error(`未知的权限类型: ${auth}`)
21
+ return Promise.reject(new Error('权限类型不存在'))
22
+ }
23
+
24
+ return new Promise((resolve) => {
25
+ // 通常用 resolve 而不是 reject,避免调用方需要写 catch
12
26
  uni.getSetting({
13
- success(res) {
14
- if (auth === '麦克风') {
15
- if (res.authSetting[param]) resolve(true)
16
- if (res.authSetting[param] === undefined) resolve({ auth: false, status: 'determined' })
17
- else wxRequest(param, auth).then((res) => resolve({ auth: res, status: 'determined' }))
27
+ success: (res) => {
28
+ const authSetting = res.authSetting[param]
29
+
30
+ // 情况1:已授权
31
+ if (authSetting === true) {
32
+ resolve(true)
33
+ return
34
+ }
35
+
36
+ // 情况2:未授权(包括从未请求过 和 已拒绝过)
37
+ // 对于麦克风等敏感权限,可以特殊处理
38
+ if (auth === '麦克风' && authSetting === false) {
39
+ // 已拒绝过麦克风,直接引导去设置
40
+ showPermissionGuide(auth, param, resolve)
18
41
  } else {
19
- if (res.authSetting[param]) resolve(true)
20
- else wxRequest(param, auth).then((res) => resolve(res))
42
+ // 其他情况先尝试请求授权
43
+ requestPermission(param, auth, resolve)
21
44
  }
22
45
  },
23
- fail: () => {
24
- getApp().globalData.$toast('请稍后再试...')
46
+ fail: (err) => {
47
+ console.error('获取权限状态失败', err)
48
+ getApp().globalData.$toast('无法获取权限状态,请稍后重试')
49
+ resolve(false)
25
50
  },
26
51
  })
27
52
  })
28
53
  }
29
54
 
30
- function wxRequest(param, auth) {
31
- return new Promise((resolve, reject) => {
32
- uni.authorize({
33
- scope: param,
34
- success: (res) => {
35
- resolve(true)
36
- },
37
- fail: (err) => {
38
- uni.showModal({
39
- title: '提示',
40
- content: `需要授权${auth}权限保存`,
41
- success: (res) => {
42
- if (res.confirm) {
43
- uni.openSetting({
44
- success(res) {
45
- if (res.authSetting[param]) resolve(true)
46
- else reject(false)
47
- },
48
- fail: (res) => {
49
- getApp().globalData.$toast('本次操作需要此授权')
50
- reject(false)
51
- },
52
- })
53
- } else (reject(false), getApp().globalData.$toast('本次操作需要此授权'))
55
+ /**
56
+ * 请求权限
57
+ */
58
+ function requestPermission(param, auth, resolve) {
59
+ uni.authorize({
60
+ scope: param,
61
+ success: () => {
62
+ // 授权成功
63
+ resolve(true)
64
+ },
65
+ fail: (err) => {
66
+ console.log('授权失败', err)
67
+ // 授权失败,引导去设置页
68
+ showPermissionGuide(auth, param, resolve)
69
+ },
70
+ })
71
+ }
72
+
73
+ /**
74
+ * 显示权限引导弹窗
75
+ */
76
+ function showPermissionGuide(auth, param, resolve) {
77
+ uni.showModal({
78
+ title: '权限申请',
79
+ content: `需要您授权${auth}权限才能使用此功能,是否前往设置页面开启?`,
80
+ confirmText: '去设置',
81
+ success: (modalRes) => {
82
+ if (modalRes.confirm) {
83
+ // 用户确认,打开设置页
84
+ uni.openSetting({
85
+ success: (settingRes) => {
86
+ if (settingRes.authSetting[param]) {
87
+ // 用户在设置页开启了权限
88
+ getApp().globalData.$toast('授权成功,请重试')
89
+ resolve(true)
90
+ } else {
91
+ // 用户没开启
92
+ getApp().globalData.$toast(`未获得${auth}权限`)
93
+ resolve(false)
94
+ }
95
+ },
96
+ fail: () => {
97
+ getApp().globalData.$toast('打开设置失败')
98
+ resolve(false)
54
99
  },
55
100
  })
56
- },
57
- })
101
+ } else {
102
+ // 用户取消
103
+ getApp().globalData.$toast(`需要${auth}权限才能继续`)
104
+ resolve(false)
105
+ }
106
+ },
58
107
  })
59
108
  }