微信小程序----用戶拒絕授權,重新調起授權
阿新 • • 發佈:2018-04-12
HA authorize success crypt *** app tin rsa -- 獲取用戶信息
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) { }, }) }, }) } } }) }, }) // 失敗之後再次登入 } }) } }, }) } }) } } }) } })
方法可能有點繁瑣,有不足的地方還請多指教
微信小程序----用戶拒絕授權,重新調起授權