fix: 添加deviceId作为登录备选方案
- 后端:微信API失败时使用deviceId作为openId - 前端:生成并缓存唯一deviceId - 确保同一设备登录时用户身份一致
This commit is contained in:
1 parent
7b6df6426c
commit
6574279fce
2 files changed
+22
-4
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
Reference in new issue
Block a user