250 lines
9.0 KiB
Java
250 lines
9.0 KiB
Java
package com.gh.gamecenter.manager;
|
|
|
|
import android.content.SharedPreferences;
|
|
import android.os.Build;
|
|
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.halo.assistant.HaloApp;
|
|
import com.lightgame.utils.Util_System_Phone_State;
|
|
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.
|
|
*/
|
|
|
|
public class UserManager {
|
|
|
|
private final String COMMUNITY_KEY = "CommunityKey";
|
|
|
|
private static volatile UserManager singleton;
|
|
|
|
private SharedPreferences mSp;
|
|
|
|
private ApiService mApiService;
|
|
|
|
private UserInfoEntity mUserInfoEntity;
|
|
private LoginTokenEntity mLoginTokenEntity;
|
|
private CommunityEntity mCommunityData;
|
|
|
|
public static UserManager getInstance() {
|
|
if (singleton == null) {
|
|
synchronized (UserManager.class) {
|
|
singleton = new UserManager();
|
|
}
|
|
}
|
|
return singleton;
|
|
}
|
|
|
|
private UserManager() {
|
|
mSp = PreferenceManager.getDefaultSharedPreferences(HaloApp.getInstance().getApplication());
|
|
mApiService = RetrofitManager.getInstance(HaloApp.getInstance().getApplication()).getApi();
|
|
}
|
|
|
|
public LoginTokenEntity getLoginTokenEntity() {
|
|
return mLoginTokenEntity;
|
|
}
|
|
|
|
public void setLoginTokenEntity(LoginTokenEntity loginTokenEntity) {
|
|
mLoginTokenEntity = loginTokenEntity;
|
|
}
|
|
|
|
public UserInfoEntity getUserInfoEntity() {
|
|
return mUserInfoEntity;
|
|
}
|
|
|
|
public void setUserInfoEntity(UserInfoEntity userInfoEntity) {
|
|
mUserInfoEntity = userInfoEntity;
|
|
}
|
|
|
|
public String getToken() {
|
|
if (mLoginTokenEntity != null) {
|
|
return mLoginTokenEntity.getAccessToken().getValue();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String getUserId() {
|
|
if (mUserInfoEntity != null) {
|
|
return mUserInfoEntity.getUserId();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public void setCommunityData(CommunityEntity community) {
|
|
if (community != null) {
|
|
mSp.edit().putString(COMMUNITY_KEY, GsonUtils.Companion.getInstance().toJson(community)).apply();
|
|
}
|
|
this.mCommunityData = community;
|
|
}
|
|
|
|
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);
|
|
}
|
|
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();
|
|
}
|
|
mApiService
|
|
.refreshToken("tokens:refresh", body)
|
|
.subscribe(new Response<LoginTokenEntity>() {
|
|
@Override
|
|
public void onResponse(LoginTokenEntity response) {
|
|
super.onResponse(response);
|
|
|
|
saveLoginToken(response);
|
|
|
|
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()
|
|
, "网络状态", DeviceUtils.getNetwork(HaloApp.getInstance().getApplication())
|
|
, "用户机型", Build.MODEL
|
|
, "设备IMEI", Util_System_Phone_State.getDeviceId(HaloApp.getInstance().getApplication()));
|
|
}
|
|
} catch (Exception e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void refreshUserInfo(final refreshCallBack callBack, final String cacheId) {
|
|
mApiService
|
|
.getRetryUserInfo(BuildConfig.API_HOST + "tokens: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) {
|
|
LoginTokenEntity loginTokenEntity = getLoginTokenEntity();
|
|
if (loginTokenEntity != null) {
|
|
tokenEntity.setLoginType(loginTokenEntity.getLoginType());
|
|
tokenEntity.setId(loginTokenEntity.getId());
|
|
}
|
|
setLoginTokenEntity(tokenEntity);
|
|
mSp.edit().putString(Constants.USER_TOKEN_KEY, GsonUtils.Companion.getInstance().toJson(tokenEntity)).apply();
|
|
}
|
|
|
|
private void saveUserInfo(final UserInfoEntity userInfo) {
|
|
setUserInfoEntity(userInfo);
|
|
mSp.edit().putString(Constants.USER_INFO_KEY, GsonUtils.Companion.getInstance().toJson(userInfo)).apply();
|
|
}
|
|
|
|
public interface refreshCallBack {
|
|
void onLogin();
|
|
|
|
void onLoginFailure();
|
|
}
|
|
|
|
}
|