Commit 688455d7 by 段启岩

用户登录完成

parent 9cfb20d0
package cn.yunliyunwai.beyondclouds.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Type;
public class JsonUtils {
private static Gson gson = new GsonBuilder().create();
public static <T> T json2Bean(String jsonStr, Class<T> objClass){
return gson.fromJson(jsonStr, objClass);
}
public static <T> T json2Bean(String jsonStr, Type objClass){
return gson.fromJson(jsonStr, objClass);
}
public static String toJson(Object obj) {
String reString=gson.toJson(obj);
return reString;
}
}
package cn.yunliyunwai.beyondclouds.viewmodel; package cn.yunliyunwai.beyondclouds.viewmodel;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
...@@ -16,6 +17,7 @@ import cn.yunliyunwai.beyondclouds.data.model.AuthenticationResult; ...@@ -16,6 +17,7 @@ import cn.yunliyunwai.beyondclouds.data.model.AuthenticationResult;
import cn.yunliyunwai.beyondclouds.data.model.Result; import cn.yunliyunwai.beyondclouds.data.model.Result;
import cn.yunliyunwai.beyondclouds.data.model.UserInfo; import cn.yunliyunwai.beyondclouds.data.model.UserInfo;
import cn.yunliyunwai.beyondclouds.data.source.IUserRepository; import cn.yunliyunwai.beyondclouds.data.source.IUserRepository;
import cn.yunliyunwai.beyondclouds.util.JsonUtils;
import dagger.Lazy; import dagger.Lazy;
@Singleton @Singleton
...@@ -25,6 +27,8 @@ public class AuthenticationViewModel extends ViewModel { ...@@ -25,6 +27,8 @@ public class AuthenticationViewModel extends ViewModel {
private static final String SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN = "SHARED_PREFERENCES_AUTHENTICATION_TOKEN"; private static final String SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN = "SHARED_PREFERENCES_AUTHENTICATION_TOKEN";
private static final String SHARED_PREFERENCES_AUTHENTICATION_KEY_USER_INFO = "SHARED_PREFERENCES_AUTHENTICATION_USER_INFO";
public enum AuthenticationState { public enum AuthenticationState {
/** /**
* 未认证 * 未认证
...@@ -111,18 +115,15 @@ public class AuthenticationViewModel extends ViewModel { ...@@ -111,18 +115,15 @@ public class AuthenticationViewModel extends ViewModel {
*/ */
private UserInfo mUserInfo; private UserInfo mUserInfo;
private Context mContext;
@Inject @Inject
public AuthenticationViewModel(Context context, Lazy<IUserRepository> userRepositoryLazy) { public AuthenticationViewModel(Context context, Lazy<IUserRepository> userRepositoryLazy) {
this.mContext = context;
this.userRepositoryLazy = userRepositoryLazy; this.userRepositoryLazy = userRepositoryLazy;
String token = context.getSharedPreferences(SHARED_PREFERENCES_NAME_AUTHENTICATION, Context.MODE_PRIVATE).getString(SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN, null); loadTokenAndUserInfoFromLocal();
if (null != token) {
this.mToken = token;
this.authenticationState.setValue(AuthenticationState.AUTHENTICATED);
} else {
this.authenticationState.setValue(AuthenticationState.UN_AUTHENTICATE);
}
this.loginWay.setValue(LoginWay.ACCOUNT); this.loginWay.setValue(LoginWay.ACCOUNT);
this.passwordVisibility.setValue(false); this.passwordVisibility.setValue(false);
...@@ -238,6 +239,7 @@ public class AuthenticationViewModel extends ViewModel { ...@@ -238,6 +239,7 @@ public class AuthenticationViewModel extends ViewModel {
if (userInfoResult.getCode() == 0) { if (userInfoResult.getCode() == 0) {
// 保存用户信息 // 保存用户信息
mUserInfo = userInfoResult.getData(); mUserInfo = userInfoResult.getData();
saveUserInfoAndTokenToLocal();
authenticationState.setValue(AuthenticationState.AUTHENTICATED); authenticationState.setValue(AuthenticationState.AUTHENTICATED);
} else { } else {
mAuthErrorMsg = userInfoResult.getMsg(); mAuthErrorMsg = userInfoResult.getMsg();
...@@ -246,4 +248,31 @@ public class AuthenticationViewModel extends ViewModel { ...@@ -246,4 +248,31 @@ public class AuthenticationViewModel extends ViewModel {
} }
}); });
} }
/**
* 将用户信息和token保存到本地
*/
private void saveUserInfoAndTokenToLocal() {
SharedPreferences sharedPreferences = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME_AUTHENTICATION, Context.MODE_PRIVATE);
sharedPreferences.edit()
.putString(SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN, mToken)
.putString(SHARED_PREFERENCES_AUTHENTICATION_KEY_USER_INFO, JsonUtils.toJson(mUserInfo))
.commit();
}
/**
* 从本地加载token和用户信息
*/
private void loadTokenAndUserInfoFromLocal() {
SharedPreferences sharedPreferences = mContext.getSharedPreferences(SHARED_PREFERENCES_NAME_AUTHENTICATION, Context.MODE_PRIVATE);
String token = sharedPreferences.getString(SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN, null);
String userInfo = sharedPreferences.getString(SHARED_PREFERENCES_AUTHENTICATION_KEY_USER_INFO, null);
if (null != token && null != userInfo) {
mToken = token;
mUserInfo = JsonUtils.json2Bean(userInfo, UserInfo.class);
this.authenticationState.setValue(AuthenticationState.AUTHENTICATED);
} else {
this.authenticationState.setValue(AuthenticationState.UN_AUTHENTICATE);
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment