From 6574279fce38bc87bbb1bcc38b55cf7dfe449053 Mon Sep 17 00:00:00 2001 From: H1d3rOne Date: Tue, 10 Mar 2026 17:00:00 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0deviceId=E4=BD=9C?= =?UTF-8?q?=E4=B8=BA=E7=99=BB=E5=BD=95=E5=A4=87=E9=80=89=E6=96=B9=E6=A1=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 后端:微信API失败时使用deviceId作为openId - 前端:生成并缓存唯一deviceId - 确保同一设备登录时用户身份一致 --- src/main/java/xuan/controller/UserController.java | 14 +++++++++++--- .../src/components/auth-modal/auth-modal.vue | 12 +++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/xuan/controller/UserController.java b/src/main/java/xuan/controller/UserController.java index 933f417..d5ed3ea 100644 --- a/src/main/java/xuan/controller/UserController.java +++ b/src/main/java/xuan/controller/UserController.java @@ -76,9 +76,17 @@ public class UserController { } if (openId == null || openId.isEmpty()) { - result.put("success", false); - result.put("error", "获取用户身份失败,请重试"); - return result; + // 开发环境:如果appId或appSecret未配置,使用deviceId作为临时方案 + String deviceId = params.get("deviceId") != null ? params.get("deviceId").toString() : null; + if (deviceId != null && !deviceId.isEmpty()) { + openId = "device_" + deviceId; + System.out.println("使用设备ID作为openId: " + openId); + } else { + System.err.println("获取openId失败,请检查微信配置或提供deviceId"); + result.put("success", false); + result.put("error", "获取用户身份失败,请重试"); + return result; + } } UserInfo user = userInfoService.loginOrRegister(openId, username, avatarUrl); diff --git a/xuan-deduct-uniapp/src/components/auth-modal/auth-modal.vue b/xuan-deduct-uniapp/src/components/auth-modal/auth-modal.vue index 7e4af43..0d84126 100644 --- a/xuan-deduct-uniapp/src/components/auth-modal/auth-modal.vue +++ b/xuan-deduct-uniapp/src/components/auth-modal/auth-modal.vue @@ -245,7 +245,8 @@ export default { const res = await api.userApi.loginOrRegister({ code: loginRes.code, nickname: this.nickname, - avatarUrl: avatarUrlForServer + avatarUrl: avatarUrlForServer, + deviceId: this.getDeviceId() }) if (res.success) { @@ -288,6 +289,15 @@ export default { } }, + getDeviceId() { + let deviceId = uni.getStorageSync('deviceId') + if (!deviceId) { + deviceId = 'dev_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9) + uni.setStorageSync('deviceId', deviceId) + } + return deviceId + }, + handleCancel() { this.showAnimation = false setTimeout(() => {