From 1c218cd730d4a2d5e2701cf64400142ff482fa49 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 23 Dec 2023 10:01:58 +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=E7=99=BB=E5=BD=95=EF=BC=9A=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E7=9A=84=E6=8E=A5=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index/login.vue | 7 +- .../provider/wechat/officialAccount.js | 70 ++++++++++--------- 2 files changed, 42 insertions(+), 35 deletions(-) diff --git a/pages/index/login.vue b/pages/index/login.vue index d2f8ff50..f3298b24 100644 --- a/pages/index/login.vue +++ b/pages/index/login.vue @@ -11,9 +11,12 @@ onLoad(async (options) => { // #ifdef H5 let event = ''; - if (options.login_code) { + new URLSearchParams(location.search).forEach((value, key) => { + options[key] = value; + }); + if (options.code) { event = 'login'; - const { error } = await sheep.$platform.useProvider().login(options.login_code); + const { error } = await sheep.$platform.useProvider().login(options.code, options.state); if (error === 0) { sheep.$store('user').getInfo(); } diff --git a/sheep/platform/provider/wechat/officialAccount.js b/sheep/platform/provider/wechat/officialAccount.js index 713e7f1d..e36280af 100644 --- a/sheep/platform/provider/wechat/officialAccount.js +++ b/sheep/platform/provider/wechat/officialAccount.js @@ -1,16 +1,17 @@ import third from '@/sheep/api/third'; import $wxsdk from '@/sheep/libs/sdk-h5-weixin'; import $store from '@/sheep/store'; -import $platform from '@/sheep/platform'; import { getRootUrl } from '@/sheep/helper'; +import AuthUtil from '@/sheep/api/member/auth'; + +const socialType = 31; // 社交类型 - 微信公众号 // 加载微信公众号JSSDK async function load() { - if ( - $store('app').platform.auto_login && - !$store('user').isLogin && - location.href.search('pages/index/login') === -1 - ) { + // TODO 芋艿:自动登录的逻辑 + if ($store('app').platform.auto_login + && $store('user').isLogin + && location.href.search('pages/index/login') === -1) { // 发起自动登陆 login(); } @@ -18,19 +19,21 @@ async function load() { } // 微信公众号登陆 -async function login(code = '') { +async function login(code = '', state = '') { // 获取登陆地址 - debugger if (!code) { - const loginResult = await getLoginUrl(); - if (loginResult.error === 0 && loginResult.data.login_url) { + const loginUrl = await getLoginUrl(); + if (loginUrl) { uni.setStorageSync('returnUrl', location.href); - window.location = loginResult.data.login_url; + window.location = loginUrl; } } else { - // 解密code发起登陆 - const loginResult = await loginByCode(code); - if (loginResult.error === 0) { + // 解密 code 发起登陆 + const loginResult = await loginByCode(code, state); + if (loginResult.code === 0) { + if (loginResult.data.openid) { + setOpenid(loginResult.data.openid); + } return loginResult; } } @@ -39,13 +42,12 @@ async function login(code = '') { // 微信公众号绑定 async function bind(code = '') { - debugger // 获取绑定地址 if (code === '') { - const loginResult = await getLoginUrl('bind'); - if (loginResult.error === 0 && loginResult.data.login_url) { + const loginUrl = await getLoginUrl('bind'); + if (loginUrl) { uni.setStorageSync('returnUrl', location.href); - window.location = loginResult.data.login_url; + window.location = loginUrl; } } else { // 解密code发起登陆 @@ -67,24 +69,21 @@ async function unbind() { } // 获取公众号登陆地址 -function getLoginUrl(event = 'login') { - debugger - let page = getRootUrl() + 'pages/index/login'; - - return third.wechat.oauthLogin({ - platform: 'officialAccount', - payload: encodeURIComponent( - JSON.stringify({ - page, - event, - }), - ), - }); +async function getLoginUrl(event = 'login') { + const page = getRootUrl() + 'pages/index/login'; + const { code, data } = await AuthUtil.socialAuthRedirect(socialType, page); + if (code !== 0) { + return undefined; + } + return data; } // 此处使用前端发送code在后端解密,防止用户在后端过长时间停留 -function loginByCode(code) { - debugger +function loginByCode(code, state) { + if (true) { + return AuthUtil.socialLogin(socialType, code, state); + } + // TODO 芋艿:shareLog return third.wechat.login({ platform: 'officialAccount', shareInfo: uni.getStorageSync('shareLog') || {}, @@ -109,6 +108,11 @@ function bindByCode(code) { }); } +// 设置 openid 到本地存储,目前只有 pay 支付时会使用 +function setOpenid(openid) { + uni.setStorageSync('openid', openid); +} + export default { load, login,