import Taro from "@tarojs/taro"; export const setAuth = (getRunData: Function, refuseAuth: Function) => { Taro.getSetting({ success: res => { //第一步,检测是否有授权 - 没有授权 if (!res.authSetting['scope.werun']) { //第二步,开始授权,但这里有一个坑点(腾讯的bug),之前授权过但是是拒绝,所以会进入失败 Taro.authorize({ scope: 'scope.werun', success: () => { console.log("授权了") getRunData() }, fail: () => { Taro.showModal({ title: '提示', content: '检测到您没有打开微信运动的权限,是否去设置打开?', success: res => { if (res.confirm) { Taro.openSetting({ success: res => { if (res.authSetting['scope.werun']) { console.log("授权了") getRunData() // this.getRunData(); } else { refuseAuth() Taro.showToast({ title: '您已拒绝授权,无法获取步数', icon: 'none' }); } } }); } else { refuseAuth() Taro.showToast({ title: '您已拒绝授权,无法获取步数', icon: 'none' }); } } }) //第三步,引导用户,手动引导用户点击按钮,去设置页开启,## Modals是自定义组件 // this.$invoke('Modals', '__modalConfirm__', [ // '检测到您没有打微信运动的权限,是否去设置?', // 'openSetting', // //第四步,进入设置页的回调 - 成功 // res => { // let { authSetting } = res.detail; // if (authSetting['scope.werun']) { // this.getRunData(); // } else { // this.$invoke('Toast', '__warning__', [ // `您没有同意授权微信运动,获取步数失败` // ]); // } // }, // //第五步,点击取消按钮的回调 // () => { // this.$invoke('Toast', '__warning__', [ // `您已拒绝微信运动授权,无法获取步数` // ]); // } // ]); } }); } else { //第六步,已经授权直接进入保存逻辑 console.log("授权了") getRunData() // this.getRunData(); } } }); }