From 7e40037df3f8d45da3d823f8bdb988d1ff911d50 Mon Sep 17 00:00:00 2001 From: "[yxf]" <[1524240689@qq.com]> Date: Wed, 13 May 2026 09:16:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=94=A8=E6=88=B7=E7=AB=AF?= =?UTF-8?q?=E4=BC=A0=E5=8F=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/bx/implatform/dto/LoginDTO.java | 2 +- .../service/impl/UserServiceImpl.java | 14 +- im-uniapp/pages/login/login.vue | 156 +++++++++--------- 3 files changed, 87 insertions(+), 85 deletions(-) diff --git a/im-platform/src/main/java/com/bx/implatform/dto/LoginDTO.java b/im-platform/src/main/java/com/bx/implatform/dto/LoginDTO.java index 992e60d..c42712d 100644 --- a/im-platform/src/main/java/com/bx/implatform/dto/LoginDTO.java +++ b/im-platform/src/main/java/com/bx/implatform/dto/LoginDTO.java @@ -35,7 +35,7 @@ public class LoginDTO { private String uniqueToken; @Schema(description = "客服ID") - private String keFuId; + private String customerId; @Schema(description = "可切换客服账号") private String switchableAccountIds; 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 791c27f..9e11188 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 @@ -163,24 +163,24 @@ public class UserServiceImpl extends ServiceImpl implements Us */ private Long getCustomerServiceId(LoginDTO dto) { // 1. 如果前端传了 kefuid,直接使用并验证该客服是否有效 - if (StrUtil.isNotBlank(dto.getKeFuId())) { + if (StrUtil.isNotBlank(dto.getCustomerId())) { try { - Long kefuid = Long.parseLong(dto.getKeFuId()); + Long customerId = Long.parseLong(dto.getCustomerId()); // 验证该客服是否存在且有效 - User customer = this.getById(kefuid); + User customer = this.getById(customerId); if (customer != null && customer.getIsCustomer() == 2) { // 验证该客服的uniqueToken是否与传入的匹配 if (dto.getUniqueToken().equals(customer.getUniqueToken())) { - return kefuid; + return customerId; } else { - log.warn("前端传入的客服ID {} 的uniqueToken与当前不匹配,将使用token查询", kefuid); + log.warn("前端传入的客服ID {} 的uniqueToken与当前不匹配,将使用token查询", customerId); } } else { - log.warn("前端传入的客服ID {} 无效或不是客服,将使用token查询", kefuid); + log.warn("前端传入的客服ID {} 无效或不是客服,将使用token查询", customerId); } } catch (NumberFormatException e) { - log.warn("前端传入的kefuid格式错误:{}", dto.getKeFuId()); + log.warn("前端传入的kefuid格式错误:{}", dto.getCustomerId()); } } // 2. 通过uniqueToken查询客服 diff --git a/im-uniapp/pages/login/login.vue b/im-uniapp/pages/login/login.vue index 9558c56..a887299 100644 --- a/im-uniapp/pages/login/login.vue +++ b/im-uniapp/pages/login/login.vue @@ -21,7 +21,7 @@ export default { ip: '', sourceUrl: '', uniqueToken: '', - kefuid: '' + customerid: '' } } }, @@ -43,15 +43,15 @@ export default { } } // #endif - + // #ifdef APP-PLUS this.dataForm.sourceUrl = 'app'; // #endif - + // #ifdef MP-WEIXIN this.dataForm.sourceUrl = 'wechat-miniprogram'; // #endif - + // 如果没有获取到,设置默认值 if (!this.dataForm.sourceUrl) { this.dataForm.sourceUrl = 'unknown'; @@ -59,35 +59,35 @@ export default { this.dataForm.sourceUrl = this.dataForm.sourceUrl.replace(/\/+$/, ''); console.log("来源网址:", this.dataForm.sourceUrl); }, - - // 获取URL参数中的token和kefuid + + // 获取URL参数中的token和customerid getTokenFromUrl() { // #ifdef H5 const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; const options = currentPage.options || {}; - + if (options.token) { this.dataForm.uniqueToken = options.token; } - - if (options.kefuid) { - this.dataForm.kefuid = options.kefuid; + + if (options.customerid) { + this.dataForm.customerid = options.customerid; } - + const urlParams = new URLSearchParams(window.location.search); const token = urlParams.get('token'); - const kefuid = urlParams.get('kefuid'); - + const customerid = urlParams.get('customerid'); + if (token) { this.dataForm.uniqueToken = token; } - - if (kefuid) { - this.dataForm.kefuid = kefuid; + + if (customerid) { + this.dataForm.customerid = customerid; } // #endif - + // #ifdef APP-PLUS // App端获取参数 const pages = getCurrentPages(); @@ -96,11 +96,11 @@ export default { if (options.token) { this.dataForm.uniqueToken = options.token; } - if (options.kefuid) { - this.dataForm.kefuid = options.kefuid; + if (options.customerid) { + this.dataForm.customerid = options.customerid; } // #endif - + // #ifdef MP-WEIXIN // 小程序端获取参数 const pages = getCurrentPages(); @@ -110,41 +110,40 @@ export default { this.dataForm.uniqueToken = options.token; } console.log(options); - if (options.kefuid) { - this.dataForm.kefuid = options.kefuid; + if (options.customerid) { + this.dataForm.customerid = options.customerid; } // #endif }, - + 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(); - + // 准备登录数据 const loginData = { terminal: this.dataForm.terminal, @@ -153,9 +152,9 @@ export default { ip: this.dataForm.ip, sourceUrl: this.dataForm.sourceUrl, uniqueToken: this.dataForm.uniqueToken, - keFuId: this.dataForm.kefuid, // 添加kefuid参数 + customerId: this.dataForm.customerid, }; - + this.$http({ url: '/login', data: loginData, @@ -163,56 +162,61 @@ export default { }).then(loginInfo => { uni.setStorageSync("isAgree", this.isAgree); uni.setStorageSync("loginInfo", loginInfo); - + getApp().$vm.init() getApp().$vm.unloadStore(); this.$http({ - url: "/friend/add?friendId=" + loginInfo.customerServiceId, - method: "POST" + url: "/friend/add?friendId=" + loginInfo.customerServiceId, + method: "POST" }).then((data) => { - let friend = { - id: loginInfo.user.id, - nickName: loginInfo.user.nickName, - headImage: loginInfo.user.headImageThumb, - online: loginInfo.user.online, - delete: false - } - this.friendStore.addFriend(friend); - - // 清除 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)) { + let friend = { + id: loginInfo.user.id, + nickName: loginInfo.user.nickName, + headImage: loginInfo.user.headImageThumb, + online: loginInfo.user.online, + delete: false + } + this.friendStore.addFriend(friend); + + this.clearUrlParams(); + // #ifdef H5 - window.location.href = '/h5/pages/login/404'; + const cleanUrl = window.location.origin + '/h5/#/pages/chat/chat-box?targetId=' + loginInfo.customerServiceId + '&type=PRIVATE'; + window.location.href = cleanUrl; // #endif - + // #ifndef H5 - uni.redirectTo({ - url: '/pages/login/404' + uni.reLaunch({ + url: `/pages/chat/chat-box?targetId=${loginInfo.customerServiceId}&type=PRIVATE` }); // #endif - } + }) + + }).catch(err => { + console.log("自动登录失败", err); + GLOBAL_AUTO_LOGIN_LOCK = false; + this.clearUrlParams(); + + // #ifdef H5 + window.location.href = '/h5/#/pages/login/404'; + // #endif + + // #ifndef H5 + uni.redirectTo({ url: '/pages/login/404' }); + // #endif }); }, - + + clearUrlParams() { + // #ifdef H5 + const url = new URL(window.location.href); + url.searchParams.delete('token'); + url.searchParams.delete('customerid'); + window.history.replaceState(null, '', url.pathname + url.search + url.hash); + // #endif + }, + getIp() { - // 获取本机ip return new Promise((resolve, reject) => { uni.request({ url: 'https://api.ipify.org?format=json', @@ -235,16 +239,14 @@ export default { }, onLoad(options) { - // 在onLoad中直接获取options中的token if (options.token) { this.dataForm.uniqueToken = options.token; console.log("onLoad获取到token:", this.dataForm.uniqueToken); } - if (options.kefuid) { - this.dataForm.kefuid = options.kefuid; - console.log("onLoad获取到kefuid:", this.dataForm.kefuid); - } - // 延迟执行,彻底避开双加载 + if (options.customerid) { + this.dataForm.customerid = options.customerid; + console.log("onLoad获取到customerid:", this.dataForm.customerid); + } setTimeout(() => { this.autoLogin(); }, 50); @@ -254,4 +256,4 @@ export default { } } - \ No newline at end of file +