From bfdd997ae890a49eae8e2083ae9ccdeab795e7cd Mon Sep 17 00:00:00 2001 From: pan <1029559041@qq.com> Date: Mon, 24 May 2021 23:23:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- foreground/src/main/java/com/gyf/csams/Api.kt | 3 +- .../main/java/com/gyf/csams/InitViewModel.kt | 11 ++-- .../csams/account/model/AccountViewModel.kt | 17 ++++--- .../gyf/csams/account/ui/AccountActivity.kt | 3 +- .../com/gyf/csams/main/model/MainViewModel.kt | 50 +++++++++++++++++++ .../com/gyf/csams/main/ui/MainActivity.kt | 29 +++++++---- .../main/java/com/gyf/csams/util/TokenUtil.kt | 17 ++----- .../main/java/com/gyf/lib/util/HttpUtil.kt | 17 ++++++- 8 files changed, 110 insertions(+), 37 deletions(-) diff --git a/foreground/src/main/java/com/gyf/csams/Api.kt b/foreground/src/main/java/com/gyf/csams/Api.kt index 97447d8..6eba528 100644 --- a/foreground/src/main/java/com/gyf/csams/Api.kt +++ b/foreground/src/main/java/com/gyf/csams/Api.kt @@ -20,7 +20,8 @@ enum class AccountApi(val path: String) : UrlPath { Register("/register"), CheckId("/register/checkId"), Login("/login"), - LoginToken("/login/token"); + LoginToken("/login/token"), + Logout("/logout"); override fun build(): String { diff --git a/foreground/src/main/java/com/gyf/csams/InitViewModel.kt b/foreground/src/main/java/com/gyf/csams/InitViewModel.kt index e5f1d62..3ce1737 100644 --- a/foreground/src/main/java/com/gyf/csams/InitViewModel.kt +++ b/foreground/src/main/java/com/gyf/csams/InitViewModel.kt @@ -10,10 +10,10 @@ import com.google.gson.reflect.TypeToken import com.gyf.csams.util.AppDatabase import com.gyf.csams.util.SimpleCallback import com.gyf.csams.util.Token +import com.gyf.csams.util.TokenManager import com.gyf.lib.util.ApiResponse import com.gyf.lib.util.HttpClient import com.orhanobut.logger.Logger -import kotlinx.coroutines.delay import kotlinx.coroutines.launch data class TokenVo(val token: String, val studentId: String) @@ -38,7 +38,6 @@ class InitViewModel : ViewModel() { private fun checkServer() { Logger.i("测试连接到服务端") viewModelScope.launch { - delay(5000) HttpClient.get( Api.buildUrl(TestApi.Test), SimpleCallback(action = "测试", onSuccess = { @@ -72,6 +71,9 @@ class InitViewModel : ViewModel() { it.body?.let { _token.postValue(it) Logger.i("token校验结果:${it}") + if (it) { + TokenManager.token = currentToken + } } }, @@ -86,8 +88,11 @@ class InitViewModel : ViewModel() { ) ) } else if (tokenList != null && tokenList.size > 1) { + //TODO 实现切换历史登录帐号 - Logger.i("token数量大于一,需要手动登录") + Logger.i("本地存储了${tokenList.size}个令牌,需要手动登录") + db.tokenDao().deleteAll() + Logger.i("清空所有token") _token.postValue(false) } else { Logger.i("本地没有任何token,跳转到登录界面") diff --git a/foreground/src/main/java/com/gyf/csams/account/model/AccountViewModel.kt b/foreground/src/main/java/com/gyf/csams/account/model/AccountViewModel.kt index a2151e7..70ea6e5 100644 --- a/foreground/src/main/java/com/gyf/csams/account/model/AccountViewModel.kt +++ b/foreground/src/main/java/com/gyf/csams/account/model/AccountViewModel.kt @@ -11,11 +11,12 @@ import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.gyf.csams.AccountApi import com.gyf.csams.Api -import com.gyf.csams.InitActivity import com.gyf.csams.R import com.gyf.csams.account.ui.AccountRoute +import com.gyf.csams.main.ui.MainActivity import com.gyf.csams.util.AppDatabase import com.gyf.csams.util.SimpleCallback +import com.gyf.csams.util.TokenManager import com.gyf.csams.util.TokenResDto import com.gyf.lib.uikit.FormStatus import com.gyf.lib.uikit.StringForm @@ -265,15 +266,17 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) SimpleCallback( action = loginDesc, onSuccess = { + Logger.i(it.message) callback(it.message) val context = getApplication().applicationContext val token = it.body?.token if (token != null) { val db = AppDatabase.getInstance(context) viewModelScope.launch { + TokenManager.token = token db?.tokenDao()?.save(token = token) }.invokeOnCompletion { - context.startActivity(Intent(context, InitActivity::class.java)) + context.startActivity(Intent(context, MainActivity::class.java)) _finishLogin.postValue(true) } } @@ -281,12 +284,10 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) onFail = { callback(it) }, type = object : TypeToken>() {}.type ), - jsonBody = Gson().toJson( - UserLoginVo( - studentId = studentId, - password = password, - device = "${Build.MANUFACTURER} ${Build.MODEL}" - ) + param = UserLoginVo( + studentId = studentId, + password = password, + device = "${Build.MANUFACTURER} ${Build.MODEL}" ) ) } else { diff --git a/foreground/src/main/java/com/gyf/csams/account/ui/AccountActivity.kt b/foreground/src/main/java/com/gyf/csams/account/ui/AccountActivity.kt index fe8b731..4fd380f 100644 --- a/foreground/src/main/java/com/gyf/csams/account/ui/AccountActivity.kt +++ b/foreground/src/main/java/com/gyf/csams/account/ui/AccountActivity.kt @@ -138,12 +138,13 @@ class AccountActivity : ComponentActivity() { modifier = Modifier.fillMaxSize() ) { Column(modifier = Modifier.width(IntrinsicSize.Min)) { + val name by accountViewModel.name.formValue.observeAsState("") Text(buildAnnotatedString { withStyle( style = MaterialTheme.typography.subtitle1.toSpanStyle() .copy(color = MaterialTheme.colors.primary) ) { - append(accountViewModel.name.formValue.value ?: "") + append(name) } withStyle(style = MaterialTheme.typography.subtitle1.toSpanStyle()) { append(accountViewModel.welcomeStart) diff --git a/foreground/src/main/java/com/gyf/csams/main/model/MainViewModel.kt b/foreground/src/main/java/com/gyf/csams/main/model/MainViewModel.kt index d0ecfd9..735f2bd 100644 --- a/foreground/src/main/java/com/gyf/csams/main/model/MainViewModel.kt +++ b/foreground/src/main/java/com/gyf/csams/main/model/MainViewModel.kt @@ -1,14 +1,25 @@ package com.gyf.csams.main.model +import android.app.Activity +import android.content.Intent import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.google.gson.reflect.TypeToken +import com.gyf.csams.AccountApi +import com.gyf.csams.Api import com.gyf.csams.NOT_IMPL_TIP +import com.gyf.csams.account.ui.AccountActivity import com.gyf.csams.uikit.SendInterface +import com.gyf.csams.util.AppDatabase +import com.gyf.csams.util.SimpleCallback +import com.gyf.csams.util.TokenManager import com.gyf.lib.ScrollList import com.gyf.lib.uikit.PersonInfoVo import com.gyf.lib.uikit.StringForm +import com.gyf.lib.util.ApiResponse +import com.gyf.lib.util.HttpClient import com.gyf.lib.util.randomChinese import com.gyf.lib.util.randomNum import com.orhanobut.logger.Logger @@ -173,6 +184,8 @@ data class InfoVo( override val desc: String ) : PersonInfoVo() +data class UserLogoutVo(val studentId: String) + /** * 个人中心 * @@ -202,4 +215,41 @@ class CenterViewModel : ViewModel() { } } + + fun logout(context: Activity, callback: (message: String) -> Unit) { + val studentId = TokenManager.token?.studentId + if (studentId != null) { + Logger.i("帐号$studentId 将要退出登录") + viewModelScope.launch { + HttpClient.post(Api.buildUrl(AccountApi.Logout), + SimpleCallback(action = "登出", onSuccess = { it -> + it.body?.let { + if (it) { + viewModelScope.launch { + val db = AppDatabase.getInstance(context = context) + db?.tokenDao()?.deleteAll() + Logger.i("退出登陆成功") + context.finish() + context.startActivity( + Intent( + context, + AccountActivity::class.java + ) + ) + + } + TokenManager.token = null + } + } + callback(it.message) + }, onFail = { callback("退出登陆失败") }, + type = object : TypeToken>() {}.type + ), + param = UserLogoutVo(studentId = studentId) + ) + } + } else { + callback("操作异常,请联系管理员") + } + } } diff --git a/foreground/src/main/java/com/gyf/csams/main/ui/MainActivity.kt b/foreground/src/main/java/com/gyf/csams/main/ui/MainActivity.kt index aec2a6a..2a9554d 100644 --- a/foreground/src/main/java/com/gyf/csams/main/ui/MainActivity.kt +++ b/foreground/src/main/java/com/gyf/csams/main/ui/MainActivity.kt @@ -52,16 +52,17 @@ class MainActivity : ComponentActivity() { setContent { - Body { nav -> - NavHost(navController = nav, startDestination = MainMenu.Main.name) { - composable(MainMenu.Main.name) { - Main(navController = nav) - } - composable(MainMenu.List.name) { - AssociationList(navController = nav) - } - composable(MainMenu.Center.name) { - Center(navController = nav) + TestBody { nav, s -> + NavHost(navController = nav, startDestination = MainMenu.Main.name) { + composable(MainMenu.Main.name) { + Main(navController = nav) + } + composable(MainMenu.List.name) { + AssociationList(navController = nav) + } + composable(MainMenu.Center.name) { + Center(navController = nav) + ShowSnackbar(scaffoldState = s) } } @@ -90,6 +91,7 @@ class MainActivity : ComponentActivity() { @Composable private fun Center( model: CenterViewModel = viewModel(), + scaffoldModel: ScaffoldModel = viewModel(), navController: NavHostController ) { MainColumnFrame( @@ -97,7 +99,7 @@ class MainActivity : ComponentActivity() { mainMenu = MainMenu.Center, nav = navController ) { - val context = LocalContext.current + val context = LocalContext.current as MainActivity val info by model.info.observeAsState() @@ -121,6 +123,11 @@ class MainActivity : ComponentActivity() { CenterMenuItem(text = model.myJoinActivity) CenterMenuItem(text = model.myLikeActivity) CenterMenuItem(text = model.myCollectActivity) + CenterMenuItem(text = "退出登录") { + model.logout(context = context) { + scaffoldModel.update(message = it) + } + } } } diff --git a/foreground/src/main/java/com/gyf/csams/util/TokenUtil.kt b/foreground/src/main/java/com/gyf/csams/util/TokenUtil.kt index 47e3b22..42432a9 100644 --- a/foreground/src/main/java/com/gyf/csams/util/TokenUtil.kt +++ b/foreground/src/main/java/com/gyf/csams/util/TokenUtil.kt @@ -34,20 +34,13 @@ interface TokenDao { @Delete suspend fun delete(user: Token) -} - -class TokenManager private constructor(private var token: Token?) { - companion object { - @Volatile - private var instance: TokenManager? = null - fun getInstance(token: Token? = null) = - instance ?: synchronized(this) { - instance ?: TokenManager(token).also { instance = it } - } - - } + @Query("delete from token") + suspend fun deleteAll() +} +object TokenManager { + var token: Token? = null } diff --git a/lib/src/main/java/com/gyf/lib/util/HttpUtil.kt b/lib/src/main/java/com/gyf/lib/util/HttpUtil.kt index 43da770..e9980d9 100644 --- a/lib/src/main/java/com/gyf/lib/util/HttpUtil.kt +++ b/lib/src/main/java/com/gyf/lib/util/HttpUtil.kt @@ -14,6 +14,8 @@ object HttpClient { private val JSON_CONTENT_TYPE = "application/json; charset=UTF-8".toMediaType() + private val json = Gson() + /** * 构建url查询参数 * @@ -89,6 +91,10 @@ object HttpClient { * @param callback * @param jsonBody */ + @Deprecated( + message = "", + replaceWith = ReplaceWith("com.gyf.lib.util.HttpClient.post(java.lang.String, okhttp3.Callback, java.lang.Object)") + ) fun post(url: String, callback: Callback, jsonBody: String) { Logger.json(jsonBody) val request = Request.Builder() @@ -99,7 +105,16 @@ object HttpClient { call.enqueue(callback) } - + fun post(url: String, callback: Callback, param: Any) { + val jsonBody = json.toJson(param) + Logger.json(jsonBody) + val request = Request.Builder() + .url(url) + .post(body = jsonBody.toRequestBody(contentType = JSON_CONTENT_TYPE)) + .build() + val call = httpClient.newCall(request) + call.enqueue(callback) + } } /**