我這里就用獲取微信授權運動步數的接口做案例,簡單通過,也試過使用微信保存圖片授權上,
代碼如下:
wx.getSetting({ success: res => { if(!res.authSetting['scope.werun']){ wx.authorize({ scope: 'scope.werun', // 代表的是打開步數的
// 如果你是封裝是用戶授權也行 拒絕了也可以彈起 success: res => { wx.getWeRunData({ success: res => { console.log(res) } }); }, fail: () => { // 打開設置面 用戶拒絕 if(res.authSetting['scope.writePhotosAlbum'] == undefined){ wx.showToast({ title: '授權失敗', //提示的內容, icon: 'none', //圖標, duration: 2000, //延遲時間, }); }else {
//提示一下這里官方文檔說必須是彈出帶有確定取消的窗口才可以 官方鏈接 https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/authorize.html wx.showModal({ title: '提示', //提示的標題, content: '確定授權保存圖片?', //提示的內容, showCancel: true, //是否顯示取消按鈕, cancelText: '取消', //取消按鈕的文字,默認為取消,最多 4 個字符, cancelColor: '#000000', //取消按鈕的文字顏色, confirmText: '確定', //確定按鈕的文字,默認為取消,最多 4 個字符, confirmColor: '#3CC51F', //確定按鈕的文字顏色, success: res => { if (res.confirm) { console.log('用戶點擊確定') wx.openSetting({ success: res => { wx.getWeRunData({ success: res => { console.log(res) } });
} }); } else if (res.cancel) { console.log('用戶點擊取消') } } }); } }, complete: () => {} }); } } });
這是一個小小的案例