196 lines
6.4 KiB
Kotlin
196 lines
6.4 KiB
Kotlin
package com.sw.platecabinet.viewmodel
|
|
|
|
import androidx.lifecycle.viewModelScope
|
|
import com.arcsoft.face.ErrorInfo
|
|
import com.sw.inbound.utils.SPUtil
|
|
import com.sw.plate.App
|
|
import com.sw.plate.utils.Base64
|
|
import com.sw.plate.utils.ToastUtils
|
|
import com.sw.plate.utils.arcface.FaceApi
|
|
import com.sw.plate.utils.arcface.facedb.entity.FaceEntity
|
|
import com.sw.platecabinet.GlobalData
|
|
import com.sw.platecabinet.GlobalKey
|
|
import com.sw.platecabinet.model.DeviceConfig
|
|
import com.sw.platecabinet.model.request.LoginParam
|
|
import com.sw.platecabinet.model.response.UserFaceModel
|
|
import com.sw.platecabinet.utils.SpTool
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.launch
|
|
import kotlinx.coroutines.withContext
|
|
import timber.log.Timber
|
|
|
|
/**
|
|
* 用户viewmodel
|
|
*/
|
|
class UserViewModel : BaseViewModel() {
|
|
|
|
companion object {
|
|
const val PAGE_SIZE = 100
|
|
}
|
|
|
|
// 通过人脸获取到的用户信息
|
|
// private val _currentUserInfo = MutableStateFlow<EquipmentUserInfo?>(null)
|
|
// val currentUserInfo: StateFlow<EquipmentUserInfo?> = _currentUserInfo
|
|
|
|
private val faceApi: FaceApi = FaceApi()
|
|
|
|
/**
|
|
* 获取token
|
|
*/
|
|
// fun generateToken(deviceId: String = "SWSN:88:12:AC:4E:D9:CC") {
|
|
// launchWithLoading {
|
|
// val response = repository.generateToken(deviceId)
|
|
// if (parseResponse(response)) {
|
|
// // 缓存token
|
|
// SPUtil.getInstance().put(GlobalKey.KEY_TOKEN, response.data)
|
|
// // 首次运行获取人脸数据
|
|
// if (SPUtil.getInstance().get(GlobalKey.KEY_FIRST_RUN, false) != true) {
|
|
// getUserFaceCache()
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* 获取人脸数据
|
|
*/
|
|
fun getUserFaceCache(
|
|
pageNo: Int = 1,
|
|
pageSize: Int = PAGE_SIZE,
|
|
callback: (Boolean, String?) -> Unit = { statue, msg -> }
|
|
) {
|
|
var currentPageNo = pageNo
|
|
launchWithLoading {
|
|
val response = repository.getUserFaceCache(pageNum = pageNo, pageSize = pageSize)
|
|
val status = parseResponse(response)
|
|
if (status.not()) {
|
|
callback(false, response.msg)
|
|
return@launchWithLoading
|
|
}
|
|
// 获取成功一次后缓存状态
|
|
SPUtil.getInstance().put(GlobalKey.KEY_FIRST_RUN, true)
|
|
val list: List<UserFaceModel> = response.data ?: emptyList()
|
|
if (pageNo == 1 && list.isEmpty()) {
|
|
callback(true, "暂无数据")
|
|
return@launchWithLoading
|
|
}
|
|
val faceEntityList = list.map {
|
|
FaceEntity(it.userId, null, Base64.decode(it.faceFeatureStr)).apply {
|
|
userType = if (it.member) "1" else "2"
|
|
}
|
|
}
|
|
withContext(Dispatchers.Default) {
|
|
if (currentPageNo == 1) {
|
|
//第一页数据删除本地数据库
|
|
faceApi.clearFaceData()
|
|
}
|
|
faceApi.updateFaceData( pageNo, faceEntityList)
|
|
if (list.size >= pageSize) {
|
|
faceTimestamp = list.last().faceUpdateTimestamp ?: 0
|
|
currentPageNo++
|
|
getUserFaceCache(
|
|
pageNo = currentPageNo,
|
|
callback = callback
|
|
)
|
|
return@withContext
|
|
}
|
|
if (list.isNotEmpty()) {
|
|
faceTimestamp = list.last().faceUpdateTimestamp ?: 0
|
|
}
|
|
SpTool.lastFaceTimestamp = faceTimestamp
|
|
callback(true, "查询完成")
|
|
}
|
|
}
|
|
}
|
|
|
|
private var faceTimestamp = 0L
|
|
|
|
|
|
/**
|
|
* 激活人脸识别引擎
|
|
*/
|
|
fun activeEngine() {
|
|
Timber.d("activeEngine")
|
|
|
|
faceApi.activeEngine(
|
|
App.getContext(),
|
|
GlobalData.appId,
|
|
GlobalData.sdkKey,
|
|
GlobalData.activeKey,
|
|
object : FaceApi.ActiveCallback {
|
|
override fun onSuccess(activeCode: Int) {
|
|
Timber.d("activeEngine activeCode = $activeCode")
|
|
// ThreadUtils.launch {
|
|
viewModelScope.launch(Dispatchers.Main) {
|
|
when (activeCode) {
|
|
ErrorInfo.MOK -> {
|
|
ToastUtils.showToast("激活引擎成功")
|
|
}
|
|
|
|
ErrorInfo.MERR_ASF_ALREADY_ACTIVATED -> {
|
|
// ToastUtils.showToast("引擎已激活,无需再次激活")
|
|
}
|
|
|
|
else -> {
|
|
ToastUtils.showToast("激活引擎失败($activeCode)")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onFail(e: Exception?) {
|
|
viewModelScope.launch(Dispatchers.Main) {
|
|
ToastUtils.showToast("激活引擎异常,${e?.message}")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 校验码登录
|
|
*/
|
|
fun loginWithPwd(loginParam: LoginParam) {
|
|
// launchWithLoading {
|
|
// val response = repository.equipmentBoxLogin(loginParam)
|
|
// if (parseResponse(response)) {
|
|
// _currentUserInfo.value = response.data
|
|
// }
|
|
// }
|
|
}
|
|
|
|
/**
|
|
* 通过用户id获取用户信息
|
|
*/
|
|
// fun getUserInfoById(memberId: String?, action: (EquipmentUserInfo?) -> Unit = {}) {
|
|
// _currentUserInfo.value = null
|
|
// launchWithLoading {
|
|
// val loginParam = LoginParam(faceId = memberId)
|
|
// val response = repository.equipmentBoxLogin(loginParam)
|
|
// if (parseResponse(response)) {
|
|
// _currentUserInfo.value = response.data
|
|
// action(response.data)
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
/**
|
|
* 重置用户信息
|
|
*/
|
|
fun resetUserInfo() {
|
|
Timber.d("resetUserInfo")
|
|
// _currentUserInfo.value = null
|
|
}
|
|
|
|
fun getDeviceConfig(block: (DeviceConfig?) -> Unit) {
|
|
// Timber.tag(TAG).d("getDeviceConfig")
|
|
launch {
|
|
val response = repository.getDeviceConfig()
|
|
if (parseResponse(response)) {
|
|
block(response.data)
|
|
} else {
|
|
block(null)
|
|
}
|
|
}
|
|
}
|
|
|
|
} |