From 4e40a69f61ea822364790e0ebe82e4367cd4e771 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 11 Sep 2023 09:15:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=EF=BC=9A=E5=88=9D=E6=AD=A5?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7=E7=9A=84=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E9=9D=99=E9=BB=98=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.vue | 45 +++++++++++++++++++-------------------------- libs/login.js | 7 +------ libs/wechat.js | 35 +++++++++++++++++++++-------------- 3 files changed, 41 insertions(+), 46 deletions(-) diff --git a/App.vue b/App.vue index aa427ec7..486d688e 100644 --- a/App.vue +++ b/App.vue @@ -77,37 +77,30 @@ // #endif // #ifdef H5 - // TODO 芋艿:公众号的静默授权 + // 静默登录的情况一:微信公众号 let snsapiBase = 'snsapi_base'; let urlData = location.pathname + location.search; if (!that.$store.getters.isLogin && Auth.isWeixin()) { - const { code, } = option.query; - // debugger + const { code, state } = option.query; + debugger if (code && code !== uni.getStorageSync('snsapiCode') && location.pathname.indexOf('/pages/users/wechat_login/index') === -1) { - // 存储静默授权code + // 存储静默授权code uni.setStorageSync('snsapiCode', code); - let spread = that.globalData.spid ? that.globalData.spid : 0; - Auth.auth(code, that.$Cache.get('spread')) - .then(res => { - uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query.back_url))); - if (res.type === 'register') { - this.$Cache.set('snsapiKey', res.key); - } - if (res.type === 'login') { - this.$store.commit('LOGIN', { - token: res.token - }); - this.$store.commit("SETUID", res.uid); - location.replace(decodeURIComponent(decodeURIComponent(option.query.back_url))); - } - }).catch(error => { - if (!this.$Cache.has('snsapiKey')) { - if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) { - Auth.oAuth(snsapiBase, option.query.back_url); - } - } - }); + Auth.auth(code, state, that.$Cache.get('spread')).then(res => { + // TODO 芋艿:snRouter 的作用是啥 + uni.setStorageSync('snRouter', decodeURIComponent(decodeURIComponent(option.query.back_url))); + // 跳转回去 + location.replace(decodeURIComponent(decodeURIComponent(option.query.back_url))); + }).catch(error => { + this.$Cache.set('snsapiKey', code); + // TODO 芋艿:为什么没有 snsapiKey 就反复认证???没看懂 + // if (!this.$Cache.has('snsapiKey')) { + // if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) { + // Auth.oAuth(snsapiBase, option.query.back_url); + // } + // } + }); } else { if (!this.$Cache.has('snsapiKey')) { if (location.pathname.indexOf('/pages/users/wechat_login/index') === -1) { @@ -123,7 +116,7 @@ // #endif // #ifdef MP - // 小程序静默授权 + // 静默登录的情况二:微信小程序 if (!this.$store.getters.isLogin) { let spread = that.globalData.spid ? that.globalData.spid : 0; Routine.getCode().then(code => { diff --git a/libs/login.js b/libs/login.js index a7e56297..ec0a43fb 100644 --- a/libs/login.js +++ b/libs/login.js @@ -33,12 +33,7 @@ export function _toLogin(push, pathLogin) { // #ifdef H5 if (isWeixin()) { let urlData = location.pathname + location.search - if (urlData.indexOf('?') !== -1) { - urlData += '&go_longin=1'; - } else { - urlData += '?go_longin=1'; - } - if (true && !Cache.has('snsapiKey')) { + if (!Cache.has('snsapiKey')) { // TODO 芋艿:先临时禁用掉,改成普通登录页 // auth.oAuth('snsapi_base', urlData); uni.navigateTo({ diff --git a/libs/wechat.js b/libs/wechat.js index 756fb318..c1c81863 100644 --- a/libs/wechat.js +++ b/libs/wechat.js @@ -1,6 +1,7 @@ // #ifdef H5 import WechatJSSDK from "@/plugin/jweixin-module/index.js"; import * as AuthApi from "@/api/member/auth"; +import * as BrokerageAPI from '@/api/trade/brokerage.js' import * as WeiXinApi from '@/api/system/weixin.js'; import { @@ -151,17 +152,13 @@ class AuthWechat { * 自动去授权 */ oAuth(snsapiBase,url) { - // TODO 芋艿:先链式去掉这个逻辑; - // if (true) { - // return; - // } - if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase == 'snsapi_base') return; - const { - code - } = parseQuery(); + if (uni.getStorageSync(WX_AUTH) && store.state.app.token && snsapiBase === 'snsapi_base') { + return; + } + const { code } = parseQuery(); if (!code || code == uni.getStorageSync('snsapiCode')){ return this.toAuth(snsapiBase,url); - }else{ + } else{ if(Cache.has('snsapiKey')) return this.auth(code).catch(error=>{ uni.showToast({ @@ -177,13 +174,23 @@ class AuthWechat { * * 实现逻辑是:发起社交登录 */ - auth(code, state) { + auth(code, state, spread) { return new Promise((resolve, reject) => { // 31 的原因,它是公众号登录的社交类型 AuthApi.socialLogin(31, code, state) - .then((data) => { - debugger - resolve(data); + .then(res => { + // 设置访问令牌 + store.commit('LOGIN', { + token: res.data.accessToken + }); + store.commit("SETUID", res.data.userId); + store.commit("OPENID", res.data.openid); + // 绑定推广员 + if (spread > 0) { + BrokerageAPI.bindBrokerageUser(spread) + } + // 回调 + resolve(res); Cache.set(WX_AUTH, code); Cache.clear(STATE_KEY); }).catch(reject); @@ -213,7 +220,7 @@ class AuthWechat { ("" + Math.random()).split(".")[1] + "authorizestate" ); uni.setStorageSync(STATE_KEY, state); - return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`; + return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=${snsapiBase}&state=${state}#wechat_redirect`; } /**