From 263515758c9e28ca22dd395554cc71be6134a52b Mon Sep 17 00:00:00 2001 From: sexygoat <1538832180@qq.com> Date: Thu, 16 Apr 2026 18:15:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=99=BB=E5=BD=95=E6=89=AB=E4=B8=80?= =?UTF-8?q?=E6=89=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/login/login.vue | 49 +++++++++++++++++++++++++++++++++++++++++-- utils/env.js | 2 +- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/pages/login/login.vue b/pages/login/login.vue index d868731..6aa0fd3 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -68,6 +68,7 @@ import { ref, onMounted } from 'vue'; import { authApi } from '@/api/index.js'; import { useUserStore } from '@/store/index.js'; + import { APP_ID } from '@/utils/env.js'; import { initWxSdk } from '@/utils/wxsdk.js'; const userStore = useUserStore(); @@ -77,6 +78,7 @@ if (token) { uni.reLaunch({ url: '/pages/index/index' }); } + handleWechatCallback(); getPathDeviceId(); }); @@ -86,6 +88,22 @@ const inputFocus = ref(false); const showError = ref(false); + const getCode = () => { + const params = new URLSearchParams(window.location.search); + return params.get('code'); + }; + + const clearCode = () => { + window.history.replaceState({}, '', window.location.pathname); + }; + + const redirectToWxAuth = (assetToken) => { + sessionStorage.setItem('assetToken', assetToken); + const redirectUri = encodeURIComponent(window.location.origin + window.location.pathname); + const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APP_ID}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_userinfo&state=wechat_auth#wechat_redirect`; + window.location.href = url; + }; + const clearInput = () => { identifier.value = ''; showError.value = false; @@ -163,17 +181,44 @@ userStore.setAssetToken(verifyData.asset_token); userStore.setIdentifier(identifier.value); - const loginData = await authApi.wechatLogin(verifyData.asset_token, 'mock_code'); + redirectToWxAuth(verifyData.asset_token); + } catch (e) { + console.error('登录失败', e); + loading.value = false; + } + }; + + const handleWechatCallback = async () => { + + const code = getCode(); + const assetToken = sessionStorage.getItem('assetToken'); + + if (!code || !assetToken) return; + + loading.value = true; + clearCode(); + + try { + const loginData = await authApi.wechatLogin(assetToken, code); userStore.setToken(loginData.token); uni.setStorageSync('identifier', userStore.state.identifier); + // #ifdef H5 + if (loginData.user_info) { + userStore.setUserInfo({ + avatar: loginData.user_info.avatar || '', + nickname: loginData.user_info.nickname || '' + }); + } + // #endif + if (loginData.need_bind_phone) { uni.redirectTo({ url: '/pages/bind/bind?fromLogin=true' }); } else { uni.redirectTo({ url: '/pages/index/index' }); } } catch (e) { - console.error('登录失败', e); + console.error('微信登录失败', e); loading.value = false; } }; diff --git a/utils/env.js b/utils/env.js index ada0313..f598f00 100644 --- a/utils/env.js +++ b/utils/env.js @@ -2,4 +2,4 @@ export const BASE_URL = 'https://cmp-api.boss160.cn'; export const APP_TYPE = 'official_account'; -export const APP_ID = 'wx404dc822ce15c5f0'; \ No newline at end of file +export const APP_ID = 'wx404dc822ce15c5f0';