From a8da38115220a0677442899ecf7bf75fd1ef325c Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期二, 25 三月 2025 14:22:39 +0800
Subject: [PATCH] 1.支付
---
app/src/main/java/com/example/firstapp/ui/login/LoginViewModel.kt | 61 +++++++++++++++++++-----------
1 files changed, 38 insertions(+), 23 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 6d102f4..f61b646 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
@@ -1,45 +1,60 @@
package com.example.firstapp.ui.login
-import android.app.Application
-import android.content.Context
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
-import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
-import androidx.lifecycle.AndroidViewModel
+import androidx.lifecycle.ViewModel
+import com.example.firstapp.database.service.RetrofitClient
+import com.example.firstapp.utils.Log
-class LoginViewModel(application: Application) : AndroidViewModel(application) {
+class LoginViewModel : ViewModel() {
private val _loginState = MutableLiveData<Boolean>()
val loginState: LiveData<Boolean> = _loginState
+ private val _loginMessage = MutableLiveData<String>()
+ val loginMessage: LiveData<String> = _loginMessage
+
+ private val _isLoading = MutableLiveData<Boolean>()
+ val isLoading: LiveData<Boolean> = _isLoading
+
fun sendVerificationCode(phone: String) {
viewModelScope.launch {
- // 这里实现发送验证码的逻辑
- // 模拟网络请求
- delay(1000)
- // 实际应用中需要调用后端API
+ _isLoading.value = true
+ try {
+// val response = RetrofitClient.apiService.sendVerificationCode(phone)
+// if (response.code == 200) {
+ _loginMessage.value = "验证码已发送"
+// } else {
+// _loginMessage.value = response.msg.ifEmpty { "发送验证码失败" }
+// }
+ } catch (e: Exception) {
+ Log.e("LoginError", "Login failed: ${e.message}", e)
+ _loginMessage.value = "网络错误,请稍后重试"
+ } finally {
+ _isLoading.value = false
+ }
}
}
fun login(phone: String, code: String) {
viewModelScope.launch {
- // 模拟登录请求
- delay(1000)
- // 保存登录状态和手机号
- saveLoginInfo(phone)
- _loginState.value = true
+ _isLoading.value = true
+ try {
+// val response = RetrofitClient.apiService.verifyCode(phone, code)
+// if (response.code == 200 && response.data) {
+ _loginState.value = true
+// } else {
+// _loginMessage.value = response.msg.ifEmpty { "登录失败" }
+// }
+ } catch (e: Exception) {
+ Log.e("LoginError", "Login failed: ${e.message}", e)
+ _loginMessage.value = "网络错误,请稍后重试"
+ } finally {
+ _isLoading.value = false
+ }
}
}
- private fun saveLoginInfo(phone: String) {
- getApplication<Application>().getSharedPreferences(
- "user_info", Context.MODE_PRIVATE
- ).edit().apply {
- putBoolean("is_logged_in", true)
- putString("phone", phone)
- apply()
- }
- }
}
\ No newline at end of file
--
Gitblit v1.9.3