From 12c5bbad1a492fd1be58df6cbf2e7a95ead87dd0 Mon Sep 17 00:00:00 2001 From: gongfuxiang Date: Tue, 2 Aug 2022 23:23:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=B4=E6=9D=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B8=80=E9=94=AE=E8=8E=B7=E5=8F=96=E6=89=8B?= =?UTF-8?q?=E6=9C=BA=E5=8F=B7=E7=A0=81=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 131 +++++++++++++++++++++++------------------- pages/login/login.vue | 31 +++++----- 2 files changed, 90 insertions(+), 72 deletions(-) diff --git a/App.vue b/App.vue index b404a6f4..3bb39fee 100644 --- a/App.vue +++ b/App.vue @@ -318,68 +318,81 @@ user_login(object, method) { var login_data = uni.getStorageSync(this.data.cache_user_login_key) || null; if (login_data == null) { - var self = this; - uni.showLoading({ - title: "授权中..." - }); - var action = 'login'; - // #ifdef MP-BAIDU - action = 'getLoginCode'; - // #endif - uni[action]({ - success: res => { - if (res.code) { - uni.request({ - url: self.get_request_url('appminiuserauth', 'user'), - method: 'POST', - data: { - authcode: res.code - }, - dataType: 'json', - success: res => { - uni.hideLoading(); - if (res.data.code == 0) { - var data = res.data.data; - if ((data.is_user_exist || 0) == 1) { - uni.setStorage({ - key: self.data.cache_user_info_key, - data: data, - success: res => { - if (typeof object === 'object' && (method || null) != null) { - object[method](); - } - }, - fail: () => { - self.showToast('用户信息缓存失败'); - } - }); - } else { - uni.setStorage({ - key: self.data.cache_user_login_key, - data: data - }); - self.login_to_auth(); - } - } else { - uni.hideLoading(); - self.showToast(res.data.msg); - } - }, - fail: () => { - uni.hideLoading(); - self.showToast('服务器请求出错'); - } - }); - } - }, - fail: e => { - uni.hideLoading(); - self.showToast('授权失败'); - } - }); + this.user_login_handle(object, method, true); } else { this.login_to_auth(); } + }, + + /** + * 用户登录处理 + * object 回调操作对象 + * method 回调操作对象的函数 + * auth_data 授权数据 + * is_to_auth 是否进入授权 + */ + user_login_handle(object, method, is_to_auth = true) { + var self = this; + uni.showLoading({ + title: "授权中..." + }); + var action = 'login'; + // #ifdef MP-BAIDU + action = 'getLoginCode'; + // #endif + uni[action]({ + success: res => { + if (res.code) { + uni.request({ + url: self.get_request_url('appminiuserauth', 'user'), + method: 'POST', + data: { + authcode: res.code + }, + dataType: 'json', + success: res => { + uni.hideLoading(); + if (res.data.code == 0) { + var data = res.data.data; + if ((data.is_user_exist || 0) == 1) { + uni.setStorage({ + key: self.data.cache_user_info_key, + data: data, + success: res => { + if (typeof object === 'object' && (method || null) != null) { + object[method](); + } + }, + fail: () => { + self.showToast('用户信息缓存失败'); + } + }); + } else { + uni.setStorage({ + key: self.data.cache_user_login_key, + data: data + }); + if(is_to_auth) { + self.login_to_auth(); + } + } + } else { + uni.hideLoading(); + self.showToast(res.data.msg); + } + }, + fail: () => { + uni.hideLoading(); + self.showToast('服务器请求出错'); + } + }); + } + }, + fail: e => { + uni.hideLoading(); + self.showToast('授权失败'); + } + }); }, /** diff --git a/pages/login/login.vue b/pages/login/login.vue index 61e97f2d..fabd0d29 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -555,17 +555,17 @@ this.user_auth_code(userinfo); // #endif // #ifdef MP-TOUTIAO - if(e == 1) { - var self = this; - uni.getUserInfo({ - success (res) { - var userinfo = JSON.parse(res.rawData); - self.user_auth_code(userinfo); - } - }); - } else { - app.globalData.auth_check(this, 'get_user_info_event', 'scope.userInfo'); - } + uni.getUserProfile({ + force: true, + success: (res) => { + // 由于头条getUserProfile内置了login + // 会刷新前面的login会导致后面获取手机号码数据解析失败 + // 这里重新获取sessionKey登录 + app.globalData.user_login_handle(null, null, false); + // 用户授权登录 + this.user_auth_code(res.userInfo); + } + }); // #endif // #ifdef MP-KUAISHOU var userinfo = e.target.userInfo; @@ -573,7 +573,7 @@ // #endif }, - // 用户授权 + // 用户授权登录 // auth_data 授权数据 user_auth_code(auth_data) { if ((auth_data || null) != null) { @@ -876,9 +876,14 @@ }, fail: () => { uni.hideLoading(); - self.showToast('服务器请求出错'); + app.globalData.showToast('服务器请求出错'); } }); + } else { + var msg = e.errmsg || e.errMsg || e.detail.errmsg || e.detail.errMsg || null; + if(msg != null) { + app.globalData.showToast(msg); + } } },