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.

298 lines
9.2 KiB

package com.gyf.csams.account.model
import android.app.Application
import android.content.Intent
import android.os.Build
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.gyf.csams.AccountApi
import com.gyf.csams.Api
import com.gyf.csams.InitActivity
import com.gyf.csams.R
import com.gyf.csams.account.ui.AccountRoute
import com.gyf.csams.util.AppDatabase
import com.gyf.csams.util.SimpleCallback
import com.gyf.csams.util.TokenResDto
import com.gyf.lib.uikit.FormStatus
import com.gyf.lib.uikit.StringForm
import com.gyf.lib.uikit.ValidStringForm
import com.gyf.lib.util.ApiResponse
import com.gyf.lib.util.HttpClient
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?)
typealias Token = TokenResDto
/**
* 注册表单
*/
class AccountViewModel(application: Application) : AndroidViewModel(application) {
val welcomeEnd = application.getString(R.string.welcome_end)
val welcomeStart = application.getString(R.string.welcome_start)
//学号
val studentId = 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 : StringForm(formDesc = "姓名", textLength = 4) {
override fun onChange(value: String) {
super.onChange(value)
checkForm()
}
}
private val _isValidName = MutableLiveData<Boolean>()
val isValidName: LiveData<Boolean> = _isValidName
val nameFormat = "姓名不能为空"
//密码
val password = object : StringForm(formDesc = "密码", textLength = 8) {
override fun onChange(value: String) {
super.onChange(value)
checkForm()
}
}
private val _isValidPwd = MutableLiveData<Boolean>()
val isValidPwd: LiveData<Boolean> = _isValidPwd
val passwordFormat = "八位纯数字"
//注册按钮
private val _isValidForm = MutableLiveData<Boolean>()
val isValidForm: LiveData<Boolean> = _isValidForm
private val _dialogMsg = MutableLiveData<DialogMessage?>()
val dialogMsg: LiveData<DialogMessage?> = _dialogMsg
val loginDesc = "登陆"
//返回登陆
val backLogin = "返回$loginDesc"
//确定按钮
val confirmDesc = "确定"
//显示密码提示
val title = "提示信息"
val passwordTip = "密码会在点击${regBtnDesc}以后,在后台自动生成,请留意系统提示。"
val passwordDialogStart = "${regBtnDesc}成功,后台为您自动生成的密码是"
val passwordDialogEnd = "\n密码有且只有这里显示一次,请在记住密码后点击确定或${backLogin}"
//转到注册
var goRegister = "转到$regBtnDesc"
/**
* 完成登录状态
*/
private val _finishLogin = MutableLiveData<Boolean>()
val finishLogin: LiveData<Boolean> = _finishLogin
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("检测${studentId.formDesc},请求接口$url")
HttpClient.get(
url, SimpleCallback<Boolean>(
action = "${studentId.formDesc}重复检测",
onSuccess = {
if (it.body == true) {
result.postValue(FormStatus.Repeat)
} else {
result.postValue(FormStatus.Valid)
}
},
onFail = { Logger.e(it) },
type = object : TypeToken<ApiResponse<Boolean>>() {}.type
), mapOf("studentId" to "${studentId.formValue.value}")
)
}
}
}
/**
* 检测姓名
*
* @return
*/
private fun checkName(): Boolean {
_isValidName.value = name.formValue.value?.isNotEmpty()
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 {
if (checkJob?.isActive == true) {
_isValidForm.value = false
} else {
_isValidForm.value =
studentId.statusForm.value == FormStatus.Valid && (if (route == AccountRoute.Register) checkName() else checkPassword())
}
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
),
jsonBody = Gson().toJson(
UserVo(
studentId = "${studentId.formValue.value}",
name = "${name.formValue.value}"
)
)
)
resetForm()
} else {
Logger.wtf("表单校验失败,无法$regBtnDesc!!!")
}
}
fun resetDialogMsg() {
_dialogMsg.value = null
}
private fun resetForm() {
studentId.onChange("")
name.onChange("")
}
/**
* 登录
*
*/
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 = {
callback(it.message)
val context = getApplication<Application>().applicationContext
val token = it.body?.token
if (token != null) {
val db = AppDatabase.getInstance(context)
viewModelScope.launch {
db?.tokenDao()?.save(token = token)
}.invokeOnCompletion {
context.startActivity(Intent(context, InitActivity::class.java))
_finishLogin.postValue(true)
}
}
},
onFail = { callback(it) },
type = object : TypeToken<ApiResponse<Token>>() {}.type
),
jsonBody = Gson().toJson(
UserLoginVo(
studentId = studentId,
password = password,
device = "${Build.MANUFACTURER} ${Build.MODEL}"
)
)
)
} else {
Logger.wtf("表单校验失败,无法$loginDesc!!!")
}
}
}