原生微信授权拒绝授权强制调取授权功能实现

wx.getSetting({
        success: (res) => {
          let authSetting = res.authSetting
          console.log(res)
          if (!authSetting['scope.userLocation'] || authSetting['scope.userLocation'] == undefined) {
            wx.showModal({
              title: '温馨提示',
              content: '若不授权使用定位,将无法使用现场签到功能!',
              cancelText: '不授权',
              cancelColor: '#999',
              confirmText: '授权',
              confirmColor: '#1ba9ba',
              success: (res) => {
                if (res.confirm) { 
                  console.log(authSetting['scope.userLocation'])
                  if (authSetting['scope.userLocation'] == undefined) {
                    console.log('从来没有调用过授权,直接调起授权')
                    wx.authorize({
                      scope: 'scope.userLocation'
                    })
                  } else {
                    console.log('调起过授权但是被拒了,需要手动操作授权')
                    wx.openSetting({
                      success: (res) => {
                        res.authSetting = {
                          "scope.userLocation": true
                        }
                      },
                    })
                  }
                } else if (res.cancel) { //拒绝打开授权页面
                  console.log('//拒绝打开授权页面')
                }
              }
            })
          } else {
            console.log('获取当前定位信息,并签到')
            wx.getLocation({
              type: 'wgs84',
              success: (res) => {
                const {
                  latitude,
                  longitude
                } = res
                console.log(res)
              }
            })
          }
        }
      })

在 app.json 中配置

 "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  },
  "requiredPrivateInfos": [
    "getLocation"
  ]

你可能感兴趣的:(微信,javascript,前端)