添加了界面及逻辑

This commit is contained in:
zxj
2025-07-22 15:48:12 +08:00
parent 9d14b5499b
commit 5ad82eb224
40 changed files with 2157 additions and 182 deletions
@@ -1,7 +1,12 @@
package com.sw.platecabinet.viewmodel
import com.sw.platecabinet.GlobalData.globalEquipmentCode
import com.sw.platecabinet.model.ErrorInfo
import com.sw.platecabinet.model.request.BindParam
import com.sw.platecabinet.model.request.EquipmentParam
import com.sw.platecabinet.model.request.SearchParam
import com.sw.platecabinet.model.response.EquipmentUserInfo
import com.sw.platecabinet.model.response.SearchResult
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -10,12 +15,99 @@ class SettingViewModel : BaseViewModel() {
private val _equipmentList = MutableStateFlow<List<EquipmentUserInfo>>(emptyList())
val equipmentList: StateFlow<List<EquipmentUserInfo>> = _equipmentList
fun getEquipmentList(param: EquipmentParam) {
private val _searchMemberList = MutableStateFlow<List<SearchResult.Member>>(emptyList())
val searchMemberList: StateFlow<List<SearchResult.Member>> = _searchMemberList
var currentPage = 1
var isLoading = false
var canLoadMore = true
/**
* 绑定/解绑状态 <绑定/解绑, 错误信息,>
*/
private val _bindStateChange = MutableStateFlow<Pair<Boolean?, ErrorInfo>>(null to ErrorInfo())
val bindStateChange: StateFlow<Pair<Boolean?, ErrorInfo>> = _bindStateChange
/**
* 获取设备绑定用户信息列表
*/
fun getEquipmentList(equipmentCode: String = globalEquipmentCode) {
launchWithLoading {
val param = EquipmentParam(equipmentCode = equipmentCode)
val response = repository.getEquipmentList(param)
if (parseResponse(response)) {
_equipmentList.value = response.data ?: emptyList()
}
}
}
/**
* 搜索会员信息列表
*/
fun getSearchMemberList(param: String, pageNum: Int = 1, pageSize: Int = 50) {
currentPage = pageNum
launch {
val param = SearchParam(
param = param,
pageNum = pageNum,
pageSize = pageSize
)
isLoading = true
val response = repository.searchUser(param)
isLoading = false
if (parseResponse(response)) {
response.data?.let {
canLoadMore = it.hasNextPage != false
val newList = it.list ?: emptyList()
if (pageNum == 1) {
_searchMemberList.value = newList
} else {
_searchMemberList.value = _searchMemberList.value.plus(newList)
}
currentPage++
}
}
}
}
fun bindPlate(
equipmentBoxCode: String,
equipmentCode: String = globalEquipmentCode,
memberId: Int,
plateNumber: String
) {
launchWithLoading {
val bindParam = BindParam(
equipmentBoxCode = equipmentBoxCode,
equipmentCode = equipmentCode,
memberId = memberId,
plateNumber = plateNumber
)
val response = repository.bindEquipment(bindParam)
if (parseResponse(response)) {
_bindStateChange.value = true to ErrorInfo()
} else {
_bindStateChange.value = true to ErrorInfo(response.code, response.msg.toString())
}
}
}
fun unbindPlate(
equipmentBoxCode: String,
equipmentCode: String = globalEquipmentCode
) {
launchWithLoading {
val bindParam = BindParam(
equipmentBoxCode = equipmentBoxCode,
equipmentCode = equipmentCode
)
val response = repository.bindEquipment(bindParam)
if (parseResponse(response)) {
_bindStateChange.value = false to ErrorInfo()
} else {
_bindStateChange.value = false to ErrorInfo(response.code, response.msg.toString())
}
}
}
}
@@ -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
}
}
}
}