uni-auth 1.0.0

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 ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "uni-auth",
3
+ "version": "1.0.0",
4
+ "main": "src/index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": [],
9
+ "author": "",
10
+ "license": "ISC",
11
+ "description": "UniApp 授权模块"
12
+ }
package/src/index.js ADDED
@@ -0,0 +1,12 @@
1
+ export { default as appAuth } from './util/app.js'
2
+ export { default as wxAuth } from './util/wx.js'
3
+
4
+ export async function ifUserAuth(code, isWebView = false) {
5
+ // #ifdef APP-PLUS
6
+ return await appAuth(code, isWebView)
7
+ // #endif
8
+ // #ifdef MP-WEIXIN
9
+ // return await wxAuth(code)
10
+ // #endif
11
+ return true
12
+ }
@@ -0,0 +1,76 @@
1
+ import { hintOpenAuth } from './common.js'
2
+
3
+ const appName = uni.getSystemInfoSync().appName // 获取应用名称
4
+ const MESSAGE = {
5
+ ios: {
6
+ 相册: `允许${appName}访问您的相册/相机权限来获取您的照片用于上传您的图片和相册等信息`,
7
+ 相机: `允许${appName}访问您的相册/相机权限及摄像头来方便用户拍摄上传图片等信息`,
8
+ 麦克风: `允许${appName}访问您的麦克风来获取您的声音用于录音等信息`,
9
+ },
10
+ android: {
11
+ 相册: `允许${appName}访问您的相册/相机,您可以上传/拍摄照片及视频;`,
12
+ 相机: `允许${appName}访问您的相册/相机,您可以上传/拍摄照片及视频`,
13
+ 麦克风: `允许${appName}访问您的麦克风来获取您的声音用于录音等信息`,
14
+ 设备信息: `允许${appName}访问您设备的唯一标识,用于数据统计和分析`,
15
+ 电话: `允许${appName}访问您设备的电话,用于联系客服`,
16
+ 位置: `允许${appName}访问您的位置,用于为你提供下单配送服务`,
17
+ },
18
+ }
19
+
20
+ export function appAuth(code = '相册') {
21
+ return new Promise((resolve, reject) => {
22
+ const platform = uni.getSystemInfoSync().platform // ios/android
23
+ if (platform === 'ios') {
24
+ const permissionInfo = uni.getAppAuthorizeSetting()
25
+ const authStatus = permissionInfo[APP[platform][code]]
26
+ switch (authStatus) {
27
+ case 'authorized':
28
+ resolve(true)
29
+ break
30
+ case 'not determined':
31
+ hintOpenAuth({ code, platform, content: MESSAGE[platform][code] }).then((res) => resolve(res))
32
+ break
33
+ case 'denied':
34
+ hintOpenAuth({ code, platform, content: MESSAGE[platform][code] }).then((res) => resolve(res))
35
+ break
36
+ default:
37
+ break
38
+ }
39
+ } else if (platform === 'android') {
40
+ console.log(plus.navigator.checkPermission())
41
+ const authStatus = plus.navigator.checkPermission(APP[platform][code])
42
+ if (code === '设备信息' && !HANDLE_READ_PHONE_STATE()) return resolve(false)
43
+ switch (authStatus) {
44
+ case 'authorized': //已开启
45
+ resolve(true)
46
+ break
47
+ // case 'denied': //被拒绝
48
+ // case 'undetermined': //未请求授权
49
+ // case 'unknown': //无法查询权限
50
+ default:
51
+ hintOpenAuth({ code, platform, content: MESSAGE[platform][code] }).then((res) => {
52
+ if (!res) return resolve(res)
53
+ else {
54
+ //granted通过/deniedPresent临时拒绝/deniedAlways永久拒绝
55
+ plus.android.requestPermissions([APP[platform][code]], (result) => {
56
+ if (result.granted && result.granted.length > 0) resolve(true)
57
+ else {
58
+ getApp().globalData.$toast(`请在设置中开启${code}权限`)
59
+ resolve(false)
60
+ }
61
+ })
62
+ }
63
+ })
64
+ break
65
+ }
66
+ }
67
+ })
68
+ }
69
+
70
+ function HANDLE_READ_PHONE_STATE() {
71
+ const res = uni.getStorageSync('auth') || 1
72
+ if (Number(res) === 1) {
73
+ uni.setStorageSync('auth', 2)
74
+ return true
75
+ } else return false
76
+ }
@@ -0,0 +1,16 @@
1
+ export function hintOpenAuth(params = {}) {
2
+ const { code, platform = 'android', content = '', isOpenAuth = false } = params || {}
3
+ return new Promise((resolve, reject) => {
4
+ uni.showModal({
5
+ content: content,
6
+ showCancel: platform !== 'ios',
7
+ cancelText: '拒绝',
8
+ confirmText: platform === 'ios' ? '下一步' : '同意',
9
+ success: (res) => {
10
+ if (!res.confirm) return resolve(false)
11
+ resolve(true)
12
+ isOpenAuth && uni.openAppAuthorizeSetting()
13
+ },
14
+ })
15
+ })
16
+ }
package/src/util/wx.js ADDED
@@ -0,0 +1,59 @@
1
+ import { hintOpenAuth } from './common.js'
2
+
3
+ const weixin = {
4
+ 相册: 'scope.writePhotosAlbum',
5
+ 相机: 'scope.writePhotosAlbum',
6
+ 麦克风: 'scope.record',
7
+ }
8
+
9
+ function wxAuth(auth = '相册') {
10
+ const param = weixin[auth]
11
+ return new Promise((resolve, reject) => {
12
+ 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' }))
18
+ } else {
19
+ if (res.authSetting[param]) resolve(true)
20
+ else wxRequest(param, auth).then((res) => resolve(res))
21
+ }
22
+ },
23
+ fail: () => {
24
+ getApp().globalData.$toast('请稍后再试...')
25
+ },
26
+ })
27
+ })
28
+ }
29
+
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('本次操作需要此授权'))
54
+ },
55
+ })
56
+ },
57
+ })
58
+ })
59
+ }