接口联调

This commit is contained in:
2025-11-28 18:46:41 +08:00
parent 9bcd7c9983
commit 29188c5944
39 changed files with 1882 additions and 1662 deletions
@@ -1,16 +1,15 @@
package com.sw.dualscreen.viewmodel
import android.content.Context
import android.net.Uri
import androidx.lifecycle.viewModelScope
import com.arcsoft.face.ErrorInfo
import com.sw.dualscreen.GlobalData
import com.sw.dualscreen.GlobalKey
import com.sw.dualscreen.model.request.UserNutritionParam
import com.sw.dualscreen.model.response.DinnerTypeInfo
import com.sw.dualscreen.model.response.DinnerType
import com.sw.dualscreen.model.response.FoodInfo
import com.sw.dualscreen.model.response.UserFaceModel
import com.sw.dualscreen.model.response.UserNutritionData
import com.sw.dualscreen.objbox.CollectedFoodInfo
import com.sw.dualscreen.utils.SPUtil
import com.sw.plate.App
import com.sw.plate.utils.Base64
@@ -22,10 +21,15 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.RequestBody
import timber.log.Timber
import kotlin.math.atan
import java.io.File
class UserViewModel : BaseViewModel() {
companion object {
private const val TAG = "UserViewModel"
const val PAGE_SIZE = 100
}
private val faceApi: FaceApi = FaceApi()
@@ -44,8 +48,8 @@ class UserViewModel : BaseViewModel() {
/**
* 饭点类型 早餐/午餐/晚餐
*/
private val _dinnerTypeInfo = MutableStateFlow<DinnerTypeInfo?>(null)
val dinnerTypeInfo: StateFlow<DinnerTypeInfo?> = _dinnerTypeInfo
private val _dinnerTypeInfo = MutableStateFlow<DinnerType?>(null)
val dinnerTypeInfo: StateFlow<DinnerType?> = _dinnerTypeInfo
/**
* 人脸加载完成
@@ -59,45 +63,66 @@ class UserViewModel : BaseViewModel() {
* 获取token
*/
fun getEquipmentToken(qrcodeId: String = GlobalData.deviceId) {
Timber.d("getEquipmentToken")
launchWithLoading {
val response = repository.getEquipmentToken(qrcodeId)
if (parseResponse(response)) {
// 缓存token
SPUtil.getInstance().put(GlobalKey.KEY_TOKEN, response.result)
// 首次运行获取人脸数据
if (SPUtil.getInstance().get(GlobalKey.KEY_FIRST_RUN, false) != true) {
getUserFaceCache(index = 0)
}
}
}
// Timber.tag(TAG).d("getEquipmentToken")
// launchWithLoading {
// val response = repository.getEquipmentToken(qrcodeId)
// if (parseResponse(response)) {
// // 缓存token
// SPUtil.getInstance().put(GlobalKey.KEY_TOKEN, response.result)
// // 首次运行获取人脸数据
// if (SPUtil.getInstance().get(GlobalKey.KEY_FIRST_RUN, false) != true) {
// getUserFaceCache(pageNo = 1)
// }
// }
// }
}
/**
* 获取人脸数据
*/
fun getUserFaceCache(index: Int = 0) {
Timber.d("getUserFaceCache index = $index")
fun getUserFaceCache(pageNo: Int = 1, pageSize: Int = PAGE_SIZE) {
var currentPageNo = pageNo
Timber.tag(TAG).d("getUserFaceCache index = $currentPageNo")
launch {
_loadFaceResult.value = false
val response = repository.getUserFaceCache(index)
val response = repository.getUserFaceCache(currentPageNo)
if (parseResponse(response)) {
// 获取成功一次后缓存状态
SPUtil.getInstance().put(GlobalKey.KEY_FIRST_RUN, true)
withContext(Dispatchers.Default) {
val list: List<UserFaceModel> = response.result?.data ?: emptyList()
val list: List<UserFaceModel> = response.result ?: emptyList()
val item = list.firstOrNull { it.userId == "1951105919342936066" }
Timber.d("getUserFaceCache userId = ${item?.userId}")
Timber.tag(TAG).d("getUserFaceCache userId = ${item?.userId}")
val faceEntity = list.map {
FaceEntity(it.userId, null, Base64.decode(it.faceFeatureString))
FaceEntity(it.userId, null, Base64.decode(it.faceFeatureStr))
}
faceApi.updateFaceData(currentPageNo, faceEntity)
if (list.size >= pageSize) {
currentPageNo++
getUserFaceCache(currentPageNo)
} else {
_loadFaceResult.value = true
}
faceApi.updateFaceData(index, faceEntity)
}
val nextPageIndex = response.result?.nextPageIndex ?: -1
if (nextPageIndex > 0) {
getUserFaceCache(nextPageIndex)
} else {
_loadFaceResult.value = true
// val nextPageIndex = response.result?.nextPageIndex ?: -1
// if (nextPageIndex > 0) {
// getUserFaceCache(nextPageIndex)
// } else {
// _loadFaceResult.value = true
// }
}
}
}
fun getCollectedFoodList(pageNo: Int = 1, pageSize: Int = 100, block:(List<CollectedFoodInfo>)-> Unit) {
Timber.tag(TAG).d("getCollectedFoodList index = $pageNo")
launch {
val response = repository.getCollectedFoodList(pageNo)
if (parseResponse(response)) {
withContext(Dispatchers.Default) {
val list: List<CollectedFoodInfo> = response.result ?: emptyList()
block(list)
}
}
}
@@ -107,7 +132,7 @@ class UserViewModel : BaseViewModel() {
* 激活人脸识别引擎
*/
fun activeEngine() {
Timber.d("activeEngine")
Timber.tag(TAG).d("activeEngine")
faceApi.activeEngine(
App.getContext(),
@@ -116,7 +141,7 @@ class UserViewModel : BaseViewModel() {
GlobalData.activeKey,
object : FaceApi.ActiveCallback {
override fun onSuccess(activeCode: Int) {
Timber.d("activeEngine activeCode = $activeCode")
Timber.tag(TAG).d("activeEngine activeCode = $activeCode")
viewModelScope.launch(Dispatchers.Main) {
when (activeCode) {
ErrorInfo.MOK -> {
@@ -142,18 +167,18 @@ class UserViewModel : BaseViewModel() {
})
}
fun getIdentifiedFoodList() {
Timber.d("identifiedFoodList")
launchWithLoading {
val response = repository.getRestInfoFoodsByType(foodName = "")
if (parseResponse(response)) {
_identifiedFoodInfoList.value = response.result ?: emptyList()
}
}
}
// fun getIdentifiedFoodList() {
// Timber.tag(TAG).d("identifiedFoodList")
// launchWithLoading {
// val response = repository.getRestInfoFoodsByType(foodName = "")
// if (parseResponse(response)) {
// _identifiedFoodInfoList.value = response.result ?: emptyList()
// }
// }
// }
fun cleanIdentifiedFoodInfoList() {
Timber.d("cleanIdentifiedFoodInfoList")
Timber.tag(TAG).d("cleanIdentifiedFoodInfoList")
_identifiedFoodInfoList.value = emptyList<FoodInfo>()
}
@@ -161,19 +186,19 @@ class UserViewModel : BaseViewModel() {
* 搜索食物
*/
fun searchByFoodName(foodName: String, action: (List<FoodInfo>) -> Unit = {}) {
Timber.d("searchByFoodName foodName = $foodName")
Timber.tag(TAG).d("searchByFoodName foodName = $foodName")
launchWithLoading {
val response = repository.getRestInfoFoodsByType(foodName = foodName)
if (parseResponse(response)) {
val list = response.result ?: emptyList()
action(list)
_searchFoodInfoList.value = list
_identifiedFoodInfoList.value = response.result ?: emptyList()
}
}
}
fun cleanSearchFoodInfoList() {
Timber.d("cleanSearchFoodInfoList")
Timber.tag(TAG).d("cleanSearchFoodInfoList")
_searchFoodInfoList.value = emptyList<FoodInfo>()
}
@@ -181,7 +206,7 @@ class UserViewModel : BaseViewModel() {
* 获取用户就餐数据
*/
fun getUserNutritionData(userId: String, foodId: String) {
Timber.d("getUserNutritionData userId = ${userId}, foodId = $foodId")
Timber.tag(TAG).d("getUserNutritionData userId = ${userId}, foodId = $foodId")
_nutritionData.value = null
launch {
val response = repository.getUserNutritionData(userId = userId, foodId = foodId)
@@ -192,7 +217,7 @@ class UserViewModel : BaseViewModel() {
}
fun getDinnerType() {
Timber.d("getDinnerType")
Timber.tag(TAG).d("getDinnerType")
_dinnerTypeInfo.value = null
launch {
val response = repository.getDinnerType()
@@ -206,7 +231,7 @@ class UserViewModel : BaseViewModel() {
* 提交就餐数据
*/
fun postUserNutritionData(param: List<UserNutritionParam>) {
Timber.d("postUserNutritionData param = $param")
Timber.tag(TAG).d("postUserNutritionData param = $param")
launch {
val response = repository.postUserNutritionData(param)
if (parseResponse(response)) {
@@ -215,13 +240,13 @@ class UserViewModel : BaseViewModel() {
}
}
fun getFoodInfo(foodName: String, action:(List<FoodInfo>)-> Unit = {}) {
Timber.d("getFoodInfo")
fun getFoodInfo(foodName: String, action: (List<FoodInfo>) -> Unit = {}) {
Timber.tag(TAG).d("getFoodInfo")
launchWithLoading {
val response = repository.getFoodInfo(foodName = foodName)
if (parseResponse(response)) {
val list = response.result ?: emptyList()
_identifiedFoodInfoList2.value = list
_identifiedFoodInfoList.value = list
action(list)
} else {
action(emptyList())
@@ -229,22 +254,22 @@ class UserViewModel : BaseViewModel() {
}
}
fun postImageData(
context: Context,
foodId: String,
foodName: String,
foodVector: String,
uri: Uri
) {
Timber.d("postImageData")
fun uploadCollectFoodPics(files: List<File?>, params: HashMap<String, RequestBody>, callback: (Boolean) -> Unit) {
Timber.tag(TAG).d("uploadCollectFoodPics")
launchWithLoading {
val response = repository.postImageData(
context, foodId = foodId, foodName = foodName, foodVector = foodVector,
uri = uri
)
if (parseResponse(response)) {
Timber.tag("mzf").d("code=${response.code}")
val fileList = files.filterNotNull()
if (fileList.isEmpty()) {
callback(false)
return@launchWithLoading
}
val resp = repository.uploadCollectFoodPics(fileList, params)
if (!parseResponse(resp)) {
callback(false)
return@launchWithLoading
}
callback(resp.code == "00000")
}
}
}