package com.example.smart.ui.login
|
|
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.viewModelScope
|
import com.example.basecore.base.BaseViewModel
|
import com.example.smart.reqeust.RetrofitClient
|
import kotlinx.coroutines.launch
|
import com.example.basecore.net.http.Result
|
import com.example.smart.bean.LoginBean
|
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.delay
|
|
|
class LoginViewModel : BaseViewModel() {
|
|
val loginBean = MutableLiveData<LoginBean>()
|
|
|
fun loginRequest(phone: String, password: String, fail: (String) -> Unit) {
|
|
val bean = LoginRequestBean(phone, password)
|
viewModelScope.launch(Dispatchers.Main) {
|
safeCall {
|
execute(RetrofitClient.api.login(bean)) }.apply {
|
if (this is Result.Success) {
|
loginBean.value = data
|
} else if (this is Result.Error) {
|
fail(exception.message ?: "未知错误")
|
}
|
}
|
}
|
}
|
//延时2000毫秒,返回一个String结果
|
suspend fun requestUserInfo(): String {
|
delay(2000)
|
return "result form userInfo"
|
}
|
|
}
|