微信小程序中用户拒绝授权的处理方式

微信小程序中用户拒绝授权的处理方式,第1张

https://www.jianshu.com/p/80e33d16182f

//获取用户地理位置权限

  getPermission: function (callback) {

    wx.getLocation({

      success: function (res) {

        console.log(res, 'getLocation成功')

        if (res.latitude && res.latitude != '') {

          // wx.showToast({

          //   title: '进行搜索',

          // })

          //进行搜索

          callback()

        }

      },

      fail: function () {

        wx.getSetting({

          success: function (res) {

            // console.log(res, '之前允许')

            if (res.authSetting['scope.userLocation'] == true) {

              wx.showToast({

                title: '请打开手机位置服务',

                icon:'none',

                duration:2000,

              })

            } else {

              wx.showModal({

                title: '提示',

                content: '您拒绝了定位权限,将无法使用蓝牙搜索功能',

                showCancel: false,//是否显示取消按钮

                success: res => {

                  if (res.confirm) {

                    wx.showToast({

                      title: '删除小程序,重新进入',

                      duration:2000,

                      icon:'none'

                    })

                  }

                }

              })

            }

          },

          fail: function (res) {

            // console.log(res, '之前拒绝')

            wx.showToast({

              title: '请删除小程序,重新进入',

              icon:'none'

            })

          },

        })

      }

    })

  },

多次拒绝登录和允许登录

微信小程序的登录获取用户信息,是通过微信d出窗口,用户可点击允许 和拒绝两个按钮,点击允许,则获取用户信息登录成功,若点击拒绝,则获取失败,可通过二次请求调用d起获取用户信息窗口。

具体代码如下:

//app.js

App({

onLaunch: function () {

//调用API从本地缓存中获取数据

var logs = wx.getStorageSync('logs') || []

logs.unshift(Date.now())

wx.setStorageSync('logs', logs)

console.log('logs',logs)

},

getUserInfo:function(cb){

var that = this

//调用登录接口

function getOpenid(backMsg){

that.globalData.userInfo = backMsg.userInfo

that.globalData.encryptedData = backMsg.encryptedData

that.globalData.iv = backMsg.iv

that.globalData.login = true

console.log(that.globalData.code)

wx.request({

url:that.localUrl+'Login/sendCodeLogin',

data:{

code:that.globalData.code,

encryptedData: backMsg.encryptedData,

iv:backMsg.iv

},

success:function(openData){

console.log('返回openid',openData,openData.data)

if(openData.data.code==1001){

that.globalData.openid = openData.data.data.openid

wx.setStorageSync('openid',that.globalData.openid)

wx.setStorageSync('userInfo',that.globalData.userInfo)

wx.setStorageSync('isManager', openData.data.data.isManager)

wx.showToast({

title: '登录成功',

icon: 'success',

duration: 500

})

typeof cb == "function" &&cb(that.globalData.userInfo)

}else{

wx.showLoading({

title: '登录失败'

})

setTimeout(function () {

wx.hideLoading()

}, 500)

}

}

})

}

wx.login({

success: function (msg) {

console.log('code',msg)

if(msg.code){

that.globalData.code = msg.code

if(that.globalData.login==false){

wx.openSetting({

success: function (data) {

if(data) {

if (data.authSetting["scope.userInfo"] == true) {

//loginStatus = true

wx.getUserInfo({

withCredentials: false,

success: function (res) {

console.log('第二次成功',res)

getOpenid(res)

},

fail: function (res) {

that.globalData.login = false

console.log('二次失败',res)

}

})

}else{

that.globalData.login = false

console.log('二次失败02')

}

}

},

fail: function () {

console.info("设置失败返回数据")

}

})

}else{

wx.getUserInfo({

success: function (res) {

console.log('第一次成功',res)

getOpenid(res)

},fail:function(msg){

that.globalData.login = false

console.log('第一次失败',msg)

}

})

}

}

},

fail:function(res){

console.log(res)

}

})

},

globalData:{

userInfo:null,

encryptedData:null,

iv:null,

openid:null,

code:null

}

})


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8026980.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存