From 6077ea8b26377fa23b5b9b876888fb832887109f Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 23 Dec 2023 22:40:30 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E5=BE=AE=E4=BF=A1=E5=85=AC?= =?UTF-8?q?=E4=BC=97=E5=8F=B7=EF=BC=9A=E6=8E=A5=E5=85=A5=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/login.vue | 28 +++++------ .../provider/wechat/officialAccount.js | 47 ++++--------------- 2 files changed, 22 insertions(+), 53 deletions(-) diff --git a/pages/index/login.vue b/pages/index/login.vue index f3298b24..3ef6f525 100644 --- a/pages/index/login.vue +++ b/pages/index/login.vue @@ -1,32 +1,29 @@ + diff --git a/sheep/platform/provider/wechat/officialAccount.js b/sheep/platform/provider/wechat/officialAccount.js index cac051bb..1e8064cb 100644 --- a/sheep/platform/provider/wechat/officialAccount.js +++ b/sheep/platform/provider/wechat/officialAccount.js @@ -23,8 +23,9 @@ async function login(code = '', state = '') { // 情况二:有 code 时,使用 code 去自动登录 } else { // 解密 code 发起登陆 - const loginResult = await loginByCode(code, state); + const loginResult = await AuthUtil.socialLogin(socialType, code, state); if (loginResult.code === 0) { + // TODO 芋艿:shareLog setOpenid(loginResult.data.openid); return loginResult; } @@ -33,8 +34,8 @@ async function login(code = '', state = '') { } // 微信公众号绑定 -async function bind(code = '') { - // 获取绑定地址 +async function bind(code = '', state = '') { + // 情况一:没有 code 时,去获取 code if (code === '') { const loginUrl = await getLoginUrl('bind'); if (loginUrl) { @@ -42,9 +43,10 @@ async function bind(code = '') { window.location = loginUrl; } } else { - // 解密code发起登陆 - const loginResult = await bindByCode(code); - if (loginResult.error === 0) { + // 情况二:有 code 时,使用 code 去自动绑定 + const loginResult = await SocialApi.socialBind(socialType, code, state); + if (loginResult.code === 0) { + setOpenid(loginResult.data); return loginResult; } } @@ -59,7 +61,8 @@ const unbind = async (openid) => { // 获取公众号登陆地址 async function getLoginUrl(event = 'login') { - const page = getRootUrl() + 'pages/index/login'; + const page = getRootUrl() + 'pages/index/login' + + '?event=' + event; // event 目的,区分是 login 还是 bind const { code, data } = await AuthUtil.socialAuthRedirect(socialType, page); if (code !== 0) { return undefined; @@ -67,36 +70,6 @@ async function getLoginUrl(event = 'login') { return data; } -// 此处使用前端发送code在后端解密,防止用户在后端过长时间停留 -function loginByCode(code, state) { - if (true) { - return AuthUtil.socialLogin(socialType, code, state); - } - // TODO 芋艿:shareLog - return third.wechat.login({ - platform: 'officialAccount', - shareInfo: uni.getStorageSync('shareLog') || {}, - payload: encodeURIComponent( - JSON.stringify({ - code, - }), - ), - }); -} - -// 此处使用前端发送code在后端解密,防止用户在后端过长时间停留 -function bindByCode(code) { - debugger - return third.wechat.bind({ - platform: 'officialAccount', - payload: encodeURIComponent( - JSON.stringify({ - code, - }), - ), - }); -} - // 设置 openid 到本地存储,目前只有 pay 支付时会使用 function setOpenid(openid) { uni.setStorageSync('openid', openid);