# 登录

本章节将介绍如何初始化 JC SDK 并登录。

# 初始化

在调用初始化方法前需要先继承 JCClientCallback 对象,然后实现 JCClientCallback 对象中的纯虚函数。

class JCManager : public JCClientCallback
{
public:
    //单例管理类
    JCManager();
    ~JCManager();

    static std::shared_ptr<JCManager> shared();
    bool initialize();
    void uninitialize();
    //登录回调
    virtual void onLogin(bool result, JCClientReason reason);
    //登出回调
    virtual void onLogout(JCClientReason reason);
    //登录状态变化回调
    virtual void onClientStateChange(JCClientState state, JCClientState oldState);

public:
    //JCClient 对象
    JCClient* client;
};

然后在主线程调用 JCClientImpl 类中的 createJCClient (opens new window) 方法,传入获取到的 appKey 进行 JC SDK 的初始化。

bool JCManager::initialize()
{
    // 初始化
    client = createJCClient("用户 appKey", this, NULL);
    if (client->getState() == JCClientStateNotInit) {
        return false;
    }
    return true;
}

// 登录结果回调
void JCManager::onLogin(bool result, JCClientReason reason) {
}
// 登出结果回调
void JCManager::onLogout(JCClientReason reason) {
}
// 登录状态变化回调
void JCManager::onClientStateChange(JCClientState state, JCClientState oldState) {
}

初始化成功后,JCClient 状态变为 JCClientStateIdle(未登录状态)。

# 发起登录

SDK 初始化之后,即可进行登录的集成。登录接口调用流程如下所示:

../../../../_images/ios_login.jpg

先创建 JCClientLoginParam (opens new window) 对象以设置登录参数。然后调用 login (opens new window) 发起登录:

//登录
void JCSampleDlg::OnBnClickedButtonLogin()
{
    int state = JCManager::shared()->client->getState();
    if (state == JCClientStateIdle) {
        JCClientLoginParam* loginParam = new JCClientLoginParam();
        // 发起登录
        JCManager::shared()->client->login("userID", "password", loginParam);
    }
    else {
        // 如果已经登录则登出
        JCManager::shared()->client->logout();
    }
}

TIP

  1. userID 用户名不能为空,可由英文、数字和 +-_. 组成(特殊字符不能作为第一个字符),大小写不敏感,长度不能超过 64 个字符。

  2. password 密码,不能为空,长度不能超过 128 字符。

  3. 调用该接口返回 true 时只代表调用接口成功,并不代表登录成功。登录的结果会通过 onLogin 回调上报。

调用接口成功后,首先会触发登录状态改变回调 onClientStateChange (opens new window) ,您可以在该回调中执行逻辑操作。

void JCManager::onClientStateChange(JCClientState state, JCClientState oldState) {
     if (state == JCClientStateIdle) { // 未登录
       ...
    } else if (state == JCClientStateLogining) { // 正在登录
       ...
    } else if (state == JCClientStateLogined) { // 登录成功
       ...
    } else if (state == JCClientStateLogouting) { // 登出中
       ...
    }
}

之后触发 onLogin (opens new window) 回调。您可以在该回调中执行逻辑操作。

void JCManager::onLogin(bool result, JCClientReason reason) {
    if (result) {/// 登录成功
        ...
    }
    if (reason == JCClientReasonAuth) {// 账号密码错误
        ...
    }
}

登录成功后,JCClientState 状态从 JCClientStateIdle(未登录)变为 JCClientStateLogined(登录成功)。SDK 会自动保持与服务器的连接状态,直到用户主动调用登出接口,或者因为帐号在其他设备登录导致该设备登出。登录成功/失败原因 参考 JCClientReason (opens new window)

# 登出

登出接口调用流程如下所示:

../../../../_images/ios_logout.jpg

调用 logout (opens new window) 发起登出。

JCManager::shared()->client->logout();

登出同样会触发登录状态改变(onClientStateChange)回调,之后将通过 onLogout 回调上报登出结果:

void JCManager::onLogout(JCClientReason reason) {
    if (reason == JCClientReasonServerLogout) {// 强制登出
        ...
    }
}

登出原因请参考:JCClientReason (opens new window)

登出成功后,JCClientState 状态从 JCClientStateLogined(登录成功) 变为 JCClientStateIdle(未登录)。

最后更新时间: 2023/5/22 17:31:39