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.
225 lines
6.9 KiB
225 lines
6.9 KiB
package com.gyf.csams.account.model
|
|
|
|
import android.app.Activity
|
|
import android.app.Application
|
|
import android.os.Build
|
|
import androidx.lifecycle.LiveData
|
|
import androidx.lifecycle.MutableLiveData
|
|
import androidx.lifecycle.viewModelScope
|
|
import com.google.gson.reflect.TypeToken
|
|
import com.gyf.csams.R
|
|
import com.gyf.csams.account.ui.AccountActivity
|
|
import com.gyf.csams.account.ui.AccountRoute
|
|
import com.gyf.csams.util.SimpleCallback
|
|
import com.gyf.lib.model.AbstractLoginViewModel
|
|
import com.gyf.lib.uikit.FormStatus
|
|
import com.gyf.lib.uikit.ValidStringForm
|
|
import com.gyf.lib.util.*
|
|
import com.orhanobut.logger.Logger
|
|
import kotlinx.coroutines.Job
|
|
import kotlinx.coroutines.launch
|
|
|
|
/**
|
|
* 响应自动生成密码
|
|
*
|
|
* @property password
|
|
*/
|
|
data class UserResDto(val password: String)
|
|
|
|
/**
|
|
* 构造登录、注册信息实体表单
|
|
*
|
|
* @property studentId 学号
|
|
* @property name 姓名
|
|
*/
|
|
data class UserVo(val studentId: String, val name: String)
|
|
|
|
/**
|
|
* 用户登陆表单
|
|
*
|
|
* @property studentId 学号
|
|
* @property password 密码
|
|
* @property device 设备型号
|
|
*/
|
|
data class UserLoginVo(val studentId: String, val password: String, val device: String)
|
|
|
|
/**
|
|
* 密码弹窗信息
|
|
*
|
|
* @property message
|
|
* @property userResDto
|
|
*/
|
|
data class DialogMessage(val message: String, val userResDto: UserResDto?)
|
|
|
|
/**
|
|
* 注册表单
|
|
*/
|
|
class AccountViewModel(application: Application) : AbstractLoginViewModel(application) {
|
|
val welcomeEnd = application.getString(R.string.welcome_end)
|
|
val welcomeStart = application.getString(R.string.welcome_start)
|
|
|
|
override val api: AccountApi = AccountApi.ForegroundLogin
|
|
|
|
override val clientType: ClientType = ClientType.Foreground
|
|
override val loginClass: Class<out Activity> = AccountActivity::class.java
|
|
|
|
//学号
|
|
override val id = object : ValidStringForm(formDesc = "学号", textLength = 8) {
|
|
override fun check() {
|
|
_formValue.value?.let {
|
|
when {
|
|
!it.matches(Regex("\\d{8}")) -> _statusForm.value = FormStatus.FormatError
|
|
route == AccountRoute.Login -> _statusForm.value = FormStatus.Valid
|
|
route == AccountRoute.Register -> checkRepeat(_statusForm)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
val regBtnDesc = application.getString(R.string.reg_btn)
|
|
|
|
//已注册
|
|
val registered = "已$regBtnDesc"
|
|
|
|
private var checkJob: Job? = null
|
|
|
|
//姓名
|
|
val name = object : ValidStringForm(formDesc = "姓名", textLength = 4) {
|
|
override fun check() {
|
|
_statusForm.value = FormStatus.Valid
|
|
checkForm()
|
|
}
|
|
}
|
|
val nameFormat = "姓名不能为空"
|
|
|
|
//密码
|
|
override val password = object : ValidStringForm(formDesc = "密码", textLength = 8) {
|
|
override fun check() {
|
|
_formValue.value?.let {
|
|
if (it.matches(Regex("\\d{8}"))) _statusForm.value =
|
|
FormStatus.Valid else _statusForm.value = FormStatus.FormatError
|
|
}
|
|
checkForm()
|
|
}
|
|
}
|
|
|
|
val passwordFormat = "八位纯数字"
|
|
|
|
|
|
private val _dialogMsg = MutableLiveData<DialogMessage?>()
|
|
val dialogMsg: LiveData<DialogMessage?> = _dialogMsg
|
|
|
|
//返回登陆
|
|
val backLogin = "返回$loginDesc"
|
|
|
|
//确定按钮
|
|
val confirmDesc = "确定"
|
|
|
|
//显示密码提示
|
|
val title = "提示信息"
|
|
val passwordTip = "密码会在点击${regBtnDesc}以后,在后台自动生成,请留意系统提示。"
|
|
val passwordDialogStart = "${regBtnDesc}成功,后台为您自动生成的密码是"
|
|
val passwordDialogEnd = "\n密码有且只有这里显示一次,请在记住密码后点击确定或${backLogin}。"
|
|
|
|
//转到注册
|
|
var goRegister = "转到$regBtnDesc"
|
|
|
|
lateinit var route: AccountRoute
|
|
|
|
/**
|
|
* 检查学号是否已注册
|
|
*
|
|
*/
|
|
private fun checkRepeat(result: MutableLiveData<FormStatus>) {
|
|
viewModelScope.launch {
|
|
if (checkJob?.isActive == true) {
|
|
checkJob?.cancel()
|
|
}
|
|
checkJob = viewModelScope.launch {
|
|
val url = Api.buildUrl(AccountApi.CheckId)
|
|
Logger.i("检测${id.formDesc},请求接口$url")
|
|
HttpClient.get(
|
|
url, SimpleCallback<Boolean>(
|
|
action = "${id.formDesc}重复检测",
|
|
onSuccess = {
|
|
if (it.body == true) {
|
|
result.postValue(FormStatus.Repeat)
|
|
} else {
|
|
result.postValue(FormStatus.Valid)
|
|
}
|
|
checkForm()
|
|
},
|
|
onFail = { Logger.e(it) },
|
|
type = object : TypeToken<ApiResponse<Boolean>>() {}.type
|
|
), mapOf("studentId" to "${id.formValue.value}")
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun loginParam(): Any {
|
|
val studentId = "${id.formValue.value}"
|
|
val password = "${password.formValue.value}"
|
|
return UserLoginVo(
|
|
studentId = studentId,
|
|
password = password,
|
|
device = "${Build.MANUFACTURER} ${Build.MODEL}"
|
|
)
|
|
}
|
|
|
|
override fun checkForm(): Boolean {
|
|
if (checkJob?.isActive == true) {
|
|
_isValidForm.postValue(false)
|
|
} else {
|
|
_isValidForm.postValue(
|
|
id.statusForm.value == FormStatus.Valid && (if (route == AccountRoute.Register) name.statusForm.value == FormStatus.Valid
|
|
else password.statusForm.value == FormStatus.Valid)
|
|
)
|
|
}
|
|
return _isValidForm.value == true
|
|
}
|
|
|
|
/**
|
|
* 注册
|
|
*
|
|
*/
|
|
fun register(onFail: (error: String) -> Unit) {
|
|
if (checkForm()) {
|
|
val url = Api.buildUrl(AccountApi.Register)
|
|
Logger.i("开始$regBtnDesc,请求接口:$url")
|
|
HttpClient.post(
|
|
url, SimpleCallback<UserResDto>(
|
|
action = regBtnDesc,
|
|
onSuccess = {
|
|
_dialogMsg.postValue(
|
|
DialogMessage(
|
|
message = it.message,
|
|
userResDto = it.body
|
|
)
|
|
)
|
|
},
|
|
onFail = onFail,
|
|
type = object : TypeToken<ApiResponse<UserResDto>>() {}.type
|
|
),
|
|
jsonParam = UserVo(
|
|
studentId = "${id.formValue.value}",
|
|
name = "${name.formValue.value}"
|
|
)
|
|
)
|
|
resetForm()
|
|
} else {
|
|
Logger.wtf("表单校验失败,无法$regBtnDesc!!!")
|
|
}
|
|
}
|
|
|
|
fun resetDialogMsg() {
|
|
_dialogMsg.value = null
|
|
}
|
|
|
|
private fun resetForm() {
|
|
id.clean()
|
|
name.clean()
|
|
}
|
|
|
|
|
|
} |