UMAuthListener authListener = new UMAuthListener() {
/**
* @desc 授权开始的回调
* @param platform 平台名称
*/
@Override
public void onStart(SHARE_MEDIA platform) {
WaitDialog.show("正在绑定中...");
}
/**
* @desc 授权成功的回调
* @param platform 平台名称
* @param action 行为序号,开发者用不上
* @param data 用户资料返回
*/
@Override
public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
// 获取所有键值对对象的集合
Set<Map.Entry<String, String>> set = data.entrySet();
// 遍历键值对对象的集合,得到每一个键值对对象
for (Map.Entry<String, String> me : set) {
// 根据键值对对象获取键和值
String key = me.getKey();
String value = me.getValue();
System.out.println(key + "---" + value);
}
switch (platform.getName()) {
case "wxsession":
mmkv.encode("USER_HEAD",data.get("iconurl"));
mmkv.encode("USER_NAME",data.get("name"));
thirdPartyData = new HashMap<String, Object>();
thirdPartyData.put("expires_in", data.get("expires_in"));
thirdPartyData.put("openid", data.get("openid"));
thirdPartyData.put("access_token", data.get("accessToken"));
associateWithAuthData("Wechat",thirdPartyData,data.get("name"));
break;
case "qq":
mmkv.encode("USER_HEAD",data.get("iconurl"));
mmkv.encode("USER_NAME",data.get("name"));
thirdPartyData = new HashMap<String, Object>();
thirdPartyData.put("expires_in", data.get("expires_in"));
thirdPartyData.put("openid", data.get("openid"));
thirdPartyData.put("access_token", data.get("accessToken"));
associateWithAuthData("Tencent",thirdPartyData,data.get("name"));
break;
case "sina":
mmkv.encode("USER_HEAD",data.get("iconurl"));
mmkv.encode("USER_NAME",data.get("name"));
thirdPartyData = new HashMap<String, Object>();
thirdPartyData.put("expires_in", data.get("expiration"));
thirdPartyData.put("openid", data.get("uid"));
thirdPartyData.put("access_token", data.get("accessToken"));
associateWithAuthData("Sina",thirdPartyData,data.get("name"));
break;
}
}
/**
* @desc 授权失败的回调
* @param platform 平台名称
* @param action 行为序号,开发者用不上
* @param t 错误原因
*/
@Override
public void onError(SHARE_MEDIA platform, int action, Throwable t) {
PopTip.show(t.getMessage());
WaitDialog.dismiss();
}
/**
* @desc 授权取消的回调
* @param platform 平台名称
* @param action 行为序号,开发者用不上
*/
@Override
public void onCancel(SHARE_MEDIA platform, int action) {
PopTip.show("取消登录");
WaitDialog.dismiss();
}
};
private void associateWithAuthData(String platform , Map<String, Object> thirdData,String username) {
switch (platform) {
case "Wechat":
user.put("wechatName",username);
break;
case "Tencent":
user.put("tencentName",username);
break;
case "Sina":
user.put("sinaName",username);
break;
}
user.associateWithAuthData(thirdData, platform).subscribe(new Observer<LCUser>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(LCUser avUser) {
WaitDialog.dismiss();
PopTip.show("绑定成功");
initView();
}
@Override
public void onError(Throwable e) {
System.out.println("绑定失败:" + e.getMessage());
}
@Override
public void onComplete() {
}
});
}