From 076ddb189888f3ebe53ae268fe8355d71095f9fa Mon Sep 17 00:00:00 2001 From: Devil Date: Sun, 1 Dec 2019 00:37:54 +0800 Subject: [PATCH] =?UTF-8?q?qq=E5=B0=8F=E7=A8=8B=E5=BA=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/User.php | 9 +- public/appmini/old/qq/app.js | 151 ++++++++++++++-------- public/appmini/old/qq/pages/user/user.js | 49 +++---- public/appmini/old/qq/pages/user/user.qml | 2 +- public/appmini/old/weixin/app.js | 22 ++-- 5 files changed, 144 insertions(+), 89 deletions(-) diff --git a/application/api/controller/User.php b/application/api/controller/User.php index 8e1cbe630..66108825c 100755 --- a/application/api/controller/User.php +++ b/application/api/controller/User.php @@ -403,7 +403,14 @@ class User extends Common $result = (new \base\QQ(MyC('common_app_mini_qq_appid'), MyC('common_app_mini_qq_appsecret')))->GetAuthSessionKey($this->data_post['authcode']); if($result !== false) { - return DataReturn('授权登录成功', 0, $result); + // 先从数据库获取用户信息 + $user = UserService::AppUserInfoHandle(null, 'qq_openid', $result); + if(empty($user)) + { + return DataReturn('授权登录成功', 0, ['is_alipay_user_exist'=>0, 'openid'=>$result]); + } + $user['is_alipay_user_exist'] = 1; + return DataReturn('授权登录成功', 0, $user); } return DataReturn('授权登录失败', -100); } diff --git a/public/appmini/old/qq/app.js b/public/appmini/old/qq/app.js index 352012c24..b1779a046 100755 --- a/public/appmini/old/qq/app.js +++ b/public/appmini/old/qq/app.js @@ -154,6 +154,24 @@ App({ ); }, + /** + * 获取用户信息,信息不存在则唤醒授权 + * object 回调操作对象 + * method 回调操作对象的函数 + * return 有用户数据直接返回, 则回调调用者 + */ + get_user_info(object, method) { + var user = this.get_user_cache_info(); + if (user == false) { + // 唤醒用户授权 + this.user_login(object, method); + + return false; + } else { + return user; + } + }, + /** * 从缓存获取用户信息 */ @@ -178,13 +196,13 @@ App({ var openid = qq.getStorageSync(self.data.cache_user_login_key) || null; if (openid == null) { - self.user_login(object, method, auth_data); + self.user_login(object, method); } else { self.get_user_login_info(object, method, openid, auth_data); } }, fail: function () { - self.user_login(object, method, auth_data); + self.user_login(object, method); } }); }, @@ -193,45 +211,82 @@ App({ * 用户登录 * object 回调操作对象 * method 回调操作对象的函数 - * auth_data 授权数据 */ - user_login(object, method, auth_data) { - var self = this; - qq.showLoading({ title: "授权中..." }); - qq.login({ - success: (res) => { - if (res.code) { - qq.request({ - url: self.get_request_url('qquserauth', 'user'), - method: 'POST', - data: { authcode: res.code }, - dataType: 'json', - header: { 'content-type': 'application/x-www-form-urlencoded' }, - success: (res) => { - qq.hideLoading(); - if (res.data.code == 0) { - qq.setStorage({ - key: self.data.cache_user_login_key, - data: res.data.data - }); - if((auth_data || null) != null && (auth_data.encryptedData || null) != null && (auth_data.iv || null) != null) - { - self.get_user_login_info(object, method, res.data.data, auth_data); + user_login(object, method) { + var openid = wx.getStorageSync(this.data.cache_user_login_key) || null; + if (openid == null) + { + var self = this; + qq.showLoading({ title: "授权中..." }); + qq.login({ + success: (res) => { + if (res.code) { + qq.request({ + url: self.get_request_url('qquserauth', 'user'), + method: 'POST', + data: { authcode: res.code }, + dataType: 'json', + header: { 'content-type': 'application/x-www-form-urlencoded' }, + success: (res) => { + qq.hideLoading(); + if (res.data.code == 0) { + var data = res.data.data; + if ((data.is_alipay_user_exist || 0) == 1) { + qq.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 { + qq.setStorage({ + key: self.data.cache_user_login_key, + data: data.openid + }); + self.login_to_auth(); + } + } else { + self.showToast(res.data.msg); } - } else { - self.showToast(res.data.msg); - } - }, - fail: () => { - qq.hideLoading(); - self.showToast('服务器请求出错'); - }, + }, + fail: () => { + qq.hideLoading(); + self.showToast('服务器请求出错'); + }, + }); + } + }, + fail: (e) => { + qq.hideLoading(); + self.showToast('授权失败'); + } + }); + } else { + this.login_to_auth(); + } + }, + + /** + * 跳转到登录页面授权 + */ + login_to_auth() { + qq.showModal({ + title: '温馨提示', + content: '授权用户信息', + confirmText: '确认', + cancelText: '暂不', + success: (result) => { + if (result.confirm) { + qq.navigateTo({ + url: "/pages/login/login" }); } - }, - fail: (e) => { - qq.hideLoading(); - self.showToast('授权失败'); } }); }, @@ -528,24 +583,16 @@ App({ } }, - // 登录校验 - is_login_check(res) { + /** + * 登录校验 + * object 回调操作对象 + * method 回调操作对象的函数 + */ + is_login_check(res, object, method) { if(res.code == -400) { qq.clearStorage(); - qq.showModal({ - title: '温馨提示', - content: '授权用户信息', - confirmText: '确认', - cancelText: '暂不', - success: (result) => { - if (result.confirm) { - qq.navigateTo({ - url: "/pages/login/login?event_callback=init" - }); - } - }, - }); + this.get_user_info(object, method); return false; } return true; diff --git a/public/appmini/old/qq/pages/user/user.js b/public/appmini/old/qq/pages/user/user.js index 00980006d..28534f99f 100755 --- a/public/appmini/old/qq/pages/user/user.js +++ b/public/appmini/old/qq/pages/user/user.js @@ -33,31 +33,32 @@ Page({ }, init(e) { - var user = app.get_user_cache_info(this, "init"), - self = this; - // 用户未绑定用户则转到登录页面 - var msg = (user == false) ? '授权用户信息' : '绑定手机号码'; - if (app.user_is_need_login(user)) { - qq.showModal({ - title: '温馨提示', - content: msg, - confirmText: '确认', - cancelText: '暂不', - success: (result) => { - qq.stopPullDownRefresh(); - if(result.confirm) { - qq.navigateTo({ - url: "/pages/login/login?event_callback=init" + var user = app.get_user_info(this, "init"), + self = this; + if (user != false) { + // 用户未绑定用户则转到登录页面 + if (app.user_is_need_login(user)) { + qq.showModal({ + title: '温馨提示', + content: '绑定手机号码', + confirmText: '确认', + cancelText: '暂不', + success: (result) => { + qq.stopPullDownRefresh(); + if(result.confirm) { + qq.navigateTo({ + url: "/pages/login/login?event_callback=init" + }); + } + self.setData({ + avatar: user.avatar || app.data.default_user_head_src, + nickname: user.user_name_view || '用户名', }); - } - self.setData({ - avatar: user.avatar || app.data.default_user_head_src, - nickname: user.user_name_view || '用户名', - }); - }, - }); - } else { - self.get_data(); + }, + }); + } else { + self.get_data(); + } } }, diff --git a/public/appmini/old/qq/pages/user/user.qml b/public/appmini/old/qq/pages/user/user.qml index b955ee872..f74bc806f 100755 --- a/public/appmini/old/qq/pages/user/user.qml +++ b/public/appmini/old/qq/pages/user/user.qml @@ -57,7 +57,7 @@ - 清除缓存 + 安全退出 diff --git a/public/appmini/old/weixin/app.js b/public/appmini/old/weixin/app.js index 94afc1ba1..43876b16c 100755 --- a/public/appmini/old/weixin/app.js +++ b/public/appmini/old/weixin/app.js @@ -281,18 +281,18 @@ App({ */ login_to_auth() { wx.showModal({ - title: '温馨提示', - content: '授权用户信息', - confirmText: '确认', - cancelText: '暂不', - success: (result) => { - if (result.confirm) { - wx.navigateTo({ - url: "/pages/login/login" - }); - } + title: '温馨提示', + content: '授权用户信息', + confirmText: '确认', + cancelText: '暂不', + success: (result) => { + if (result.confirm) { + wx.navigateTo({ + url: "/pages/login/login" + }); } - }); + } + }); }, /**