/** * 请求权限 * @param {String} scope 权限标识 * @param {String} tip 拒绝后的提示文案 */ export function requestAuth(scope, tip) { return new Promise((resolve, reject) => { // #ifdef APP-PLUS || MP-WEIXIN uni.getSetting({ success: (res) => { console.log(res) // 1. 已授权 if (res.authSetting[scope]) { resolve(true) return } // 2. 未申请过,发起授权弹窗 uni.authorize({ scope, success: () => resolve(true), fail: () => { // 3. 用户拒绝,引导去设置页开启 uni.showModal({ title: '权限申请', content: tip, confirmText: '去设置', cancelText: '取消', success: (modal) => { if (modal.confirm) { uni.openSetting({ success: (setRes) => { resolve(setRes.authSetting[scope]) }, fail: () => reject(false) }) } else { reject(false) } } }) } }) }, fail: () => reject(false) }) // #endif // #ifdef H5 resolve(true) // #endif }) }