小程序用户拒绝授权后的处理方式,效果较好,亲测

本文详细介绍了微信小程序中各种权限的使用方法,包括用户信息、地理位置、通讯地址等,并提供了具体的代码示例,帮助开发者理解如何在小程序中正确申请和使用这些权限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//微信对应的权限
scope.userInfo wx.getUserInfo 用户信息
scope.userLocation wx.getLocation, wx.chooseLocation 地理位置
scope.userLocationBackground wx.startLocationUpdateBackground 后台定位
scope.address wx.chooseAddress 通讯地址
scope.invoiceTitle wx.chooseInvoiceTitle 发票抬头
scope.invoice wx.chooseInvoice 获取发票
scope.werun wx.getWeRunData 微信运动步数
scope.record wx.startRecord 录音功能
scope.writePhotosAlbum wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum 保存到相册
scope.camera camera 组件 摄像头

chooseAddress: function(){
    // 获取用户是否开启用户
    wx.getSetting({
      success(res) {
        //唤醒是否授权
        if (!res.authSetting['scope.address']) {
          wx.chooseAddress({
            success(res) {
              //调用成功若不授权则提示授权
            },
            fail(res){
              wx.showModal({
                title: '请您授权通讯地址',
                content: '如需正常使用此小程序功能,请您按确定并在设置页面授权通讯地址',
                confirmText: '确定',
                success: res => {
                  if (res.confirm) {
                    wx.openSetting({
                      success: function (res) {
                        if (res.authSetting['scope.address']) { //设置允许获取摄像头
                          console.log('设置允许获取微信发票抬头服务')
                          wx.showToast({
                            title: '授权成功',
                            icon: 'success',
                            duration: 1000
                          });
                        } else { //不允许
                          wx.showToast({
                            title: '授权失败',
                            icon: 'none',
                            duration: 1000
                          })
                        }
                      }
                    })
                  } else { //取消
                    wx.showToast({
                      title: '授权失败',
                      icon: 'none',
                      duration: 1000
                    })
                  }
                }
              })
            }
          });     
        } else {
          wx.chooseAddress({
            success(res) {
              console.log("选择地址", res);
              thePage.setData({
                mail_contacts: res.userName,
                mail_contacts_number: res.telNumber,
                mail_address: res.detailInfo,
                mail_provinces: res.provinceName + res.cityName
              });
              let data = {};
              data.mail_contacts = thePage.data.mail_contacts;
              data.mail_contacts_number = thePage.data.mail_contacts_number;
              data.mail_address = thePage.data.mail_address;
              data.mail_provinces = thePage.data.mail_provinces;
              thePage.saveNews(data);
            }
          });
        }
      }
    })
  }

这是本人第一次在csdn写文章,也是平时工作中需要处理的问题。
以便以后遇到类似问题自己和大家快捷解决问题。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值