账号及用户 SDK
更新时间:2018-03-15 20:34:06
概述
账号和用户 SDK,提供账号能力,包括注册,登录,登出,获取账号,会话管理,人机校验等功能。同时提供 UI 定制的能力。
依赖SDK | 概述 |
---|---|
API 通道 | 提供API通道能力 |
初始化
初始化 SDK 前,请先初始化 API 通道SDK,请参见 API 通道 - 初始化。
SDK 运行前,需要通过 ConfigManager
完成对 SDK 的配置。
/**
* 设置当前的环境信息 -- 目前只支持线上环境
* Environment.TEST 日常
* Environment.PRE 预发
* Environment.ONLINE 线上
**/
ConfigManager.getInstance().setEnvironment(Environment.ONLINE);
//设置图片后缀
ConfigManager.getInstance().setSecGuardImagePostfix("114d");
//设置使用单张图片
ConfigManager.getInstance().setUseSingleImage(true);
//使用API网关
ConfigManager.getInstance().setAPIGateway(true);
//打开调试,会有详细日志输出
OpenAccountSDK.turnOnDebug();
//调用异步初始化
OpenAccountSDK.asyncInit(getApplicationContext(), new InitResultCallback() {
@Override
public void onSuccess() {
ToastUtil.show(getApplicationContext(), "OpenAccountSDK 初始化成功");
}
@Override
public void onFailure(int code, String msg) {
ToastUtil.show(getApplicationContext(), "OpenAccountSDK 初始化失败 : " + msg);
}
});
OpenAccountSDK 提供的接口,请参见 API Doc
使用方式
登录
public void login() {
OpenAccountUIService openAccountService = OpenAccountSDK.getService(OpenAccountUIService.class);
try {
openAccountService.showLogin(context, new LoginCallback() {
@Override
public void onSuccess(OpenAccountSession openAccountSession) {
ToastUtil.show(getApplicationContext(), "登录成功");
}
@Override
public void onFailure(int i, String s) {
ToastUtil.show(getApplicationContext(), "登录失败");
}
});
} catch (Exception e) {
ToastUtil.show(getApplicationContext(), "登录异常 : " + e.toString());
}
}
退出登陆
public void logout() {
OpenAccountService openAccountService = OpenAccountSDK.getService(OpenAccountService.class);
try {
openAccountService.logout(context, new LogoutCallback() {
@Override
public void onSuccess() {
ToastUtil.show(getApplicationContext(), "登出成功");
}
@Override
public void onFailure(int i, String s) {
ToastUtil.show(getApplicationContext(), "登出失败 : " + s);
}
});
} catch (Exception e) {
ToastUtil.show(getApplicationContext(), "登出异常 : " + e.toString());
}
}
刷新会话
OpenAccountSessionService openAccountSessionService = OpenAccountSDK.getService(OpenAccountSessionService.class);
openAccountSessionService.refreshSession(true);
获取会话ID
private long getSessionId() {
SessionManagerService sessionManagerService = OpenAccountSDK.getService(SessionManagerService.class);
if (sessionManagerService == null) {
return 0;
}
return sessionManagerService.getSessionId();
}
获取用户信息
public User getUserInfo() {
OpenAccountService openAccountService = OpenAccountSDK.getService(OpenAccountService.class);
OpenAccountSession session = openAccountService.getSession();
return session == null ? null : session.getUser();
}
三方自有账号接入
可以选择自由账号通过OAuth2.0协议接入,前提需要做好配置,流程参考 账号及用户服务配置
OpenAccountService service = OpenAccountSDK.getService(OpenAccountService.class);
service.authCodeLogin(OALoginActivity.this, authCode, new LoginCallback() {
@Override
public void onSuccess(OpenAccountSession openAccountSession) {
Toast.makeText(getApplicationContext(), "auth 授权登录 成功 ", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int code, String msg) {
Toast.makeText(getApplicationContext(), "auth授权登录 失败 code = " + code + " message = " + msg, Toast.LENGTH_SHORT).show();
}
});
混淆配置
-keep class com.alibaba.sdk.android.*