Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
B
beyond-clouds-android
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
云深项目组
beyond-clouds-android
Commits
688455d7
Commit
688455d7
authored
Mar 19, 2020
by
段启岩
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
用户登录完成
parent
9cfb20d0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
7 deletions
+60
-7
app/src/main/java/cn/yunliyunwai/beyondclouds/util/JsonUtils.java
+24
-0
app/src/main/java/cn/yunliyunwai/beyondclouds/viewmodel/AuthenticationViewModel.java
+36
-7
No files found.
app/src/main/java/cn/yunliyunwai/beyondclouds/util/JsonUtils.java
0 → 100644
View file @
688455d7
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
;
}
}
app/src/main/java/cn/yunliyunwai/beyondclouds/viewmodel/AuthenticationViewModel.java
View file @
688455d7
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
);
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment