用户信息托管
本文档旨在指导开发者如何在融云即时通讯 HarmonyOS 客户端 SDK 中实现用户信息订阅、查询和监听,同时支持用户信息与权限的修改、查询。
通过本文档,HarmonyOS 开发者将了解如何获取和跟踪用户信息,以及如何在用户信息变更、订阅状态变更时接收通知。
此功能在 25.12.0 版本开始支持。
开通服务
信息托管服务已默认开通,您可以直接使用此功能。
开启信息托管功能后,您可以通过 API 设置用户、群组资料信息。设置资料即视为同意将用户、群组资料信息托管在融云服务器存储和维护。信息托管服务产生的状态通知会计入消息分发和下行统计,功能开通后 15 分钟生效。
管理用户信息
可以修改或查询自己的用户信息,批量查询指定多个用户的用户信息。
设置用户信息
用户在应用中使用 updateMyUserProfile 可以修改自己的用户信息,您需要设置的相关用户信息可以通过创建 UserProfile 对象来配置相关属性。
默认情况下,您设置的资料信息不会进行审核。如需要开通审核功能,请在需先按控制台内嵌指南嵌入到管理后台的 IM 服务功能配置页面(page_code: im_service_config)中,进入 安全&审核 > IM 信息托管审核,开启并设置需要审核的内容。
接口
public updateMyUserProfile(profile: UserProfile): Promise<IAsyncResult<Array<string>>>;
参数说明
下表描述 UserProfile 类的属性,也可参考 API 文档。
| 属性名 | 类型 | 描述 |
|---|---|---|
name | string | 昵称,长度不超过 32 个字符。 |
portraitUri | string | 头像地址,长度不超过 128 个字符。 |
uniqueId | string | 用户应用号,支持大小写字母、数字,长度不超过 32 个字符。请注意 SDK 不支持设置此字段。 |
email | string | Email,长度不超过 128 个字符。 |
gender | UserGender | 性别,未知 0、男 1、女 2。 |
location | string | 所在地,长度不超过 32 个字符。 |
role | int | 角色,支持 0~100 以内数字。 |
level | int | 级别,支持 0~100 以内数字。 |
userExtProfile | HashMap | 自定义扩展信息,最多可以设置 20 个用户信息(以 Key、Value 方式设置,扩展用户信息通过开发者后台进行设置)
|
示例代码
// 更新自己的用户信息
let profile = new UserProfile();
profile.name = "name";
profile.gender = UserGender.UserGenderMale;
profile.role = 0;
profile.level = 0;
profile.userExtProfile = new HashMap<string, string>();
profile.userExtProfile.set("key", "value");
IMEngine.getInstance().updateMyUserProfile(profile).then((result: IAsyncResult<Array<string>>) => {
if (result.code === EngineError.Success) {
// 更新成功
} else {
// 更新失败
}
});
获取当前用户信息
您可以使用 getMyUserProfile 方法获取当前用户信息。
示例代码
IMEngine.getInstance().getMyUserProfile().then((result: IAsyncResult<UserProfile>) => {
if (result.code === EngineError.Success) {
// 查询成功,返回自己的用户信息
} else {
// 查询失败
}
});
批量获取用户信息
您可以使用 getUserProfiles 方法,通过传入指定用户的 userId 来查询指定用户的用户信息。
单次最多查询 20 个用户的用户信息。
示例代码
// 设置查询用户 ID 列表
let userIds = new Array<string>();
userIds.push("userId1");
userIds.push("userId2");
IMEngine.getInstance().getUserProfiles(userIds).then((result: IAsyncResult<Array<UserProfile>>) => {
if (result.code === EngineError.Success) {
// 获取用户信息完成
} else {
// 获取用户信息失败
}
});
管理用户权限
融云 IMLib SDK 提供了对用户级别用户权限进行设置和获取接口,通过接口设置 UserProfileVisibility 枚举来设置不同的用户权限。
UserProfileVisibility 枚举介绍
| 枚举值 | 用户权限 |
|---|---|
UserProfileVisibilityNotSet | 未设置:以 AppKey 权限设置为准,默认是此状态。 |
UserProfileVisibilityInvisible | 都不可见:任何人都不能搜索到我的用户信息,名称、头像除外。 |
UserProfileVisibilityEveryone | 所有人:应用中任何用户都可以查看到我的用户信息。 |
UserProfileVisibilityFriendVisible | 仅好友可见:仅我的好友列表中用户可以查看到我的用户信息。 |
用户权限说明
AppKey 下默认用户信息访问权限为 都不可见,用户级别默认用户信息访问权限为 未设置。当您都未设置也就是二者访问权限都为默认值时, 您仅可查看他人的用户名和头像信息。
下面列举了 AppKey 级 权限与 用户级 权限综合说明:
| AppKey 级权限 | 用户级权限 | 结果 |
|---|---|---|
| 都不可见、仅好友可见、所有人可见 | 未设置 | 以 AppKey 级权限设置为准 |
| 都不可见、仅好友可见、所有人可见 | 设置为(都不可见、仅好友可见、所有人可见) | 以用户级权限设置为准 |
设置用户权限
您可以使用 updateMyUserProfileVisibility 方法设置您自己的用户信息访问权限。
示例代码
let visibility: UserProfileVisibility = UserProfileVisibility.UserProfileVisibilityEveryone;
IMEngine.getInstance().updateMyUserProfileVisibility(visibility).then((result: IAsyncResult<void>) => {
if (result.code === EngineError.Success) {
// 设置用户信息访问权限成功
} else {
// 设置用户信息访问权限失败
}
});
获取用户权限
您可以使用 getMyUserProfileVisibility 方法获取您自己的用户信息访问权限。
示例代码
IMEngine.getInstance().getMyUserProfileVisibility().then((result: IAsyncResult<UserProfileVisibility>) => {
if (result.code === EngineError.Success) {
// 获取用户信息访问权限成功
} else {
// 获取用户信息访问权限失败
}
});
搜索用户
按用户应用号精确搜索
您可以使用 searchUserProfileByUniqueId 方法通过用户应用号搜索用户。