From 93775e1f3d79a1e11b839abbf24201dafcd9e646 Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期五, 25 四月 2025 13:45:56 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'
---
app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt | 49 ++++++++++++++++++++++++++++++++++++++++++++-----
1 files changed, 44 insertions(+), 5 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..1dbc3c0 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 { "发送验证码失败" }
@@ -38,12 +52,21 @@
}
}
- fun login(phone: String, code: String) {
+ fun login(phone: String, code: String, invite: String) {
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,
+ intevailCode = invite,
+ 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 +80,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