用户资料托管

概述

SDK 提供了用户帐号资料管理。以下几个接口仅当选择网易云通信托管用户资料时有效,如果开发者不希望网易云通信获取自己的用户数据,则需自行维护用户资料。

nim_user_def.h 里定义了用户信息的字段 kUInfoKeyXXX。

用户信息变更会通过注册的全用户信息变更通知回调告知APP:

C++

void OnUserInfoChange(const std::list<nim::UserNameCard> &uinfo_list)
{
    for (auto& info : uinfo_list)
    {

        if (info.ExistValue(nim::kUserNameCardKeyName) || info.ExistValue(nim::kUserNameCardKeyIconUrl)) //用户名或头像变化了
            ···;
        if (info.ExistValue((nim::UserNameCardValueKey)(nim::kUserNameCardKeyAll - nim::kUserNameCardKeyName - nim::kUserNameCardKeyIconUrl))) //用户其他信息变化了
            ···;
    }
}

void foo()
{
    //向SDK注册监听用户名片变化
    nim::User::RegUserNameCardChangedCb(&OnUserInfoChange);
}

C#

void OnUserNameCardChanged(object sender, UserNameCardChangedArgs e)
{

}

void foo()
{
    NIM.User.UserAPI.UserNameCardChangedHandler += OnUserNameCardChanged;
}

C

void CallbackUserNameCardChange(const char *result_json, const char *json_extension, const void *callback)
{
    // 解析result_json
}

typedef void (*nim_user_reg_user_name_card_changed_cb)(const char *json_extension, nim_user_name_card_change_cb_func cb, const void *user_data);

void foo()
{
    nim_user_reg_user_name_card_changed_cb func = (nim_user_reg_user_name_card_changed_cb) GetProcAddress(hInst, "nim_user_reg_user_name_card_changed_cb");
    func("", &CallbackUserNameCardChange, nullptr);
}

获取本地用户信息

C++

void OnGetUserCard(const std::list<nim::UserNameCard> &json_result)
{
    for (auto& info : json_result)
    {
        ···
    }
}

void foo()
{
    std::list<std::string> account_list;
    account_list.push_back("test1");
    account_list.push_back("test2");
    nim::User::GetUserNameCard(account_list, &OnGetUserCard);
}

C#

UserAPI.GetUserNameCard(new List<string>() {"test1", "test2"}, (ret) =>
{
    if (ret.Any())
    {

    }
});

C

void CallbackGetUserNameCard(const char *result_json, const char *json_extension, const void *callback)
{
    // 解析result_json
}

typedef void (*nim_user_get_user_name_card)(const char *accids, const char *json_extension, nim_user_get_user_name_card_cb_func cb, const void *user_data);

void foo(const std::list<std::string>& accids)
{
    nim_user_get_user_name_card func = (nim_user_get_user_name_card) GetProcAddress(hInst, "nim_user_get_user_name_card");

    Json::Value values;
    for (auto iter = accids.cbegin(); iter != accids.cend(); ++iter)
        values.append(*iter);

    Json::FastWriter fw;

    func(fw.write(values).c_str(), "", &CallbackGetUserNameCard, nullptr);
}

获取服务器用户信息

单次查询限制150人。

C++

void OnGetUserCard(const std::list<nim::UserNameCard> &json_result)
{
    for (auto& info : json_result)
    {
        ···
    }
}

void foo()
{
    std::list<std::string> account_list;
    account_list.push_back("test1");
    account_list.push_back("test2");
    nim::User::GetUserNameCardOnline(account_list, &OnGetUserCard);
}

C#

NIM.User.UserAPI.QueryUserNameCardOnline(new List<string>() {"test1", "test2"}, (ret) =>
{
    if (ret == null || !ret.Any())
    {
        return;
    }

});

C

void CallbackGetUserNameCard(const char *result_json, const char *json_extension, const void *callback)
{
    // 解析result_json
}

typedef void (*nim_user_get_user_name_card_online)(const char *accids, const char *json_extension, nim_user_get_user_name_card_cb_func cb, const void *user_data);

void foo(const std::list<std::string>& accids)
{
    nim_user_get_user_name_card_online func = (nim_user_get_user_name_card_online) GetProcAddress(hInst, "nim_user_get_user_name_card_online");

    Json::Value values;
    for (auto iter = accids.cbegin(); iter != accids.cend(); ++iter)
        values.append(*iter);

    Json::FastWriter fw;

    func(fw.write(values).c_str(), "", &CallbackGetUserNameCard, nullptr);
}

编辑用户资料

例:

C++

 void OnUpdateMyInfo(nim::NIMResCode res) 
{
    if (res == nim::kNIMResSuccess)
    {

    }
}

void foo()
{
    nim::UserNameCard info;
    info.SetName("new_name");
    nim::User::UpdateMyUserNameCard(info, &OnUpdateMyInfo);
}

C#

void foo()
{
    NIM.User.UserNameCard user_card = new NIM.User.UserNameCard;
    user_card.NickName = "new_name";
    NIM.User.UserAPI.UpdateMyCard(user_card, (a) =>
    {
        if (a == NIM.ResponseCode.kNIMResSuccess)
        {
            MessageBox.Show("修改成功");
        }
        else
        {
            MessageBox.Show("修改失败");
        }
    });
}

C

void CallbackUpdateNameCard(int res_code, const char *json_extension, const void *callback)
{
    if (res_code == kNIMResSuccess)
    ...
    else
    ...
}

typedef void (*nim_user_update_my_user_name_card)(const char *info_json, const char *json_extension, nim_user_update_my_name_card_cb_func cb, const void *user_data);

void foo()
{
    //修改昵称
    Json::Value values;
    values[kUInfoKeyAccid] = "litianyi02";
    values[kUInfoKeyName] = "修改后的大意的昵称";

    nim_user_update_my_user_name_card func = (nim_user_update_my_user_name_card) GetProcAddress(hInst, "nim_user_update_my_user_name_card");
    func(values.toStyledString().c_str(), nullptr, &CallbackUpdateNameCard, nullptr);
}