From 30b57df644a2f9c27f21b98ca2086eae1da9cfa5 Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期一, 07 四月 2025 15:04:21 +0800
Subject: [PATCH] add: 消息提醒前版本
---
app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt | 58 ++++++++++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 48 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..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,16 +23,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 +56,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,phone) // 这里获取的是 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 +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