From a7820e2f1ee06a7b43b4d351cced3343d7e1a5e2 Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期一, 31 三月 2025 08:55:52 +0800 Subject: [PATCH] fix 登录限制 --- app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt | 54 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 44 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt b/app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt index f61b646..ee242e2 100644 --- a/app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt +++ b/app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt @@ -5,8 +5,11 @@ import androidx.lifecycle.viewModelScope import kotlinx.coroutines.launch import androidx.lifecycle.ViewModel +import com.example.firstapp.database.request.SmsLoginRequest +import com.example.firstapp.database.request.SmsSendRequest import com.example.firstapp.database.service.RetrofitClient import com.example.firstapp.utils.Log +import com.example.firstapp.ui.home.HomeViewModel class LoginViewModel : ViewModel() { @@ -19,16 +22,26 @@ private val _isLoading = MutableLiveData<Boolean>() val isLoading: LiveData<Boolean> = _isLoading + private lateinit var homeViewModel: HomeViewModel + fun sendVerificationCode(phone: String) { viewModelScope.launch { _isLoading.value = true try { -// val response = RetrofitClient.apiService.sendVerificationCode(phone) -// if (response.code == 200) { + // 创建 SmsSendRequest 对象 + val request = SmsSendRequest( + tel = phone, + userType = "customer" + ) + //Retrofit 进行网络请求时,类名不需要完全一致,只要保证类的属性名称和类型与后端 DTO 对象的属性一致即可。 + //Retrofit + Gson 在序列化时会将对象转换为 JSON,后端 Spring 框架会将 JSON 反序列化为 SmsSendDTO 对象 + //HTTP 请求实际传输的是 JSON 格式的数据,而不是 Java/Kotlin 对象。 + val response = RetrofitClient.apiService.sendVerificationCode(request) + if (response.code == 0) { _loginMessage.value = "验证码已发送" -// } else { -// _loginMessage.value = response.msg.ifEmpty { "发送验证码失败" } -// } + } else { + _loginMessage.value = response.msg.ifEmpty { "发送验证码失败" } + } } catch (e: Exception) { Log.e("LoginError", "Login failed: ${e.message}", e) _loginMessage.value = "网络错误,请稍后重试" @@ -42,12 +55,20 @@ viewModelScope.launch { _isLoading.value = true try { -// val response = RetrofitClient.apiService.verifyCode(phone, code) -// if (response.code == 200 && response.data) { + val request = SmsLoginRequest( + username = phone, + smsCode = code, + userType = "customer" + ) + //HttpServletRequest request这是后端 Spring 框架中的一个特殊参数, + //用于获取 HTTP 请求的相关信息(如请求头、Cookie 等),它会由 Spring 框架自动注入,不需要客户端显式传递。 + val response = RetrofitClient.apiService.verifyCode(request) + if (response.code == "0" && response.data != null) { + saveToken(response.data.value) // 这里获取的是 access_token _loginState.value = true -// } else { -// _loginMessage.value = response.msg.ifEmpty { "登录失败" } -// } + } else { + _loginMessage.value = response.msg.ifEmpty { "登录失败" } + } } catch (e: Exception) { Log.e("LoginError", "Login failed: ${e.message}", e) _loginMessage.value = "网络错误,请稍后重试" @@ -57,4 +78,17 @@ } } + fun logout() { + viewModelScope.launch { + // 不再清除用户数据,只执行登出操作 + homeViewModel.logout() + // 其他登出操作... + } + } + + private fun saveToken(token: String) { + // TODO: 实现token存储逻辑 + // 可能还需要存储 refresh_token + } + } \ No newline at end of file -- Gitblit v1.9.3