feat(fragment): 上传失败时透传接口错误原因至 toast 提示

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-20 18:17:26 +08:00
co-authored by Claude Sonnet 4.6
parent 00bf478185
commit d580040c55
2 changed files with 8 additions and 8 deletions
@@ -366,7 +366,8 @@ class VectorCollectionFragment : BaseFragment<FragmentVectorCollectionBinding>()
// 直接 await 单批上传结果,成功返回 id 列表,失败返回 null 触发 onError
val result = currentActivity.netViewModel.uploadFoodVectorDataBatch(files, params)
log("uploadImage返回:result=${result.toJsonString()}")
if (result is UiState.Success) result.data else null
if (result is UiState.Success) result.data to null
else null to (result as? UiState.Error)?.msg
}, onProgress = { count, batch, idList ->
showWaitingDialog("图片上传中$count/$totalFileCount", timeoutMs = 60_000L)
val foodList = batch.mapIndexed { index, it ->
@@ -379,10 +380,10 @@ class VectorCollectionFragment : BaseFragment<FragmentVectorCollectionBinding>()
ObjectBox.putAll(foodList)
batch.forEach { it.isFinish = true }
vectorAdapter.notifyDataSetChanged()
}, onError = {
}, onError = { _, errorMsg ->
binding.root.postDelayed({
hideWaitingDialog()
toast("上传失败,请稍后重试")
toast("上传失败${(errorMsg ?: "").ifBlank { "请稍后重试" }}")
}, 1000)
}, onComplete = {
binding.root.postDelayed({
@@ -5,9 +5,9 @@ import java.util.concurrent.atomic.AtomicInteger
class ImageUploader(
private val totalList: List<FoodCollectionBean>,
private val uploadImage: suspend (List<FoodCollectionBean>) -> List<String>?,
private val uploadImage: suspend (List<FoodCollectionBean>) -> Pair<List<String>?, String?>,
private val onProgress: suspend (Int, List<FoodCollectionBean>, List<String>) -> Unit,
private val onError: (List<FoodCollectionBean>) -> Unit,
private val onError: (List<FoodCollectionBean>, String?) -> Unit,
private val onComplete: () -> Unit
) {
companion object {
@@ -19,10 +19,9 @@ class ImageUploader(
val batches = totalList.filter { it.imageVector!=null }.chunked(BATCH_SIZE)
for (batch in batches) {
val idList = uploadImage(batch)
val (idList, errorMsg) = uploadImage(batch)
if (idList.isNullOrEmpty()) {
//println("上传失败,终止流程")
onError(batch)
onError(batch, errorMsg)
return
}
val fileCount = batch.count { it.imageFile!=null }