|
|
@ -2,20 +2,20 @@ package com.gyf.csams.account.model |
|
|
|
|
|
|
|
|
|
|
|
import android.app.Application |
|
|
|
import android.app.Application |
|
|
|
import android.os.Build |
|
|
|
import android.os.Build |
|
|
|
import androidx.lifecycle.AndroidViewModel |
|
|
|
|
|
|
|
import androidx.lifecycle.LiveData |
|
|
|
import androidx.lifecycle.LiveData |
|
|
|
import androidx.lifecycle.MutableLiveData |
|
|
|
import androidx.lifecycle.MutableLiveData |
|
|
|
import androidx.lifecycle.viewModelScope |
|
|
|
import androidx.lifecycle.viewModelScope |
|
|
|
import com.google.gson.reflect.TypeToken |
|
|
|
import com.google.gson.reflect.TypeToken |
|
|
|
import com.gyf.csams.AccountApi |
|
|
|
|
|
|
|
import com.gyf.csams.Api |
|
|
|
|
|
|
|
import com.gyf.csams.R |
|
|
|
import com.gyf.csams.R |
|
|
|
import com.gyf.csams.account.ui.AccountRoute |
|
|
|
import com.gyf.csams.account.ui.AccountRoute |
|
|
|
import com.gyf.csams.util.SimpleCallback |
|
|
|
import com.gyf.csams.util.SimpleCallback |
|
|
|
|
|
|
|
import com.gyf.lib.model.AbstractLoginViewModel |
|
|
|
import com.gyf.lib.uikit.FormStatus |
|
|
|
import com.gyf.lib.uikit.FormStatus |
|
|
|
import com.gyf.lib.uikit.StringForm |
|
|
|
|
|
|
|
import com.gyf.lib.uikit.ValidStringForm |
|
|
|
import com.gyf.lib.uikit.ValidStringForm |
|
|
|
import com.gyf.lib.util.* |
|
|
|
import com.gyf.lib.util.AccountApi |
|
|
|
|
|
|
|
import com.gyf.lib.util.Api |
|
|
|
|
|
|
|
import com.gyf.lib.util.ApiResponse |
|
|
|
|
|
|
|
import com.gyf.lib.util.HttpClient |
|
|
|
import com.orhanobut.logger.Logger |
|
|
|
import com.orhanobut.logger.Logger |
|
|
|
import kotlinx.coroutines.Job |
|
|
|
import kotlinx.coroutines.Job |
|
|
|
import kotlinx.coroutines.launch |
|
|
|
import kotlinx.coroutines.launch |
|
|
@ -55,12 +55,14 @@ data class DialogMessage(val message: String, val userResDto: UserResDto?) |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 注册表单 |
|
|
|
* 注册表单 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class AccountViewModel(application: Application) : AndroidViewModel(application) { |
|
|
|
class AccountViewModel(application: Application) : AbstractLoginViewModel(application) { |
|
|
|
val welcomeEnd = application.getString(R.string.welcome_end) |
|
|
|
val welcomeEnd = application.getString(R.string.welcome_end) |
|
|
|
val welcomeStart = application.getString(R.string.welcome_start) |
|
|
|
val welcomeStart = application.getString(R.string.welcome_start) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override val api: AccountApi = AccountApi.ForegroundLogin |
|
|
|
|
|
|
|
|
|
|
|
//学号 |
|
|
|
//学号 |
|
|
|
val studentId = object : ValidStringForm(formDesc = "学号", textLength = 8) { |
|
|
|
override val id = object : ValidStringForm(formDesc = "学号", textLength = 8) { |
|
|
|
override fun check() { |
|
|
|
override fun check() { |
|
|
|
_formValue.value?.let { |
|
|
|
_formValue.value?.let { |
|
|
|
when { |
|
|
|
when { |
|
|
@ -80,38 +82,31 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
private var checkJob: Job? = null |
|
|
|
private var checkJob: Job? = null |
|
|
|
|
|
|
|
|
|
|
|
//姓名 |
|
|
|
//姓名 |
|
|
|
val name = object : StringForm(formDesc = "姓名", textLength = 4) { |
|
|
|
val name = object : ValidStringForm(formDesc = "姓名", textLength = 4) { |
|
|
|
override fun onChange(value: String) { |
|
|
|
override fun check() { |
|
|
|
super.onChange(value) |
|
|
|
_statusForm.value = FormStatus.Valid |
|
|
|
checkForm() |
|
|
|
checkForm() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
private val _isValidName = MutableLiveData<Boolean>() |
|
|
|
|
|
|
|
val isValidName: LiveData<Boolean> = _isValidName |
|
|
|
|
|
|
|
val nameFormat = "姓名不能为空" |
|
|
|
val nameFormat = "姓名不能为空" |
|
|
|
|
|
|
|
|
|
|
|
//密码 |
|
|
|
//密码 |
|
|
|
val password = object : StringForm(formDesc = "密码", textLength = 8) { |
|
|
|
override val password = object : ValidStringForm(formDesc = "密码", textLength = 8) { |
|
|
|
override fun onChange(value: String) { |
|
|
|
override fun check() { |
|
|
|
super.onChange(value) |
|
|
|
_formValue.value?.let { |
|
|
|
|
|
|
|
if (it.matches(Regex("\\d{8}"))) _statusForm.value = |
|
|
|
|
|
|
|
FormStatus.Valid else _statusForm.value = FormStatus.FormatError |
|
|
|
|
|
|
|
} |
|
|
|
checkForm() |
|
|
|
checkForm() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
private val _isValidPwd = MutableLiveData<Boolean>() |
|
|
|
|
|
|
|
val isValidPwd: LiveData<Boolean> = _isValidPwd |
|
|
|
|
|
|
|
val passwordFormat = "八位纯数字" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val passwordFormat = "八位纯数字" |
|
|
|
|
|
|
|
|
|
|
|
//注册按钮 |
|
|
|
|
|
|
|
private val _isValidForm = MutableLiveData<Boolean>() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val isValidForm: LiveData<Boolean> = _isValidForm |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val _dialogMsg = MutableLiveData<DialogMessage?>() |
|
|
|
private val _dialogMsg = MutableLiveData<DialogMessage?>() |
|
|
|
val dialogMsg: LiveData<DialogMessage?> = _dialogMsg |
|
|
|
val dialogMsg: LiveData<DialogMessage?> = _dialogMsg |
|
|
|
|
|
|
|
|
|
|
|
val loginDesc = "登陆" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//返回登陆 |
|
|
|
//返回登陆 |
|
|
|
val backLogin = "返回$loginDesc" |
|
|
|
val backLogin = "返回$loginDesc" |
|
|
|
|
|
|
|
|
|
|
@ -127,12 +122,6 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
//转到注册 |
|
|
|
//转到注册 |
|
|
|
var goRegister = "转到$regBtnDesc" |
|
|
|
var goRegister = "转到$regBtnDesc" |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 完成登录状态 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private val _finishLogin = MutableLiveData<Boolean>() |
|
|
|
|
|
|
|
val finishLogin: LiveData<Boolean> = _finishLogin |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lateinit var route: AccountRoute |
|
|
|
lateinit var route: AccountRoute |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -146,51 +135,44 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
} |
|
|
|
} |
|
|
|
checkJob = viewModelScope.launch { |
|
|
|
checkJob = viewModelScope.launch { |
|
|
|
val url = Api.buildUrl(AccountApi.CheckId) |
|
|
|
val url = Api.buildUrl(AccountApi.CheckId) |
|
|
|
Logger.i("检测${studentId.formDesc},请求接口$url") |
|
|
|
Logger.i("检测${id.formDesc},请求接口$url") |
|
|
|
HttpClient.get( |
|
|
|
HttpClient.get( |
|
|
|
url, SimpleCallback<Boolean>( |
|
|
|
url, SimpleCallback<Boolean>( |
|
|
|
action = "${studentId.formDesc}重复检测", |
|
|
|
action = "${id.formDesc}重复检测", |
|
|
|
onSuccess = { |
|
|
|
onSuccess = { |
|
|
|
if (it.body == true) { |
|
|
|
if (it.body == true) { |
|
|
|
result.postValue(FormStatus.Repeat) |
|
|
|
result.postValue(FormStatus.Repeat) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
result.postValue(FormStatus.Valid) |
|
|
|
result.postValue(FormStatus.Valid) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
checkForm() |
|
|
|
}, |
|
|
|
}, |
|
|
|
onFail = { Logger.e(it) }, |
|
|
|
onFail = { Logger.e(it) }, |
|
|
|
type = object : TypeToken<ApiResponse<Boolean>>() {}.type |
|
|
|
type = object : TypeToken<ApiResponse<Boolean>>() {}.type |
|
|
|
), mapOf("studentId" to "${studentId.formValue.value}") |
|
|
|
), mapOf("studentId" to "${id.formValue.value}") |
|
|
|
) |
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
override fun loginParam(): Any { |
|
|
|
* 检测姓名 |
|
|
|
val studentId = "${id.formValue.value}" |
|
|
|
* |
|
|
|
val password = "${password.formValue.value}" |
|
|
|
* @return |
|
|
|
return UserLoginVo( |
|
|
|
*/ |
|
|
|
studentId = studentId, |
|
|
|
private fun checkName(): Boolean { |
|
|
|
password = password, |
|
|
|
_isValidName.value = name.formValue.value?.isNotEmpty() |
|
|
|
device = "${Build.MANUFACTURER} ${Build.MODEL}" |
|
|
|
return _isValidName.value == true |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 检测密码 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private fun checkPassword(): Boolean { |
|
|
|
|
|
|
|
_isValidPwd.value = password.formValue.value?.matches(Regex("\\d{8}")) |
|
|
|
|
|
|
|
return _isValidPwd.value == true |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun checkForm(): Boolean { |
|
|
|
override fun checkForm(): Boolean { |
|
|
|
if (checkJob?.isActive == true) { |
|
|
|
if (checkJob?.isActive == true) { |
|
|
|
_isValidForm.value = false |
|
|
|
_isValidForm.postValue(false) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
_isValidForm.value = |
|
|
|
_isValidForm.postValue( |
|
|
|
studentId.statusForm.value == FormStatus.Valid && (if (route == AccountRoute.Register) checkName() else checkPassword()) |
|
|
|
id.statusForm.value == FormStatus.Valid && (if (route == AccountRoute.Register) name.statusForm.value == FormStatus.Valid |
|
|
|
|
|
|
|
else password.statusForm.value == FormStatus.Valid) |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
return _isValidForm.value == true |
|
|
|
return _isValidForm.value == true |
|
|
|
} |
|
|
|
} |
|
|
@ -218,7 +200,7 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
type = object : TypeToken<ApiResponse<UserResDto>>() {}.type |
|
|
|
type = object : TypeToken<ApiResponse<UserResDto>>() {}.type |
|
|
|
), |
|
|
|
), |
|
|
|
jsonParam = UserVo( |
|
|
|
jsonParam = UserVo( |
|
|
|
studentId = "${studentId.formValue.value}", |
|
|
|
studentId = "${id.formValue.value}", |
|
|
|
name = "${name.formValue.value}" |
|
|
|
name = "${name.formValue.value}" |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
|
) |
|
|
@ -233,52 +215,8 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun resetForm() { |
|
|
|
private fun resetForm() { |
|
|
|
studentId.onChange("") |
|
|
|
id.clean() |
|
|
|
name.onChange("") |
|
|
|
name.clean() |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 登录 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
fun login(callback: (message: String) -> Unit) { |
|
|
|
|
|
|
|
if (checkForm()) { |
|
|
|
|
|
|
|
val url = Api.buildUrl(AccountApi.Login) |
|
|
|
|
|
|
|
Logger.i("开始$loginDesc,请求接口:$url") |
|
|
|
|
|
|
|
val studentId = "${studentId.formValue.value}" |
|
|
|
|
|
|
|
val password = "${password.formValue.value}" |
|
|
|
|
|
|
|
this.studentId.formValue |
|
|
|
|
|
|
|
Logger.i("使用学号:$studentId,密码:$password 进行登录") |
|
|
|
|
|
|
|
HttpClient.post( |
|
|
|
|
|
|
|
url, |
|
|
|
|
|
|
|
SimpleCallback<Token>( |
|
|
|
|
|
|
|
action = loginDesc, |
|
|
|
|
|
|
|
onSuccess = { |
|
|
|
|
|
|
|
Logger.i(it.message) |
|
|
|
|
|
|
|
callback(it.message) |
|
|
|
|
|
|
|
val context = getApplication<Application>().applicationContext |
|
|
|
|
|
|
|
it.body?.let { |
|
|
|
|
|
|
|
val db = AppDatabase.getInstance(context) |
|
|
|
|
|
|
|
viewModelScope.launch { |
|
|
|
|
|
|
|
TokenManager.token = it |
|
|
|
|
|
|
|
db?.tokenDao()?.save(token = it) |
|
|
|
|
|
|
|
}.invokeOnCompletion { |
|
|
|
|
|
|
|
_finishLogin.postValue(true) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
onFail = { callback(it) }, |
|
|
|
|
|
|
|
type = object : TypeToken<ApiResponse<Token>>() {}.type |
|
|
|
|
|
|
|
), |
|
|
|
|
|
|
|
jsonParam = UserLoginVo( |
|
|
|
|
|
|
|
studentId = studentId, |
|
|
|
|
|
|
|
password = password, |
|
|
|
|
|
|
|
device = "${Build.MANUFACTURER} ${Build.MODEL}" |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
Logger.wtf("表单校验失败,无法$loginDesc!!!") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|