餐盘柜接口改造

This commit is contained in:
2025-12-26 14:05:41 +08:00
parent 5e4dc4baa4
commit 7a6c4012d1
28 changed files with 859 additions and 507 deletions
@@ -14,6 +14,7 @@ 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 com.sw.platecabinet.utils.SpTool
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -25,6 +26,11 @@ 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
@@ -34,36 +40,46 @@ class UserViewModel : BaseViewModel() {
/**
* 获取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 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(index: Int = 0) {
fun getUserFaceCache(pageNo: Int = 1, pageSize: Int = PAGE_SIZE) {
var currentPageNo = pageNo
launchWithLoading {
val response = repository.getUserFaceCache()
val response = repository.getUserFaceCache(pageNum = pageNo, pageSize = pageSize)
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))
if (list.isEmpty()) {
return@launchWithLoading
}
SpTool.setLastFaceTimestamp(System.currentTimeMillis())
val faceEntityList = list.map {
FaceEntity(it.userId, null, Base64.decode(it.faceFeatureStr))
}
withContext(Dispatchers.Default) {
faceApi.updateFaceData(index, faceEntity)
activeEngine()
faceApi.updateFaceData(pageNo, faceEntityList)
if (list.size >= pageSize) {
currentPageNo++
getUserFaceCache(currentPageNo)
}
//activeEngine()
}
}
}
@@ -112,14 +128,8 @@ class UserViewModel : BaseViewModel() {
/**
* 校验码登录
*/
fun loginWithPwd(equipmentCode: String = globalEquipmentCode, phone: String, password: String) {
fun loginWithPwd(loginParam: LoginParam) {
launchWithLoading {
val loginParam = LoginParam(
appVersion = GlobalData.appVersion,
equipmentCode = equipmentCode,
phone = phone,
password = password
)
val response = repository.equipmentBoxLogin(loginParam)
if (parseResponse(response)) {
_currentUserInfo.value = response.data
@@ -130,14 +140,10 @@ class UserViewModel : BaseViewModel() {
/**
* 通过用户id获取用户信息
*/
fun getUserInfoById(equipmentCode: String = globalEquipmentCode, memberId: Int, action:(EquipmentUserInfo?)->Unit={}) {
fun getUserInfoById(memberId: Int?, action:(EquipmentUserInfo?)->Unit={}) {
_currentUserInfo.value = null
launchWithLoading {
val loginParam = LoginParam(
appVersion = GlobalData.appVersion,
equipmentCode = equipmentCode,
memberId = memberId
)
val loginParam = LoginParam(memberId = memberId)
val response = repository.equipmentBoxLogin(loginParam)
if (parseResponse(response)) {
_currentUserInfo.value = response.data