diff --git a/frontend/app.js b/frontend/app.js index 166d69e..ab377a9 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -246,6 +246,31 @@ createApp({ }, methods: { + // 提取URL中的token(兼容缺少 ? 的场景) + getTokenFromUrl(key) { + const currentHref = window.location.href; + const url = new URL(currentHref); + let token = url.searchParams.get(key); + + if (!token) { + const match = currentHref.match(new RegExp(`${key}=([\\w-]+)`)); + if (match && match[1]) { + token = match[1]; + } + } + return token; + }, + + // 清理URL中的token(同时处理路径和查询参数) + sanitizeUrlToken(key) { + const url = new URL(window.location.href); + url.searchParams.delete(key); + if (url.pathname.includes(`${key}=`)) { + url.pathname = url.pathname.split(`${key}=`)[0]; + } + window.history.replaceState({}, document.title, url.toString()); + }, + // 模态框点击外部关闭优化 - 防止拖动选择文本时误关闭 modalMouseDownTarget: null, handleModalMouseDown(e) { @@ -460,9 +485,7 @@ handleDragLeave(e) { this.verifyMessage = '邮箱验证成功,请登录'; this.isLogin = true; // 清理URL - const url = new URL(window.location.href); - url.searchParams.delete('verifyToken'); - window.history.replaceState({}, document.title, url.toString()); + this.sanitizeUrlToken('verifyToken'); } } catch (error) { console.error('邮箱验证失败:', error); @@ -1685,9 +1708,7 @@ handleDragLeave(e) { this.showResetPasswordModal = false; this.resetPasswordForm = { token: '', new_password: '' }; // 清理URL中的token - const url = new URL(window.location.href); - url.searchParams.delete('resetToken'); - window.history.replaceState({}, document.title, url.toString()); + this.sanitizeUrlToken('resetToken'); } } catch (error) { console.error('密码重置失败:', error); @@ -2240,16 +2261,17 @@ handleDragLeave(e) { // 初始化调试模式状态 this.debugMode = localStorage.getItem('debugMode') === 'true'; - // 处理URL中的验证/重置token - const url = new URL(window.location.href); - const verifyToken = url.searchParams.get('verifyToken'); - const resetToken = url.searchParams.get('resetToken'); + // 处理URL中的验证/重置token(兼容缺少?的旧链接) + const verifyToken = this.getTokenFromUrl('verifyToken'); + const resetToken = this.getTokenFromUrl('resetToken'); if (verifyToken) { this.handleVerifyToken(verifyToken); + this.sanitizeUrlToken('verifyToken'); } if (resetToken) { this.resetPasswordForm.token = resetToken; this.showResetPasswordModal = true; + this.sanitizeUrlToken('resetToken'); } // 阻止全局拖拽默认行为(防止拖到区域外打开新页面)