From 77a733891c4fe977a33164755bc82391d28c0cc9 Mon Sep 17 00:00:00 2001 From: "[yxf]" <[1524240689@qq.com]> Date: Mon, 13 Apr 2026 09:55:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0404=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E6=8B=A6=E6=88=AA=E4=B8=8D=E5=B8=A6token=E7=9A=84=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=9B=B4=E6=96=B0=E6=9C=80?= =?UTF-8?q?=E5=90=8E=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/UserServiceImpl.java | 10 ++-- im-uniapp/pages.json | 8 ++- im-uniapp/pages/login/login.vue | 57 ++++++++++++++----- im-web/src/components/chat/ChatBox.vue | 14 ++--- 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java b/im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java index d12ff45..1bb8557 100644 --- a/im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java +++ b/im-platform/src/main/java/com/bx/implatform/service/impl/UserServiceImpl.java @@ -1,5 +1,6 @@ package com.bx.implatform.service.impl; +import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; @@ -110,7 +111,8 @@ public class UserServiceImpl extends ServiceImpl implements Us guestUser.setPassword(""); guestUser.setUuid(guestUuid); guestUser.setLastLoginIp(dto.getIp()); - guestUser.setSourceUrl(dto.getSourceUrl()); + guestUser.setSourceUrl(dto.getSourceUrl()); + guestUser.setLastLoginTime(DateUtil.date()); guestUser.setUniqueToken(dto.getUniqueToken()); // 保存到数据库 this.save(guestUser); @@ -151,10 +153,6 @@ public class UserServiceImpl extends ServiceImpl implements Us return vo; } - /** - * 根据 uniqueToken 查询【客服】 - * 客服条件:is_customer = 2 且 unique_token = 传入的token - */ public Long getCustomerServiceIdByUniqueToken(String uniqueToken) { // 1. 构建查询条件 LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); @@ -230,6 +228,8 @@ public class UserServiceImpl extends ServiceImpl implements Us String newRefreshToken = JwtUtil.sign(userId, strJson, jwtProperties.getRefreshTokenExpireIn(), jwtProperties.getRefreshTokenSecret()); LoginVO vo = new LoginVO(); + User guestUser = new User(); + guestUser.setLastLoginTime(DateUtil.date()); vo.setAccessToken(accessToken); vo.setAccessTokenExpiresIn(jwtProperties.getAccessTokenExpireIn()); vo.setRefreshToken(newRefreshToken); diff --git a/im-uniapp/pages.json b/im-uniapp/pages.json index 19c15ba..72f4341 100644 --- a/im-uniapp/pages.json +++ b/im-uniapp/pages.json @@ -38,7 +38,13 @@ "softinputMode": "adjustResize" } } - } + }, + { + "path": "pages/login/404", + "style": { + "navigationBarTitleText": "页面不存在" + } + } // { // "path": "pages/chat/chat-private-video" // }, diff --git a/im-uniapp/pages/login/login.vue b/im-uniapp/pages/login/login.vue index 8ef2251..dd3727a 100644 --- a/im-uniapp/pages/login/login.vue +++ b/im-uniapp/pages/login/login.vue @@ -104,10 +104,28 @@ export default { async autoLogin() { if (GLOBAL_AUTO_LOGIN_LOCK) return; GLOBAL_AUTO_LOGIN_LOCK = true; - + // 获取token(优先使用URL中的token) this.getTokenFromUrl(); + // 判断是否有 token,没有则跳转 404 + if (!this.dataForm.uniqueToken) { + console.log("未携带token,跳转到404页面"); + // #ifdef H5 + // 考虑基础路径 /h5/ + window.location.href = '/h5/#/pages/login/404'; + // #endif + + // #ifndef H5 + uni.redirectTo({ + url: '/pages/login/404' + }); + // #endif + + GLOBAL_AUTO_LOGIN_LOCK = false; + return; + } + // 获取来源网址 this.getSourceUrl(); await this.getIp(); @@ -148,22 +166,35 @@ export default { } this.friendStore.addFriend(friend); - // 清除 URL 中的 token 参数 - // #ifdef H5 - // 直接使用 window.location 跳转,清除所有参数 - const cleanUrl = window.location.origin + window.location.pathname + '#/pages/chat/chat-box?targetId=' + loginInfo.customerServiceId + '&type=PRIVATE'; - window.location.href = cleanUrl; - // #endif - - // #ifndef H5 - uni.reLaunch({ - url: `/pages/chat/chat-box?targetId=${loginInfo.customerServiceId}&type=PRIVATE` - }); - // #endif + // 清除 URL 中的 token 参数,跳转到聊天页面 + // #ifdef H5 + const cleanUrl = window.location.origin + '/h5/' + '#/pages/chat/chat-box?targetId=' + loginInfo.customerServiceId + '&type=PRIVATE'; + window.location.href = cleanUrl; + // #endif + + // #ifndef H5 + uni.reLaunch({ + url: `/pages/chat/chat-box?targetId=${loginInfo.customerServiceId}&type=PRIVATE` + }); + // #endif }) }).catch(err => { console.log("自动登录失败", err); + GLOBAL_AUTO_LOGIN_LOCK = false; + + // 如果是认证失败(如token无效),也可以跳转404 + if (err && (err.code === 401 || err.statusCode === 401)) { + // #ifdef H5 + window.location.href = '/h5/pages/login/404'; + // #endif + + // #ifndef H5 + uni.redirectTo({ + url: '/pages/login/404' + }); + // #endif + } }); }, diff --git a/im-web/src/components/chat/ChatBox.vue b/im-web/src/components/chat/ChatBox.vue index fe59c12..f86b10d 100644 --- a/im-web/src/components/chat/ChatBox.vue +++ b/im-web/src/components/chat/ChatBox.vue @@ -82,23 +82,19 @@