添加了界面及逻辑
This commit is contained in:
@@ -1,10 +1,31 @@
|
||||
package com.sw.platecabinet.viewmodel
|
||||
|
||||
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.globalEquipmentCode
|
||||
import com.sw.platecabinet.GlobalKey
|
||||
import com.sw.platecabinet.model.request.LoginParam
|
||||
import com.sw.platecabinet.model.response.EquipmentUserInfo
|
||||
import com.sw.platecabinet.model.response.UserFaceModel
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import timber.log.Timber
|
||||
|
||||
class UserViewModel : BaseViewModel() {
|
||||
// 通过人脸获取到的用户信息
|
||||
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)
|
||||
@@ -19,25 +40,91 @@ class UserViewModel : BaseViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
fun getUserFaceCache() {
|
||||
/**
|
||||
* 获取人脸数据
|
||||
*/
|
||||
fun getUserFaceCache(index: Int = 0) {
|
||||
launchWithLoading {
|
||||
val response = repository.getUserFaceCache()
|
||||
if (parseResponse(response)) {
|
||||
// 获取成功一次后缓存状态
|
||||
SPUtil.getInstance().put(GlobalKey.KEY_FIRST_RUN, true)
|
||||
val list: List<UserFaceModel> = response.data ?: emptyList()
|
||||
val faceEntity = list.map {
|
||||
FaceEntity(it.userId, null, Base64.decode(it.faceFeatureString))
|
||||
}
|
||||
faceApi.updateFaceData(index, faceEntity)
|
||||
activeEngine()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活 arcsoft 人脸
|
||||
* 激活人脸识别引擎
|
||||
*/
|
||||
fun activeEngine(
|
||||
appId: String = "Hkz1rBk6PZXbS8KwKr67K2eZtsz8bRoMHLg64bUdgCZj",
|
||||
sdkKey: String = "AabXs3sHM8UhhE7oCGf4LVMLrdkGb1nJNksbjjTdVt7k",
|
||||
activeKey: String = "085F-118G-Q391-53YL"
|
||||
) {
|
||||
// val runtimeABI: RuntimeABI? = FaceEngine.getRuntimeABI()
|
||||
private fun activeEngine() {
|
||||
var appId = "Hkz1rBk6PZXbS8KwKr67K2eZtsz8bRoMHLg64bUdgCZj"
|
||||
var sdkKey = "AabXs3sHM8UhhE7oCGf4LVMLrdkGb1nJNksbjjTdVt7k"
|
||||
var activeKey = "085F-118G-Q391-53YL"
|
||||
faceApi.activeEngine(
|
||||
App.getContext(),
|
||||
appId,
|
||||
sdkKey,
|
||||
activeKey,
|
||||
object : FaceApi.ActiveCallback {
|
||||
override fun onSuccess(activeCode: Int) {
|
||||
Timber.d("activeEngine activeCode = $activeCode")
|
||||
when (activeCode) {
|
||||
ErrorInfo.MOK -> {
|
||||
ToastUtils.showToast("激活引擎成功")
|
||||
}
|
||||
|
||||
ErrorInfo.MERR_ASF_ALREADY_ACTIVATED -> {
|
||||
ToastUtils.showToast("引擎已激活,无需再次激活")
|
||||
}
|
||||
|
||||
else -> {
|
||||
ToastUtils.showToast("激活引擎失败($activeCode)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFail(e: Exception?) {
|
||||
ToastUtils.showToast("激活引擎异常,${e?.message}")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验码登录
|
||||
*/
|
||||
fun loginWithPwd(equipmentCode: String = globalEquipmentCode, phone: String, password: String) {
|
||||
launchWithLoading {
|
||||
val loginParam = LoginParam(
|
||||
equipmentCode = equipmentCode,
|
||||
phone = phone,
|
||||
password = password
|
||||
)
|
||||
val response = repository.equipmentBoxLogin(loginParam)
|
||||
if (parseResponse(response)) {
|
||||
_currentUserInfo.value = response.data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过用户id获取用户信息
|
||||
*/
|
||||
fun getUserInfoById(equipmentCode: String = globalEquipmentCode, memberId: Int) {
|
||||
launchWithLoading {
|
||||
val loginParam = LoginParam(
|
||||
equipmentCode = equipmentCode,
|
||||
memberId = memberId
|
||||
)
|
||||
val response = repository.equipmentBoxLogin(loginParam)
|
||||
if (parseResponse(response)) {
|
||||
_currentUserInfo.value = response.data
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user