合并UserManager和RefreshTokenManager
This commit is contained in:
@ -1,12 +1,37 @@
|
||||
package com.gh.gamecenter.manager;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.gh.common.constant.Constants;
|
||||
import com.gh.common.util.DataUtils;
|
||||
import com.gh.common.util.DeviceUtils;
|
||||
import com.gh.common.util.GsonUtils;
|
||||
import com.gh.gamecenter.BuildConfig;
|
||||
import com.gh.gamecenter.entity.CommunityEntity;
|
||||
import com.gh.gamecenter.entity.LoginTokenEntity;
|
||||
import com.gh.gamecenter.entity.TokenEntity;
|
||||
import com.gh.gamecenter.entity.UserInfoEntity;
|
||||
import com.gh.gamecenter.eventbus.EBReuse;
|
||||
import com.gh.gamecenter.eventbus.EBShowDialog;
|
||||
import com.gh.gamecenter.personal.PersonalFragment;
|
||||
import com.gh.gamecenter.retrofit.Response;
|
||||
import com.gh.gamecenter.retrofit.RetrofitManager;
|
||||
import com.gh.gamecenter.retrofit.service.ApiService;
|
||||
import com.gh.gamecenter.retrofit.service.UserseaService;
|
||||
import com.gh.gamecenter.user.AppDatabase;
|
||||
import com.halo.assistant.HaloApp;
|
||||
import com.lightgame.utils.Utils;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
import retrofit2.HttpException;
|
||||
|
||||
/**
|
||||
* Created by khy on 29/11/17.
|
||||
@ -14,17 +39,19 @@ import com.gh.gamecenter.entity.UserInfoEntity;
|
||||
|
||||
public class UserManager {
|
||||
|
||||
private final String COMMUNITY_ID = "CommunityId";
|
||||
private final String COMMUNITY_NAME = "CommunityName";
|
||||
private final String COMMUNITY_KEY = "CommunityKey";
|
||||
|
||||
private static volatile UserManager singleton;
|
||||
|
||||
private SharedPreferences mSp;
|
||||
|
||||
private AppDatabase mDatabase;
|
||||
private UserseaService mUserseaService;
|
||||
private ApiService mApiService;
|
||||
|
||||
private UserInfoEntity mUserInfoEntity;
|
||||
|
||||
private LoginTokenEntity mLoginTokenEntity;
|
||||
|
||||
private String mCommunityId;
|
||||
private String mCommunityName;
|
||||
private CommunityEntity mCommunityData;
|
||||
|
||||
public static UserManager getInstance() {
|
||||
if (singleton == null) {
|
||||
@ -35,6 +62,13 @@ public class UserManager {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
private UserManager() {
|
||||
mSp = PreferenceManager.getDefaultSharedPreferences(HaloApp.getInstance().getApplication());
|
||||
mDatabase = AppDatabase.getInstance(HaloApp.getInstance().getApplication());
|
||||
mUserseaService = RetrofitManager.getInstance(HaloApp.getInstance().getApplication()).getUsersea();
|
||||
mApiService = RetrofitManager.getInstance(HaloApp.getInstance().getApplication()).getApi();
|
||||
}
|
||||
|
||||
public LoginTokenEntity getLoginTokenEntity() {
|
||||
return mLoginTokenEntity;
|
||||
}
|
||||
@ -59,47 +93,167 @@ public class UserManager {
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
String userId = null;
|
||||
if (mUserInfoEntity != null) {
|
||||
userId = mUserInfoEntity.getUserId();
|
||||
return mUserInfoEntity.getUserId();
|
||||
}
|
||||
return userId == null ? "" : userId;
|
||||
return "";
|
||||
}
|
||||
|
||||
public void setCommunityId(Context context, String communityId, String communityName) {
|
||||
if (TextUtils.isEmpty(mCommunityId) || !mCommunityId.equals(communityId)) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(COMMUNITY_ID, communityId).apply();
|
||||
public void setCommunityData(CommunityEntity community) {
|
||||
if (community != null) {
|
||||
mSp.edit().putString(COMMUNITY_KEY, GsonUtils.Companion.getInstance().toJson(community)).apply();
|
||||
}
|
||||
if (TextUtils.isEmpty(mCommunityName) || !mCommunityName.equals(communityName)) {
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(COMMUNITY_NAME, communityName).apply();
|
||||
}
|
||||
this.mCommunityId = communityId;
|
||||
this.mCommunityName = communityName;
|
||||
this.mCommunityData = community;
|
||||
}
|
||||
|
||||
public String getCommunityId(Context context) {
|
||||
if (TextUtils.isEmpty(mCommunityId)) {
|
||||
mCommunityId = PreferenceManager.getDefaultSharedPreferences(context).getString(COMMUNITY_ID, "");
|
||||
public CommunityEntity getCommunity() {
|
||||
if (mCommunityData == null || TextUtils.isEmpty(mCommunityData.getId())) {
|
||||
String communityJson = mSp.getString(COMMUNITY_KEY, null);
|
||||
mCommunityData = TextUtils.isEmpty(communityJson) ? new CommunityEntity()
|
||||
: GsonUtils.Companion.getInstance().fromJsonBean(communityJson, CommunityEntity.class);
|
||||
}
|
||||
|
||||
// if (TextUtils.isEmpty(mCommunityId)) mCommunityId = "5a32405b2397ab000f688de1";
|
||||
return mCommunityId;
|
||||
}
|
||||
|
||||
public String getCommunityName(Context context) {
|
||||
if (TextUtils.isEmpty(mCommunityName)) {
|
||||
mCommunityName = PreferenceManager.getDefaultSharedPreferences(context).getString(COMMUNITY_NAME, "");
|
||||
}
|
||||
// if (TextUtils.isEmpty(mCommunityName)) mCommunityName = "少年三国志";
|
||||
return mCommunityName;
|
||||
}
|
||||
|
||||
public String getTokenId(Context context) {
|
||||
return PreferenceManager.getDefaultSharedPreferences(context).getString(Constants.LOGIN_TOKEN_ID, null);
|
||||
return mCommunityData;
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
mUserInfoEntity = null;
|
||||
mLoginTokenEntity = null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param accessToken
|
||||
* @param callBack
|
||||
*/
|
||||
public synchronized void refreshToken(String accessToken, final refreshCallBack callBack) {
|
||||
|
||||
// LoginTokenEntity 为空,可能已经退出登录
|
||||
LoginTokenEntity tokenEntity = getLoginTokenEntity();
|
||||
if (tokenEntity == null) {
|
||||
callBack.onLoginFailure();
|
||||
return;
|
||||
}
|
||||
|
||||
// 对比AccessToken 如果接口传的token与UserManager的token不一致 则已被刷新过
|
||||
if (!accessToken.equals(tokenEntity.getAccessToken().getValue())) {
|
||||
callBack.onLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
// 判断RefreshToken是否过期
|
||||
TokenEntity refreshToken = tokenEntity.getRefreshToken();
|
||||
if (refreshToken.getExpire() < Utils.getTime(HaloApp.getInstance().getApplication())) {
|
||||
Utils.toast(HaloApp.getInstance().getApplication(), "账号过期,请重新登录!");
|
||||
EventBus.getDefault().post(new EBReuse(PersonalFragment.LOGOUT_TAG));
|
||||
callBack.onLoginFailure();
|
||||
return;
|
||||
}
|
||||
|
||||
RequestBody body = null;
|
||||
try {
|
||||
JSONObject device = DeviceUtils.getLoginDevice(HaloApp.getInstance().getApplication());
|
||||
JSONObject content = new JSONObject();
|
||||
content.put("refresh_token", refreshToken.getValue());
|
||||
content.put("device", device);
|
||||
body = RequestBody.create(MediaType.parse("application/json"), content.toString());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mUserseaService
|
||||
.refreshToken(body)
|
||||
.subscribe(new Response<LoginTokenEntity>() {
|
||||
@Override
|
||||
public void onResponse(LoginTokenEntity response) {
|
||||
super.onResponse(response);
|
||||
|
||||
saveLoginToken(response, HaloApp.getInstance().getApplication());
|
||||
|
||||
refreshUserInfo(callBack, response.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
if (callBack != null) {
|
||||
callBack.onLoginFailure();
|
||||
}
|
||||
|
||||
if (e != null) {
|
||||
try {
|
||||
String string = e.response().errorBody().string();
|
||||
JSONObject content = new JSONObject(string);
|
||||
int code = content.getInt("code");
|
||||
if (code == 400802) { // 其他设备登录了该账号
|
||||
EventBus.getDefault().post(new EBShowDialog("loginException", string)); // 打开提示框
|
||||
}
|
||||
|
||||
if (code == 400802 || code == 400401) { // 自动注销
|
||||
EventBus.getDefault().post(new EBReuse(PersonalFragment.LOGOUT_TAG));
|
||||
DataUtils.onMtaEvent(HaloApp.getInstance().getApplication(), "登录异常统计_自动登录"
|
||||
, "错误码_RefreshToken", code + "_" + refreshToken.getValue());
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void refreshUserInfo(final refreshCallBack callBack, final String cacheId) {
|
||||
mApiService
|
||||
.getRetryUserInfo(BuildConfig.API_HOST + "users:validate", "retry")
|
||||
.subscribe(new Response<UserInfoEntity>() {
|
||||
@Override
|
||||
public void onResponse(UserInfoEntity response) {
|
||||
super.onResponse(response);
|
||||
response.setId(cacheId);
|
||||
saveUserInfo(response);
|
||||
|
||||
if (callBack != null) {
|
||||
callBack.onLogin();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
if (callBack != null) {
|
||||
callBack.onLoginFailure();
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void saveLoginToken(final LoginTokenEntity tokenEntity, final Context context) {
|
||||
LoginTokenEntity loginTokenEntity = getLoginTokenEntity();
|
||||
if (loginTokenEntity != null) {
|
||||
tokenEntity.setLoginType(loginTokenEntity.getLoginType());
|
||||
tokenEntity.setId(loginTokenEntity.getId());
|
||||
}
|
||||
|
||||
setLoginTokenEntity(tokenEntity);
|
||||
if (TextUtils.isEmpty(tokenEntity.getId())) return;
|
||||
if (mDatabase.loginTokenDao().updateToken(tokenEntity) <= 0) {
|
||||
mDatabase.loginTokenDao().addToken(tokenEntity);
|
||||
}
|
||||
mSp.edit().putString(Constants.LOGIN_TOKEN_ID, tokenEntity.getId()).apply();
|
||||
}
|
||||
|
||||
private void saveUserInfo(final UserInfoEntity userInfo) {
|
||||
setUserInfoEntity(userInfo);
|
||||
if (TextUtils.isEmpty(userInfo.getId())) return;
|
||||
if (mDatabase.userInfoDao().updateUserInfo(userInfo) <= 0) {
|
||||
mDatabase.userInfoDao().addUserInfo(userInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public interface refreshCallBack {
|
||||
void onLogin();
|
||||
|
||||
void onLoginFailure();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user