|
|
|
@ -2,20 +2,20 @@ package com.gyf.csams.account.model |
|
|
|
|
|
|
|
|
|
import android.app.Application |
|
|
|
|
import android.os.Build |
|
|
|
|
import androidx.lifecycle.AndroidViewModel |
|
|
|
|
import androidx.lifecycle.LiveData |
|
|
|
|
import androidx.lifecycle.MutableLiveData |
|
|
|
|
import androidx.lifecycle.viewModelScope |
|
|
|
|
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.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.StringForm |
|
|
|
|
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 kotlinx.coroutines.Job |
|
|
|
|
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 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() { |
|
|
|
|
_formValue.value?.let { |
|
|
|
|
when { |
|
|
|
@ -80,38 +82,31 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
|
private var checkJob: Job? = null |
|
|
|
|
|
|
|
|
|
//姓名 |
|
|
|
|
val name = object : StringForm(formDesc = "姓名", textLength = 4) { |
|
|
|
|
override fun onChange(value: String) { |
|
|
|
|
super.onChange(value) |
|
|
|
|
val name = object : ValidStringForm(formDesc = "姓名", textLength = 4) { |
|
|
|
|
override fun check() { |
|
|
|
|
_statusForm.value = FormStatus.Valid |
|
|
|
|
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) |
|
|
|
|
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() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
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?>() |
|
|
|
|
val dialogMsg: LiveData<DialogMessage?> = _dialogMsg |
|
|
|
|
|
|
|
|
|
val loginDesc = "登陆" |
|
|
|
|
|
|
|
|
|
//返回登陆 |
|
|
|
|
val backLogin = "返回$loginDesc" |
|
|
|
|
|
|
|
|
@ -127,12 +122,6 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
|
//转到注册 |
|
|
|
|
var goRegister = "转到$regBtnDesc" |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 完成登录状态 |
|
|
|
|
*/ |
|
|
|
|
private val _finishLogin = MutableLiveData<Boolean>() |
|
|
|
|
val finishLogin: LiveData<Boolean> = _finishLogin |
|
|
|
|
|
|
|
|
|
lateinit var route: AccountRoute |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -146,51 +135,44 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
|
} |
|
|
|
|
checkJob = viewModelScope.launch { |
|
|
|
|
val url = Api.buildUrl(AccountApi.CheckId) |
|
|
|
|
Logger.i("检测${studentId.formDesc},请求接口$url") |
|
|
|
|
Logger.i("检测${id.formDesc},请求接口$url") |
|
|
|
|
HttpClient.get( |
|
|
|
|
url, SimpleCallback<Boolean>( |
|
|
|
|
action = "${studentId.formDesc}重复检测", |
|
|
|
|
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 "${studentId.formValue.value}") |
|
|
|
|
), mapOf("studentId" to "${id.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 |
|
|
|
|
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}" |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun checkForm(): Boolean { |
|
|
|
|
override fun checkForm(): Boolean { |
|
|
|
|
if (checkJob?.isActive == true) { |
|
|
|
|
_isValidForm.value = false |
|
|
|
|
_isValidForm.postValue(false) |
|
|
|
|
} else { |
|
|
|
|
_isValidForm.value = |
|
|
|
|
studentId.statusForm.value == FormStatus.Valid && (if (route == AccountRoute.Register) checkName() else checkPassword()) |
|
|
|
|
_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 |
|
|
|
|
} |
|
|
|
@ -218,7 +200,7 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
|
type = object : TypeToken<ApiResponse<UserResDto>>() {}.type |
|
|
|
|
), |
|
|
|
|
jsonParam = UserVo( |
|
|
|
|
studentId = "${studentId.formValue.value}", |
|
|
|
|
studentId = "${id.formValue.value}", |
|
|
|
|
name = "${name.formValue.value}" |
|
|
|
|
) |
|
|
|
|
) |
|
|
|
@ -233,52 +215,8 @@ class AccountViewModel(application: Application) : AndroidViewModel(application) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
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 = { |
|
|
|
|
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!!!") |
|
|
|
|
} |
|
|
|
|
id.clean() |
|
|
|
|
name.clean() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|