diff --git a/App.vue b/App.vue index 7d51e0e..cc53453 100644 --- a/App.vue +++ b/App.vue @@ -5,8 +5,13 @@ import wx from 'jweixin-module' // #endif - export default { - onLaunch: function() { + export default { + data() { + return { + h5LoginInFlight: false + } + }, + onLaunch: function() { console.log('App Launch') // 判断环境 // #ifdef H5 @@ -151,13 +156,18 @@ }) }, - // 微信公众号H5授权登录 - wxH5AuthLogin() { - // 判断是否在微信客户端中打开 - const isWeixinBrowser = /MicroMessenger/i.test(navigator.userAgent) - if (!isWeixinBrowser) { - console.log('[App] 非微信环境,跳过登录') - return + // 微信公众号H5授权登录 + wxH5AuthLogin() { + if (this.h5LoginInFlight) { + console.log('[App] 登录流程进行中,跳过重复调用') + return + } + + // 判断是否在微信客户端中打开 + const isWeixinBrowser = /MicroMessenger/i.test(navigator.userAgent) + if (!isWeixinBrowser) { + console.log('[App] 非微信环境,跳过登录') + return } // 检查是否已经有 token @@ -199,12 +209,14 @@ console.error('[App] 提取 code 失败:', e) } - console.log('[App] 提取到的 code:', code) - - if (code) { - // 清理 URL 中的 code 参数,避免重复登录 - try { - const url = new URL(link) + console.log('[App] 提取到的 code:', code) + + if (code) { + this.h5LoginInFlight = true + + // 清理 URL 中的 code 参数,避免重复登录 + try { + const url = new URL(link) url.searchParams.delete('code') url.searchParams.delete('state') // 使用 replaceState 更新 URL,但不刷新页面 @@ -235,12 +247,15 @@ uni.showToast({ title: result.errmsg || '登录失败', icon: 'none' }) } }, - fail: (err) => { - console.error('[App] 获取 token 失败:', err) - uni.showToast({ title: '网络错误,请重试', icon: 'none' }) - } - }) - } else { + fail: (err) => { + console.error('[App] 获取 token 失败:', err) + uni.showToast({ title: '网络错误,请重试', icon: 'none' }) + }, + complete: () => { + this.h5LoginInFlight = false + } + }) + } else { // 没有 code,需要跳转到授权页面 console.log('[App] 未找到 code,跳转到授权页面') const appId = 'wx9538bc740fe87fce'