正式环境某些用户uniCloud.getCurrentUserInfo()获取不到uid
正式环境某些用户uniCloud.getCurrentUserInfo()获取不到uid
信息类别 | 详细信息 |
---|---|
产品分类 | uniCloud/App |
开发环境 | Mac |
版本号 | 15.3.1 |
HBuilderX 类型 | Alpha |
HBuilderX 版本号 | 4.52 |
第三方开发者工具版本号 | v3.7.8 |
基础库版本号 | 不知道 |
项目创建方式 | HBuilderX |
### 示例代码:
```javascript
async getUserInfo() {
try {
// 调用uniCloud的API获取用户信息
const res = await uniCloud.getCurrentUserInfo();
console.log(res)
if (res.uid) {
// 存储用户ID到data中
this.userId = res.uid;
// 这里可以继续您的业务逻辑
this.getExamListFromCloud()
console.log('用户ID:', this.userId);
} else {
throw new Error('无法获取用户信息');
}
} catch (error) {
console.error('获取用户信息失败:', error);
// 处理错误情况,例如提示用户登录等
}
},
操作步骤:
- 打开小程序,获取uid
预期结果:
- 正式环境某些用户获取不到uid
实际结果:
- 正式环境某些用户能获取到uid
bug描述:
真机有些用户获取不到用户UID
真机调试报错
{message: "无法获取用户信息", line: 41949, column: 38, sourceURL: "https://usr//app-service.js", stack: "[@https](/user/https)://usr//app-service.js:41949:38↵@[native code]↵h[@https](/user/https)://lib/WAServiceMainContext.js:1:41859↵_callee$@[native code]↵_callee$[@https](/user/https)://usr//app-service.js:41961:35↵[@https](/user/https)://usr//app-service.js:6098:2681↵@[native code]↵h[@https](/user/https)://lib/WAServiceMainContext.js:1:41859↵s@[native code]↵s[@https](/user/https)://usr//app-service.js:6098:2936↵[@https](/user/https)://usr//app-service.js:6098:8498↵@[native code]↵h[@https](/user/https)://lib/WAServiceMainContext.js:1:41859↵anonymous@[native code]↵_tmp12[@https](/user/https)://usr//app-service.js:6098:8954↵[@https](/user/https)://usr//app-service.js:6098:4514↵@[native code]↵h[@https](/user/https)://lib/WAServiceMainContext.js:1:41859↵anonymous@[native code]↵[@https](/user/https)://usr//app-service.js:6098:4582↵[@https](/user/https)://usr//app-service.js:6022:470↵@[native code]↵h[@https](/user/https)://lib/WAServiceMainContext.js:1:41859↵asyncGeneratorStep@[native code]↵asyncGeneratorStep[@https](/user/https)://usr//app-service.js:6022:773↵[@https](/user/https)://usr//app-service.js:6022:1496↵@[native code]↵h[@https](/user/https)://lib/WAServiceMainContext.js:1:41859↵c@[native code]↵c[@https](/user/https)://usr//app-service.js:6022:1557↵q[@https](/user/https)://lib/WASubContext.js:1:57862↵[@https](/user/https)://lib/WASubContext.js:1:59496↵C[@https](/user/https)://lib/WASubContext.js:1:35934"}
2 回复
从问题描述来看,正式环境部分用户无法获取uid,这通常与用户登录状态或token失效有关。以下是可能的原因和解决方案:
- 用户未正确登录或token过期
- 确保用户已通过uni-id登录流程
- 检查token有效期,默认2小时,可在uni-id配置中调整
- 跨端兼容性问题
- 小程序端需要检查是否已正确配置uni-id相关设置
- 确保各端(小程序/H5/App)使用相同的uni-id配置
- 建议增加错误处理逻辑:
async getUserInfo() {
try {
const res = await uniCloud.getCurrentUserInfo();
if(!res || !res.uid){
// 重新登录逻辑
await uniCloud.callFunction({
name: 'uni-id-co',
data: { action: 'checkToken' }
});
return this.getUserInfo(); // 重试
}
this.userId = res.uid;
this.getExamListFromCloud();
} catch(e) {
console.error(e);
// 跳转登录页
uni.navigateTo({ url: '/pages/login/login' });
}
}