From 731667db1ac658a6f6064ef328d04eb1d47c20ff Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期一, 31 三月 2025 13:53:02 +0800
Subject: [PATCH] fix 登录
---
app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt | 46 ++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 42 insertions(+), 4 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 3dff2a9..92f03e9 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,12 @@
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
+import com.example.firstapp.utils.PreferencesManager
class LoginViewModel : ViewModel() {
@@ -19,12 +23,22 @@
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 { "发送验证码失败" }
@@ -42,8 +56,16 @@
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,phone) // 这里获取的是 access_token
_loginState.value = true
} else {
_loginMessage.value = response.msg.ifEmpty { "登录失败" }
@@ -57,4 +79,20 @@
}
}
+ fun logout() {
+ viewModelScope.launch {
+ // 不再清除用户数据,只执行登出操作
+ homeViewModel.logout()
+ // 其他登出操作...
+ }
+ }
+
+ private fun saveToken(token: String,phone:String) {
+ // TODO: 实现token存储逻辑
+ // 可能还需要存储 refresh_token
+ PreferencesManager.saveToken(token)
+ // 保存登录的手机号
+ PreferencesManager.savePhone(phone)
+ }
+
}
\ No newline at end of file
--
Gitblit v1.9.3