• 微信小程序用户拒绝授权,重新调起授权


    获取用户信息
    wx.getUserInfo({
    withCredentials: true,
    success: function (res) {
    var nickName = res.userInfo.nickName;
    var avatarUrl = res.userInfo.avatarUrl;
    var gender = res.userInfo.gender;
    wx.login({
    success: function (res) {
    if (res.code) {
    var rsaData = _this.rsaData({ code: res.code, wx_appid: _this.globalData.appId, nickName: nickName, gender: gender, avatarUrl: avatarUrl })
    wx.request({
    url: _this.globalData.domain + "Index/login",
    data: {
    encrypt_data: rsaData
    },
    method: "POST",
    header: {
    'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
    const user_id = res.data.data.user_id;
    var openid = res.data.data.openid;
    wx.setStorage({
    key: 'user_id',
    data: user_id,
    })
    wx.setStorage({
    key: 'openid',
    data: openid,
    })
    // 获取会员信息
    var rsaData = _this.rsaData({ user_id: user_id })
    wx.request({
    url: _this.globalData.domain + "user/get_user",
    data: {
    encrypt_data: rsaData,
    token: wx.getStorageSync('token')
    },
    header: {
    'content-type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    success: function (res) {
    
    },
    })
    },
    })
    }
    }
    })
    },
    fail: function (res) {
    var that = this;
    wx.openSetting({
    success(res) {
    if (!res.authSetting['scope.userInfo'] || !res.authSetting["scope.userLocation"]) {
    wx.authorize({
    scope: 'scope.userInfo',
    success(res) {
    // 失败之后再次登入
    wx.getUserInfo({
    withCredentials: true,
    success: function (res) {
    var nickName = res.userInfo.nickName;
    var avatarUrl = res.userInfo.avatarUrl;
    var gender = res.userInfo.gender;
    wx.login({
    success: function (res) {
    if (res.code) {
    var rsaData = _this.rsaData({ code: res.code, wx_appid: _this.globalData.appId, nickName: nickName, gender: gender, avatarUrl: avatarUrl })
    wx.request({
    url: _this.globalData.domain + "Index/login",
    data: {
    encrypt_data: rsaData
    },
    method: "POST",
    header: {
    'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
    const user_id = res.data.data.user_id;
    var openid = res.data.data.openid;
    wx.setStorage({
    key: 'user_id',
    data: user_id,
    })
    wx.setStorage({
    key: 'openid',
    data: openid,
    })
    // 获取会员信息
    var rsaData = _this.rsaData({ user_id: user_id })
    wx.request({
    url: _this.globalData.domain + "user/get_user",
    data: {
    encrypt_data: rsaData,
    token: wx.getStorageSync('token')
    },
    header: {
    'content-type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    success: function (res) {
    
    },
    })
    },
    })
    }
    }
    })
    },
    })
    // 失败之后再次登入
    }, fail: (res) => {
    /*
    * 点击取消之后再次获取user_id 和openid
    */
    wx.showModal({
    title: '警告',
    content: '您点击了拒绝授权,将无法正常使用******的功能体验,请10分钟后再次点击授权,或者删除小程序重新进入',
    success: function (res) {
    if (res.cancel) {
    wx.openSetting({
    success: (res) => {
    // 失败之后再次登入
    wx.getUserInfo({
    withCredentials: true,
    success: function (res) {
    var nickName = res.userInfo.nickName;
    var avatarUrl = res.userInfo.avatarUrl;
    var gender = res.userInfo.gender;
    wx.login({
    success: function (res) {
    if (res.code) {
    var rsaData = _this.rsaData({ code: res.code, wx_appid: _this.globalData.appId, nickName: nickName, gender: gender, avatarUrl: avatarUrl })
    wx.request({
    url: _this.globalData.domain + "Index/login",
    data: {
    encrypt_data: rsaData
    },
    method: "POST",
    header: {
    'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
    const user_id = res.data.data.user_id;
    var openid = res.data.data.openid;
    wx.setStorage({
    key: 'user_id',
    data: user_id,
    })
    wx.setStorage({
    key: 'openid',
    data: openid,
    })
    // 获取会员信息
    var rsaData = _this.rsaData({ user_id: user_id })
    wx.request({
    url: _this.globalData.domain + "user/get_user",
    data: {
    encrypt_data: rsaData,
    token: wx.getStorageSync('token')
    },
    header: {
    'content-type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    success: function (res) {
    
    },
    })
    },
    })
    }
    }
    })
    },
    })
    // 失败之后再次登入
    }
    })
    }
    },
    })
    }
    })
    }
    }
    })
    }
    })
    

      方法可能有点繁琐,有不足的地方还请多指教

  • 相关阅读:
    使用zoom.js设置博客园图片放大缩小
    js中变量对象和活动对象的区别 闭包时的区别
    总结Unity 初学者容易犯的编译与运行时错误
    三天之后的「数」下英雄会是个什么会?
    奇点云数据中台技术汇(十)| 数据服务,让业务开发更敏捷
    湖畔论剑 | 一封“数”下英雄会的神秘邀请函
    12月5日,「数据中台建设之道」线上开聊
    StartDT AI Lab | 视觉智能引擎之算法模型加速
    贵州茅台集团一行考察奇点云,探讨酒业数智化转型
    何夕:如何让实体商家拥有淘宝一样的数据化运营能力
  • 原文地址:https://www.cnblogs.com/liujun1128/p/8805271.html
Copyright © 2020-2023  润新知