登录
一、登录并回写
完成初始化后调用 showLogin。SDK 处理登录及授权码换 token,回调直接返回 LoginUser。
登录参数
| 参数 | 类型 | 说明 |
|---|---|---|
| activity | Activity | 当前游戏 Activity |
| req | SendAuth.Req | 授权请求,字段见下表 |
| listener | NiuxMobileGameLoginListener | 必须实现登录结果和退出登录两个回调 |
| req 字段 | 类型 | 说明 |
|---|---|---|
| scope | String | 平台分配的完整权限,多个权限用空格分隔 |
| state | String | 游戏自定义的授权请求标识 |
| responseType | String | 固定为 code |
调用示例
以下代码放在游戏 Activity 中,替换 scope 后调用 showGameLogin()。
import com.xovs.common.auth.base.SendAuth;
import com.xunlei.niux.mobilegame.sdk.platform.NiuxMobileGame;
import com.xunlei.niux.mobilegame.sdk.platform.NiuxMobileGameLoginListener;
import com.xunlei.niux.mobilegame.sdk.vo.LoginUser;
private final NiuxMobileGameLoginListener loginListener = new NiuxMobileGameLoginListener() {
@Override
public void onUserLoginResult(boolean isSuccess, int errorCode,
String errorDesc, LoginUser loginUser) {
runOnUiThread(() -> {
if (isSuccess && loginUser != null) {
NiuxMobileGame.getInstance().onLoginResult(true, loginUser, false);
// 建立游戏后端会话,再进入选服/选角页面。
} else {
NiuxMobileGame.getInstance().onLoginResult(false, null, false);
// 展示 errorCode/errorDesc,允许重试。
}
});
}
@Override
public void onUserLogout() {
// 清理游戏会话并返回登录页;不要再次调用 onLogout。
}
};
private void showGameLogin() {
SendAuth.Req req = new SendAuth.Req();
req.scope = "平台分配的完整 scope";
req.state = java.util.UUID.randomUUID().toString();
req.responseType = "code";
NiuxMobileGame.getInstance().showLogin(this, req, loginListener);
}
登录回调
| 参数 | 类型 | 说明 |
|---|---|---|
| isSuccess | boolean | 是否登录成功 |
| errorCode | int | 错误码 |
| errorDesc | String | 错误描述 |
| loginUser | LoginUser | 用户与凭证信息,仅成功时使用 |
| LoginUser 字段 | 类型 | 说明 |
|---|---|---|
| userId | String | 迅雷用户 ID,不是角色 ID |
| userName | String | 用户昵称,可按需设置 |
| accessToken | String | 访问凭证 |
| refreshToken | String | 刷新凭证 |
| tokenType | String | 凭证类型,默认 Bearer |
| expiresIn | int | 凭证有效期,单位秒 |
| isAutoLogin | boolean | 是否自动登录 |
登录回写
| onLoginResult 参数 | 类型 | 说明 |
|---|---|---|
| success | boolean | 登录成功传 true,失败传 false |
| loginUser | LoginUser | 成功传 SDK 返回的用户对象,失败传 null |
| isAutoAuthor | boolean | 普通接入传 false |
**成功、失败都要回写,示例已包含此步骤。**保留 SDK 返回的凭证字段;成功后 SDK 自动上报启动并尝试展示悬浮窗,不需重复调用。
需要建立游戏后端会话时,由游戏后端验证 accessToken 与 userId 的对应关系。当前回调不提供授权码 code。使用当前凭证可调用 getLoginUser();收到 onTokenExpired 时处理重新登录。
二、退出与切换账号
- 正在角色会话中时,先调用
quitGame(serverId, userId)停止心跳,userId使用迅雷用户 ID。 - 调用
NiuxMobileGame.getInstance().onLogout(),清理游戏自身会话。 - 切换账号时,再调用登录流程。
onLoginResult(false, ...) 不会清理已有账号;onLogout() 不会替代 quitGame() 停止心跳。角色会话步骤见行为上报。
Activity 销毁时移除监听器:
NiuxMobileGame.getInstance().removeLoginListener(loginListener);