const downLoadImage = (url) => { uni.getSetting({ success: (res) => { // console.log(res.authSetting['scope.writePhotosAlbum']) let flag = res.authSetting['scope.writePhotosAlbum'] if (flag===false) { // 未授权之前没该权限 返回的时undefined 拒绝授权是false 同意是true uni.showModal({ title: '提示', content: '请先授权保存相册', success: (res) => { if (res.confirm) { uni.openSetting() } else { uni.showToast({ icon: 'none', mask: true, title: '用户取消授权' }) } } }) } else { uni.showLoading({ title:'保存中...', mask:true }) uni.downloadFile({ url, success: (res) => { if (res.statusCode === 200) { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: (res) => { console.log(res) uni.showToast({ title: '保存成功', mask: true }) }, fail: (err) => { console.log(err) uni.showToast({ title: '保存失败', mask: true, icon: 'error' }) } }); } }, fail: () => { uni.showToast({ title: '保存失败', mask: true, icon: 'error' }) } }); } } }) } //如需动态获取权限,修改res.authSetting['scope.userLocation']与scope: 'scope.userLocation'的值。 /**** userInfo:用户信息 userLocation:地理位置 address:通讯地址 werun:运动步数 record:录音 writePhotosAlbum:保存到相册 camera:摄像头 ****/ const getAuth = (auth) => { return new Promise((resolve, reject) => { wx.getSetting({ success(res) { if (!res.authSetting[`scope.${auth}`]) { wx.authorize({ scope: `scope.${auth}`, success() { resolve() }, async fail(e) { wx.hideLoading() await showText(auth,resolve, reject) } }) } else { resolve() } } }) }) } const showText = async (auth,resolve, reject)=>{ await uni.showModal({ title: '提示', content: '请前往设置打开权限', success(res) { if (res.confirm) { uni.openSetting({ async success(res) { console.log(res,res.authSetting[`scope.${auth}`]) if(!res.authSetting[`scope.${auth}`]){ return await showText(auth,resolve, reject) }else{ return resolve() } }, fail(err) { console.log(err) return reject() } }) } else if (res.cancel) { uni.showModal({ title: '提示', content: '请授权小程序权限,\n「右上角」-「设置」中开启', showCancel: false, success(res) { if (res.confirm) { return reject() } } }) } } }) } module.exports = { downLoadImage, getAuth }