Commit ad6d1955 by 段启岩

适配最新登录接口

parent 8d6b8801
......@@ -3,6 +3,7 @@ package cn.yunliyunwai.beyondclouds;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
......@@ -13,6 +14,7 @@ import javax.inject.Inject;
import cn.yunliyunwai.beyondclouds.base.BaseActivity;
import cn.yunliyunwai.beyondclouds.databinding.ActivitySplashBinding;
import cn.yunliyunwai.beyondclouds.util.ContextUtils;
import cn.yunliyunwai.beyondclouds.util.VersionUtils;
import cn.yunliyunwai.beyondclouds.viewmodel.SplashActivityViewModel;
/**
......@@ -43,6 +45,9 @@ public class SplashActivity extends BaseActivity<SplashActivityViewModel, Activi
super.onCreate(savedInstanceState);
String versionName = VersionUtils.getVersionName(this);
Toast.makeText(this, "App当前版本为" + versionName, Toast.LENGTH_SHORT);
viewModel.getInitialStates().observe(this, new Observer<SplashActivityViewModel.InitialStates>() {
@Override
public void onChanged(SplashActivityViewModel.InitialStates initialStates) {
......@@ -52,7 +57,7 @@ public class SplashActivity extends BaseActivity<SplashActivityViewModel, Activi
}
}
});
binding.getViewModel().initialize();
viewModel.initialize();
}
@Override
......
......@@ -7,4 +7,6 @@ public class AuthenticationResult {
private String userId;
private String accessToken;
private String refreshToken;
}
package cn.yunliyunwai.beyondclouds.data.model;
public enum ClientType {
WEB(1),
ANDROID(2)
;
private Integer type;
ClientType(Integer type) {
this.type = type;
}
public Integer getType() {
return type;
}
public static ClientType valueOf(Integer type) {
if (null == type) {
return WEB;
}
for (ClientType clientType : values()) {
if (clientType.getType().equals(type)) {
return clientType;
}
}
return WEB;
}
}
......@@ -6,6 +6,7 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import cn.yunliyunwai.beyondclouds.data.model.AuthenticationResult;
import cn.yunliyunwai.beyondclouds.data.model.ClientType;
import cn.yunliyunwai.beyondclouds.data.model.Result;
import cn.yunliyunwai.beyondclouds.data.model.UserInfo;
import cn.yunliyunwai.beyondclouds.data.model.form.AccountLoginForm;
......@@ -24,12 +25,12 @@ public class UserRepositoryImpl implements IUserRepository {
@Override
public LiveData<Result<AuthenticationResult>> accountAuth(String account, String password) {
return userApiStore.accountAuth(AccountLoginForm.create(account, password));
return userApiStore.accountAuth(AccountLoginForm.create(account, password), ClientType.ANDROID.getType());
}
@Override
public LiveData<Result<AuthenticationResult>> smsAuth(String mobile, String verifyCode) {
return userApiStore.smsAuth(SmsLoginForm.create(mobile, verifyCode));
return userApiStore.smsAuth(SmsLoginForm.create(mobile, verifyCode), ClientType.ANDROID.getType());
}
@Override
......
......@@ -10,6 +10,7 @@ import cn.yunliyunwai.beyondclouds.data.model.form.SmsLoginForm;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
public interface UserApiStore {
......@@ -19,7 +20,7 @@ public interface UserApiStore {
* @return
*/
@POST("auth/password")
LiveData<Result<AuthenticationResult>> accountAuth(@Body AccountLoginForm accountLoginForm);
LiveData<Result<AuthenticationResult>> accountAuth(@Body AccountLoginForm accountLoginForm, @Query("clientType") Integer clientType);
/**
* 短信验证码登录
......@@ -27,7 +28,7 @@ public interface UserApiStore {
* @return
*/
@POST("auth/sms")
LiveData<Result<AuthenticationResult>> smsAuth(@Body SmsLoginForm smsLoginForm);
LiveData<Result<AuthenticationResult>> smsAuth(@Body SmsLoginForm smsLoginForm, @Query("clientType") Integer clientType);
/**
* 获取我的用户信息
......
package cn.yunliyunwai.beyondclouds.util;
import android.content.Context;
import android.content.pm.PackageInfo;
public class VersionUtils {
public static String getVersionName(Context context) {
try {
PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
return packageInfo.versionName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
......@@ -26,6 +26,7 @@ public class AuthenticationViewModel extends ViewModel {
private static final String SHARED_PREFERENCES_NAME_AUTHENTICATION = "SHARED_PREFERENCES_AUTHENTICATION";
private static final String SHARED_PREFERENCES_AUTHENTICATION_KEY_TOKEN = "SHARED_PREFERENCES_AUTHENTICATION_TOKEN";
private static final String SHARED_PREFERENCES_AUTHENTICATION_KEY_REFRESH_TOKEN = "SHARED_PREFERENCES_AUTHENTICATION_REFRESH_TOKEN";
private static final String SHARED_PREFERENCES_AUTHENTICATION_KEY_USER_INFO = "SHARED_PREFERENCES_AUTHENTICATION_USER_INFO";
......@@ -71,6 +72,11 @@ public class AuthenticationViewModel extends ViewModel {
private String mToken;
/**
* refreshToken
*/
private String mRefreshToken;
/**
* 认证失败的信息
*/
private String mAuthErrorMsg;
......@@ -198,6 +204,7 @@ public class AuthenticationViewModel extends ViewModel {
public void onChanged(Result<AuthenticationResult> authenticationResultResult) {
if (authenticationResultResult.getCode() == 0) {
mToken = authenticationResultResult.getData().getAccessToken();
mRefreshToken = authenticationResultResult.getData().getRefreshToken();
authenticationState.setValue(AuthenticationState.WAIT_FOR_FETCH_USER_INFO);
} else {
mAuthErrorMsg = authenticationResultResult.getMsg();
......@@ -219,6 +226,7 @@ public class AuthenticationViewModel extends ViewModel {
public void onChanged(Result<AuthenticationResult> authenticationResultResult) {
if (authenticationResultResult.getCode() == 0) {
mToken = authenticationResultResult.getData().getAccessToken();
mRefreshToken = authenticationResultResult.getData().getRefreshToken();
authenticationState.setValue(AuthenticationState.WAIT_FOR_FETCH_USER_INFO);
} else {
mAuthErrorMsg = authenticationResultResult.getMsg();
......@@ -256,6 +264,7 @@ public class AuthenticationViewModel extends ViewModel {
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_REFRESH_TOKEN, mRefreshToken)
.putString(SHARED_PREFERENCES_AUTHENTICATION_KEY_USER_INFO, JsonUtils.toJson(mUserInfo))
.commit();
}
......@@ -266,9 +275,11 @@ public class AuthenticationViewModel extends ViewModel {
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 refreshToken = sharedPreferences.getString(SHARED_PREFERENCES_AUTHENTICATION_KEY_REFRESH_TOKEN, null);
String userInfo = sharedPreferences.getString(SHARED_PREFERENCES_AUTHENTICATION_KEY_USER_INFO, null);
if (null != token && null != userInfo) {
if (null != token && null != refreshToken && null != userInfo) {
mToken = token;
mRefreshToken = refreshToken;
mUserInfo = JsonUtils.json2Bean(userInfo, UserInfo.class);
this.authenticationState.setValue(AuthenticationState.AUTHENTICATED);
} else {
......
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