You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
csamsclient/app/src/main/java/com/gyf/csams/account/model/RegisterViewModel.kt

204 lines
5.9 KiB

package com.gyf.csams.account.model
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.gyf.csams.Api
import com.gyf.csams.RegisterApi
import com.gyf.csams.util.ApiResponse
import com.gyf.csams.util.HttpClient
import com.gyf.csams.util.SimpleCallback
import com.orhanobut.logger.Logger
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable
@Serializable
data class UserResDto(val password:String)
data class UserVo(val studentId:String,val name:String)
data class DialogMessage(val message:String,val userResDto: UserResDto?)
/**
* 注册表单
*/
class RegisterViewModel:ViewModel() {
//欢迎信息
val welcomeStart="同学您好\n"
val welcomeEnd="欢迎使用"
//学号
private val _studentId=MutableLiveData<String>()
val studentId:LiveData<String> = _studentId
private val _isValidStudentId=MutableLiveData<Boolean>()
val isValidStudentId:LiveData<Boolean> = _isValidStudentId
val studentIdDesc="学号"
val studentIdPlaceholder="学号纯数字"
val studentIdFormat="入学年份(四位)+班级代码(两位)+学生代码(两位)"
//学号已存在
private val _isRepeat=MutableLiveData<Boolean?>()
//已注册
val registered="已注册"
//可注册
val canRegister="可注册"
//提示信息
val checkRegTip="检测学号是否已注册。。。"
val isRepeat:LiveData<Boolean?> = _isRepeat
private var checkJob: Job? = null
//姓名
private val _name=MutableLiveData<String>()
val name:LiveData<String> = _name
val nameDesc="姓名"
val namePlaceholder=nameDesc
private val _isValidName=MutableLiveData<Boolean>()
val isValidName:LiveData<Boolean> = _isValidName
val nameFormat="姓名不能为空"
//注册按钮
private val _isValidForm=MutableLiveData<Boolean>()
val regBtnDesc="注册"
val isValidForm:LiveData<Boolean> = _isValidForm
//注册请求响应信息
private val _snackBarMsg=MutableLiveData<String>()
val snackBarMsg:LiveData<String> = _snackBarMsg
private val _dialogMsg=MutableLiveData<DialogMessage>()
val dialogMsg:LiveData<DialogMessage> = _dialogMsg
//返回登陆
val backDesc="返回登陆"
//确定按钮
val confirmDesc="确定"
//显示密码提示
val title="提示信息"
val passwordTip="密码会在点击注册以后,在后台自动生成,请留意系统提示。"
val passwordDialogStart="注册成功,后台为您自动生成的密码是"
val passwordDialogEnd="\n密码有且只有这里显示一次,请在记住密码后点击确定或${backDesc}"
/**
* 更新学号
*
* @param studentId 学号
*/
fun onStudentIdChange(studentId:String){
_studentId.value=studentId
viewModelScope.launch {
checkRepeat()
}
}
/**
* 检查学号格式
*
*/
private fun checkStudentId(): Boolean {
_isValidStudentId.value= _studentId.value?.matches(Regex("\\d{8}"))
return _isValidStudentId.value==true
}
/**
* 检查学号是否已注册
*
*/
suspend fun checkRepeat(){
if (checkStudentId()) {
if (checkJob?.isActive == true) {
checkJob?.join()
}else {
_isRepeat.postValue(null)
checkJob = viewModelScope.launch {
val url = Api.buildUrl(RegisterApi.checkId)
Logger.i("检测$studentIdDesc,请求接口$url")
HttpClient.get(
url, SimpleCallback<Boolean>(
action = "${studentIdDesc}重复检测",
onSuccess = { _isRepeat.postValue(it.body)
_isValidForm.postValue( _isValidName.value==true && it.body==false)
},
onFail = { _snackBarMsg.postValue(it) },
type = object : TypeToken<ApiResponse<Boolean>>() {}.type)
, mapOf("studentId" to "${_studentId.value}"))
}
}
}else{
_isValidForm.postValue(false)
}
}
/**
* 更新姓名
*
* @param name 姓名
*/
fun onNameChange(name:String){
_name.value=name
checkForm()
}
/**
* 检测姓名
*
* @return
*/
private fun checkName():Boolean{
_isValidName.value= _name.value?.isNotEmpty()
return _isValidName.value==true
}
private fun checkForm(): Boolean {
if(checkJob?.isActive==true){
_isValidForm.value = false
}else{
_isValidForm.value = checkName() && checkStudentId() && isRepeat.value==false
}
return _isValidForm.value == true
}
/**
* 注册
*
*/
fun register(){
if(checkForm()){
val url= Api.buildUrl(RegisterApi.register)
Logger.i("开始注册,请求接口:$url")
HttpClient.post(url,SimpleCallback<UserResDto>(
action = "注册",
onSuccess = { _dialogMsg.postValue(DialogMessage(message = it.message,userResDto = it.body)) },
onFail = { _snackBarMsg.postValue(it)},
type = object : TypeToken<ApiResponse<UserResDto>>() {}.type),
jsonBody = Gson().toJson(UserVo(studentId = "${studentId.value}",name = "${name.value}")))
resetForm()
}else{
Logger.wtf("表单校验失败,无法注册!!!")
}
}
/**
* 重置信息
*
*/
fun resetRegisterResMsg(){
_snackBarMsg.value=""
}
fun resetDialogMsg(){
_dialogMsg.value=null
}
private fun resetForm(){
_studentId.value=""
_name.value=""
}
}