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.GlobalData.globalEquipmentCode import com.sw.platecabinet.GlobalKey import com.sw.platecabinet.model.DeviceConfig 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 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(null) val currentUserInfo: StateFlow = _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) { var currentPageNo = pageNo launchWithLoading { val response = repository.getUserFaceCache(pageNum = pageNo, pageSize = pageSize) if (parseResponse(response)) { // 获取成功一次后缓存状态 SPUtil.getInstance().put(GlobalKey.KEY_FIRST_RUN, true) val list: List = response.data ?: emptyList() if (pageNo==1&&list.isEmpty()) { return@launchWithLoading } val faceEntityList = list.map { FaceEntity(it.userId, null, Base64.decode(it.faceFeatureStr)) } withContext(Dispatchers.Default) { faceApi.updateFaceData(pageNo, faceEntityList) if (list.size >= pageSize) { faceTimestamp = list.last().faceUpdateTimestamp?:0 currentPageNo++ getUserFaceCache(currentPageNo) return@withContext } if (list.isNotEmpty()) { faceTimestamp = list.last().faceUpdateTimestamp?:0 } SpTool.setLastFaceTimestamp(faceTimestamp) //activeEngine() } } } } private var faceTimestamp = 0L /** * 通过用户id获取用户信息 * * 无论成功或失败(如「未找到用户绑定信息」)都会回调 action, * 以便调用方统一收尾(例如关闭 loading 弹窗)。 * 成功时回调绑定的用户信息,失败时回调 null。 * * @param silent true 表示失败时不弹错误 toast。绑定页点会员检查是否已绑定 * 时,「未找到绑定信息」属于正常态,应静默处理。 */ fun getUserInfoById(memberId: String?, silent: Boolean = false, action:(EquipmentUserInfo?)->Unit={}) { _currentUserInfo.value = null launchWithLoading { val response = repository.getMemberRefPlateByUserId(userId = memberId) if (response.isSuccess()) { _currentUserInfo.value = response.data action(response.data) } else { if (!silent) { Timber.d("msg = ${response.msg}, code = ${response.code}") ToastUtils.showToast("${response.msg}(${response.code})") } action(null) } } } /** * 重置用户信息 */ 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) } } } }