增加登录缓存校验

This commit is contained in:
qiji4215
2023-07-20 10:48:38 +08:00
parent 392fb91215
commit 0848ee491b
5 changed files with 65 additions and 18 deletions

View File

@@ -1,6 +1,7 @@
package com.navinfo.omqs.ui.activity.login
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import android.view.View
import android.widget.Toast
@@ -16,11 +17,11 @@ import com.navinfo.omqs.http.DefaultResponse
import com.navinfo.omqs.http.NetResult
import com.navinfo.omqs.http.NetworkService
import com.navinfo.omqs.tools.FileManager
import com.navinfo.omqs.util.NetUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import io.realm.Realm
import io.realm.RealmConfiguration
import kotlinx.coroutines.*
import retrofit2.Response
import java.io.File
import java.io.IOException
import javax.inject.Inject
@@ -73,7 +74,9 @@ class LoginViewModel @Inject constructor(
//是不是登录成功
val loginStatus: MutableLiveData<LoginStatus> = MutableLiveData()
var jobLogin: Job? = null;
var jobLogin: Job? = null
var sharedPreferences: SharedPreferences? = null
init {
loginUser.value = LoginUserBean(userCode = "haofuyue00213", passWord = "123456")
@@ -98,10 +101,26 @@ class LoginViewModel @Inject constructor(
if (password.isEmpty()) {
Toast.makeText(context, "请输入密码", Toast.LENGTH_SHORT).show()
}
sharedPreferences =
context.getSharedPreferences("USER_SHAREDPREFERENCES", Context.MODE_PRIVATE)
val userNameCache = sharedPreferences?.getString("userName", null)
val passwordCache = sharedPreferences?.getString("passWord", null)
val userCodeCache = sharedPreferences?.getString("userCode", null)
//增加缓存记录,不用每次连接网络登录
if (userNameCache != null && passwordCache != null && userCodeCache != null) {
if (userNameCache == userName && passwordCache == password) {
viewModelScope.launch(Dispatchers.IO) {
createUserFolder(context, userCodeCache)
loginStatus.postValue(LoginStatus.LOGIN_STATUS_SUCCESS)
}
return
}
}
//不指定IO会在主线程里运行
jobLogin = viewModelScope.launch(Dispatchers.IO) {
loginCheck(context, userName, password)
}
}
/**
@@ -115,25 +134,33 @@ class LoginViewModel @Inject constructor(
loginStatus.postValue(LoginStatus.LOGIN_STATUS_NET_LOADING)
var userCode = "99999";
//登录访问
when (val result = networkService.loginUser(LoginUserBean(userName,password))) {
is NetResult.Success<*> ->{
if (result.data!=null) {
when (val result = networkService.loginUser(LoginUserBean(userName, password))) {
is NetResult.Success<*> -> {
if (result.data != null) {
try {
val defaultUserResponse = result.data as DefaultResponse<SysUserBean>
if(defaultUserResponse.success){
if(defaultUserResponse.obj==null|| defaultUserResponse.obj!!.userCode==null){
if (defaultUserResponse.success) {
if (defaultUserResponse.obj == null || defaultUserResponse.obj!!.userCode == null) {
withContext(Dispatchers.Main) {
Toast.makeText(context, "服务返回用户Code信息错误", Toast.LENGTH_SHORT)
Toast.makeText(
context,
"服务返回用户Code信息错误",
Toast.LENGTH_SHORT
)
.show()
}
loginStatus.postValue(LoginStatus.LOGIN_STATUS_CANCEL)
return
}else{
} else {
userCode = defaultUserResponse.obj?.userCode.toString()
}
}else{
} else {
withContext(Dispatchers.Main) {
Toast.makeText(context, "${defaultUserResponse.msg}", Toast.LENGTH_SHORT)
Toast.makeText(
context,
"${defaultUserResponse.msg}",
Toast.LENGTH_SHORT
)
.show()
}
loginStatus.postValue(LoginStatus.LOGIN_STATUS_CANCEL)
@@ -145,7 +172,8 @@ class LoginViewModel @Inject constructor(
}
}
}
is NetResult.Error<*> ->{
is NetResult.Error<*> -> {
withContext(Dispatchers.Main) {
Toast.makeText(context, "${result.exception.message}", Toast.LENGTH_SHORT)
.show()
@@ -153,7 +181,8 @@ class LoginViewModel @Inject constructor(
loginStatus.postValue(LoginStatus.LOGIN_STATUS_CANCEL)
return
}
is NetResult.Failure<*> ->{
is NetResult.Failure<*> -> {
withContext(Dispatchers.Main) {
Toast.makeText(context, "${result.code}:${result.msg}", Toast.LENGTH_SHORT)
.show()
@@ -161,12 +190,16 @@ class LoginViewModel @Inject constructor(
loginStatus.postValue(LoginStatus.LOGIN_STATUS_CANCEL)
return
}
else -> {}
}
//文件夹初始化
try {
loginStatus.postValue(LoginStatus.LOGIN_STATUS_FOLDER_INIT)
sharedPreferences?.edit()?.putString("userName", userName)?.commit()
sharedPreferences?.edit()?.putString("passWord", password)?.commit()
sharedPreferences?.edit()?.putString("userCode", userCode)?.commit()
createUserFolder(context, userCode)
} catch (e: IOException) {
loginStatus.postValue(LoginStatus.LOGIN_STATUS_FOLDER_FAILURE)
@@ -185,18 +218,21 @@ class LoginViewModel @Inject constructor(
roomAppDatabase.getOfflineMapDao().insertOrUpdate(result.data)
}
}
is NetResult.Error<*> -> {
withContext(Dispatchers.Main) {
Toast.makeText(context, "${result.exception.message}", Toast.LENGTH_SHORT)
.show()
}
}
is NetResult.Failure<*> -> {
withContext(Dispatchers.Main) {
Toast.makeText(context, "${result.code}:${result.msg}", Toast.LENGTH_SHORT)
.show()
}
}
is NetResult.Loading -> {}
else -> {}
}
@@ -234,7 +270,7 @@ class LoginViewModel @Inject constructor(
// 拷贝配置文件到用户目录下
val omdbConfigFile = File(userFolder.absolutePath, Constant.OMDB_CONFIG);
// if (!omdbConfigFile.exists()) {
ResourceUtils.copyFileFromAssets(Constant.OMDB_CONFIG, omdbConfigFile.absolutePath)
ResourceUtils.copyFileFromAssets(Constant.OMDB_CONFIG, omdbConfigFile.absolutePath)
// }
}

View File

@@ -868,7 +868,7 @@ class MainActivity : BaseActivity() {
private fun setIndoorGroupEnable(enable: Boolean) {
binding.mainActivitySnapshotFinish.isEnabled = enable
binding.mainActivityTraceSnapshotPoints.isEnabled = enable
binding.mainActivitySnapshotMediaFlag.isEnabled = enable
//binding.mainActivitySnapshotMediaFlag.isEnabled = enable
binding.mainActivitySnapshotRewind.isEnabled = enable
binding.mainActivitySnapshotPause.isEnabled = enable
binding.mainActivitySnapshotNext.isEnabled = enable

View File

@@ -370,4 +370,5 @@ public class ShareUtil {
return null;
}
}

View File

@@ -362,6 +362,7 @@
<ImageButton
android:id="@+id/main_activity_snapshot_media_flag"
style="@style/top_right_drawer_btns_style"
android:visibility="gone"
android:onClick="@{()->mainActivity.mediaFlagOnclick()}"
android:src="@drawable/map_trace_mediaflag" />