Native App接入WindVane小程序容器或uni-app小程序容器后,可以在运行时将登录用户关联到小程序容器。
除非App用户尚未登录的情况下,否则在App用户登录或切换账号时,App必须在运行时将登录用户关联到小程序容器,关联后小程序容器会在以下场景中使用用户信息(仅UserId):
判断当前用户是否具备相应的设备授权
切换用户时SDK会根据用户信息是否发生变化从而自动清理掉WindVane小程序的Cookie
PV、UV等数据上报时(需引入上报插件)
在应用开放平台使用小程序白名单灰度功能
关联用户
小程序白名单灰度功能和自动清理WindVane小程序缓存功能需要调用以下接口关联用户才能使用。关联代码如下:
ServiceManager.getInstance().registerService(IUserInfoService.class.getName(), new IUserInfoService() {
@Override
public UserInfo getUserInfo() {
//根据登录情况,返回UserInfo,未登录返回null。
UserInfo userInfo = new UserInfo();
userInfo.setUserId(userId);
return userInfo;
}
});判断设备权限
private boolean isAuthorized() {
IUserInfoService userInfoService = ServiceManager.getInstance()
.getService(IUserInfoService.class.getName());
// ...
UserInfo userInfo = userInfoService.getUserInfo();
// ...
return AuthorizationManagement
.getInstance(mContext)
.isAuthorized(innerScope, userInfo.getUserId(), appId);
}
清除Cookie
private void clearMiniAppCookies(...) {
IUserInfoService userInfoService = ServiceManager.getInstance()
.getService(IUserInfoService.class.getName());
// ...
UserInfo userInfo = userInfoService.getUserInfo();
// ...
if (isDifferentUser(userInfo.getUserId())) {
clearCookie();
}
}
数据上报
private void reportData(...) {
IUserInfoService userInfoService = ServiceManager.getInstance()
.getService(IUserInfoService.class.getName());
// ...
UserInfo userInfo = userInfoService.getUserInfo();
// ...
report(userInfo.getUserId());
}