3 Commits
15 changed files with 1061 additions and 279 deletions
+1
View File
@@ -130,6 +130,7 @@ APK 文件名会自动包含应用名、版本号、版本代码、构建类型
| 构建类型 | 后端环境 | 调试 | 代码压缩 |
| --- | --- | --- | --- |
| `debug` | 测试环境 `api-test.zhifly.cn` | 开启 | 关闭 |
| `releaseDebug` | 正式环境 `api.zhifly.cn` | 开启 | 关闭 |
| `release` | 正式环境 `api.zhifly.cn` | 关闭 | 关闭 |
请勿使用 Release 包连接测试设备随意操作,Release 会访问正式接口并连接正式 WebSocket。
+9 -2
View File
@@ -89,8 +89,8 @@ android {
applicationId = "com.yzx.kiosk"
minSdk = 26
targetSdk = 36
versionCode = 21
versionName = "1.1.1"
versionCode = 22
versionName = "1.1.2"
//multiDexEnabled = true
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
@@ -161,6 +161,13 @@ android {
"\"https://vipsky.oss-cn-shanghai.aliyuncs.com/cloud_driver\""
)
}
create("releaseDebug") {
initWith(getByName("release"))
isDebuggable = true
signingConfig = signingConfigs.getByName("debug")
matchingFallbacks += listOf("release", "debug")
}
}
compileOptions {
@@ -0,0 +1,8 @@
package com.yzx.kiosk.network.model.request
import com.google.gson.annotations.SerializedName
data class PaySuccessMessageRequest(
@SerializedName("order_number")
val orderNumber: String
)
@@ -4,6 +4,9 @@ import com.google.gson.annotations.SerializedName
data class GetPayUrlResponse(
@SerializedName("url")
val url: String?
val url: String?,
@SerializedName("order_number")
val orderNumber: String?
)
@@ -0,0 +1,31 @@
package com.yzx.kiosk.network.model.response
import com.google.gson.annotations.SerializedName
data class PaySuccessMessageResponse(
@SerializedName("order_status")
val orderStatus: Int?,
@SerializedName("order_status_name")
val orderStatusName: String?,
@SerializedName("sn")
val sn: String?,
@SerializedName("type")
val type: Int?,
@SerializedName("data")
val data: PaySuccessMessageData?
)
data class PaySuccessMessageData(
@SerializedName("order_number")
val orderNumber: String?,
@SerializedName("capture_type")
val captureType: Int?,
@SerializedName("image_id")
val imageIds: List<Int>?
)
@@ -1,6 +1,8 @@
package com.yzx.kiosk.network.repository
import com.google.gson.Gson
import com.yzx.kiosk.network.model.request.GetPayUrlRequest
import com.yzx.kiosk.network.model.request.PaySuccessMessageRequest
import com.yzx.kiosk.network.model.request.PrintCompleteRequest
import com.yzx.kiosk.network.model.request.PrintNotifyRequest
import com.yzx.kiosk.network.model.request.SaveAlbumUrlRequest
@@ -13,6 +15,7 @@ import com.yzx.kiosk.network.model.response.GetPayUrlResponse
import com.yzx.kiosk.network.model.response.GetPrintInfoResponse
import com.yzx.kiosk.network.model.response.NetworkResponse
import com.yzx.kiosk.network.model.response.OscarLivesData
import com.yzx.kiosk.network.model.response.PaySuccessMessageResponse
import com.yzx.kiosk.network.model.response.SocketTokenResponse
import com.yzx.kiosk.network.model.response.VerifyResultResponse
import com.yzx.kiosk.network.model.response.VersionResponse
@@ -27,6 +30,7 @@ import javax.inject.Inject
class NetWorkRepository @Inject constructor(
private val netService: NetworkService,
private val uploadService: UploadService,
private val gson: Gson,
) {
// 基础方法示例,可根据需要添加
// fun example(): Flow<NetworkResponse<Any>> = flow {
@@ -82,6 +86,23 @@ class NetWorkRepository @Inject constructor(
emit(netService.getPayUrl(request))
}.flowOn(Dispatchers.IO)
fun getPaySuccessMessage(
request: PaySuccessMessageRequest
): Flow<NetworkResponse<PaySuccessMessageResponse>> = flow {
val response = netService.getPaySuccessMessage(request)
val parsedData = response.data
?.takeIf { it.isJsonObject }
?.let { gson.fromJson(it, PaySuccessMessageResponse::class.java) }
emit(
NetworkResponse(
data = parsedData,
code = response.code,
message = response.message
)
)
}.flowOn(Dispatchers.IO)
fun getSocketToken(): Flow<NetworkResponse<SocketTokenResponse>> = flow {
emit(netService.getSocketToken())
}.flowOn(Dispatchers.IO)
@@ -1,6 +1,7 @@
package com.yzx.kiosk.network.service
import com.yzx.kiosk.network.model.request.GetPayUrlRequest
import com.yzx.kiosk.network.model.request.PaySuccessMessageRequest
import com.yzx.kiosk.network.model.request.PrintCompleteRequest
import com.yzx.kiosk.network.model.request.PrintNotifyRequest
import com.yzx.kiosk.network.model.request.SaveAlbumUrlRequest
@@ -16,6 +17,7 @@ import com.yzx.kiosk.network.model.response.OscarLivesData
import com.yzx.kiosk.network.model.response.SocketTokenResponse
import com.yzx.kiosk.network.model.response.VerifyResultResponse
import com.yzx.kiosk.network.model.response.VersionResponse
import com.google.gson.JsonElement
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
@@ -69,6 +71,11 @@ interface NetworkService {
@POST("/api/oscar/order/get-pay-url")
suspend fun getPayUrl(@Body request: GetPayUrlRequest): NetworkResponse<GetPayUrlResponse>
@POST("/api/oscar/order/pay-success-message")
suspend fun getPaySuccessMessage(
@Body request: PaySuccessMessageRequest
): NetworkResponse<JsonElement>
@GET("/api/oscar/socket-token")
suspend fun getSocketToken(): NetworkResponse<SocketTokenResponse>
@@ -0,0 +1,303 @@
package com.yzx.kiosk.priter
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.security.MessageDigest
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
/**
* 打印图片完整性校验。
*
* 网络图片必须先完整下载为字节数组,再经过容器、尺寸和软件 Bitmap 解码校验;
* 最终 BMP 必须通过文件头、尺寸、位深和精确文件长度校验后才允许送入打印 SDK。
*/
object PrintImageIntegrityValidator {
const val MAX_DOWNLOAD_BYTES: Long = 50L * 1024L * 1024L
private const val MIN_IMAGE_BYTES = 128
private const val MAX_IMAGE_DIMENSION = 30_000
private const val MAX_DECODED_PIXELS = 24_000_000L
private const val TARGET_LONG_EDGE = 1_840
private const val TARGET_SHORT_EDGE = 1_240
data class DecodedImage(
val bitmap: Bitmap,
val format: String,
val mimeType: String?,
val sourceWidth: Int,
val sourceHeight: Int,
val sampleSize: Int,
val sha256: String
)
data class BmpValidation(
val width: Int,
val height: Int,
val bitsPerPixel: Int,
val fileSize: Long,
val sha256: String
)
fun decodeDownloadedImage(bytes: ByteArray): DecodedImage {
if (bytes.size < MIN_IMAGE_BYTES) {
throw IOException("图片数据过小: ${bytes.size} bytes")
}
if (bytes.size.toLong() > MAX_DOWNLOAD_BYTES) {
throw IOException("图片数据超过上限: ${bytes.size} bytes")
}
val format = detectAndValidateContainer(bytes)
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeByteArray(bytes, 0, bytes.size, bounds)
val sourceWidth = bounds.outWidth
val sourceHeight = bounds.outHeight
if (sourceWidth <= 0 || sourceHeight <= 0) {
throw IOException("无法解析图片尺寸,format=$format, mime=${bounds.outMimeType}")
}
if (sourceWidth > MAX_IMAGE_DIMENSION || sourceHeight > MAX_IMAGE_DIMENSION) {
throw IOException("图片尺寸异常: ${sourceWidth}x${sourceHeight}")
}
val sampleSize = calculateInSampleSize(sourceWidth, sourceHeight)
val decodeOptions = BitmapFactory.Options().apply {
inJustDecodeBounds = false
inPreferredConfig = Bitmap.Config.ARGB_8888
inSampleSize = sampleSize
inScaled = false
}
val decoded = BitmapFactory.decodeByteArray(bytes, 0, bytes.size, decodeOptions)
?: throw IOException("图片完整解码失败: ${sourceWidth}x${sourceHeight}, format=$format")
if (decoded.width <= 0 || decoded.height <= 0 || decoded.isRecycled) {
decoded.recycle()
throw IOException("解码后的 Bitmap 无效")
}
if (decoded.config == Bitmap.Config.HARDWARE) {
decoded.recycle()
throw IOException("打印图片意外解码为硬件 Bitmap")
}
// 主动读取像素,确保 Bitmap 的像素缓冲区可访问。
try {
decoded.getPixel(decoded.width / 2, decoded.height / 2)
} catch (e: Exception) {
decoded.recycle()
throw IOException("Bitmap 像素缓冲区不可读", e)
}
return DecodedImage(
bitmap = decoded,
format = format,
mimeType = bounds.outMimeType,
sourceWidth = sourceWidth,
sourceHeight = sourceHeight,
sampleSize = sampleSize,
sha256 = sha256(bytes)
)
}
fun validateBmp(file: File, expectedWidth: Int, expectedHeight: Int): BmpValidation {
if (!file.isFile || file.length() < 54L) {
throw IOException("BMP 文件不存在或过小: ${file.absolutePath}")
}
val header = ByteArray(54)
FileInputStream(file).use { input ->
var offset = 0
while (offset < header.size) {
val read = input.read(header, offset, header.size - offset)
if (read < 0) break
offset += read
}
if (offset != header.size) {
throw IOException("BMP 文件头不完整: $offset/${header.size}")
}
}
if (header[0] != 'B'.code.toByte() || header[1] != 'M'.code.toByte()) {
throw IOException("BMP 文件签名错误")
}
val declaredFileSize = uint32Le(header, 2)
val pixelOffset = uint32Le(header, 10)
val dibHeaderSize = uint32Le(header, 14)
val width = int32Le(header, 18)
val rawHeight = int32Le(header, 22)
val height = abs(rawHeight)
val planes = uint16Le(header, 26)
val bitsPerPixel = uint16Le(header, 28)
val compression = uint32Le(header, 30)
if (dibHeaderSize < 40L || pixelOffset < 54L) {
throw IOException("BMP 文件头结构异常")
}
if (width != expectedWidth || height != expectedHeight) {
throw IOException("BMP 尺寸不匹配: ${width}x${height}, expected=${expectedWidth}x${expectedHeight}")
}
if (planes != 1 || bitsPerPixel != 24 || compression != 0L) {
throw IOException("BMP 格式不受支持: planes=$planes, bpp=$bitsPerPixel, compression=$compression")
}
val rowStride = ((width.toLong() * 3L + 3L) / 4L) * 4L
val expectedFileSize = pixelOffset + rowStride * height.toLong()
val actualFileSize = file.length()
if (declaredFileSize != actualFileSize || expectedFileSize != actualFileSize) {
throw IOException(
"BMP 文件长度异常: declared=$declaredFileSize, expected=$expectedFileSize, actual=$actualFileSize"
)
}
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeFile(file.absolutePath, bounds)
if (bounds.outWidth != expectedWidth || abs(bounds.outHeight) != expectedHeight) {
throw IOException("BMP 无法重新解析: ${bounds.outWidth}x${bounds.outHeight}")
}
return BmpValidation(
width = width,
height = height,
bitsPerPixel = bitsPerPixel,
fileSize = actualFileSize,
sha256 = sha256(file)
)
}
private fun calculateInSampleSize(width: Int, height: Int): Int {
val longEdge = max(width, height)
val shortEdge = min(width, height)
var sampleSize = 1
while (
longEdge / (sampleSize * 2) >= TARGET_LONG_EDGE &&
shortEdge / (sampleSize * 2) >= TARGET_SHORT_EDGE
) {
sampleSize *= 2
}
while (
width.toLong() / sampleSize * (height.toLong() / sampleSize) > MAX_DECODED_PIXELS
) {
sampleSize *= 2
}
return sampleSize
}
private fun detectAndValidateContainer(bytes: ByteArray): String {
return when {
isJpeg(bytes) -> {
val searchStart = max(2, bytes.size - 64)
var hasEndMarker = false
for (index in bytes.size - 2 downTo searchStart) {
if (byteAt(bytes, index) == 0xFF && byteAt(bytes, index + 1) == 0xD9) {
hasEndMarker = true
break
}
}
if (!hasEndMarker) throw IOException("JPEG 缺少结束标记,文件可能被截断")
"JPEG"
}
isPng(bytes) -> {
if (
bytes.size < 20 ||
bytes[bytes.size - 8] != 'I'.code.toByte() ||
bytes[bytes.size - 7] != 'E'.code.toByte() ||
bytes[bytes.size - 6] != 'N'.code.toByte() ||
bytes[bytes.size - 5] != 'D'.code.toByte()
) {
throw IOException("PNG 缺少 IEND,文件可能被截断")
}
"PNG"
}
isWebp(bytes) -> {
val declaredSize = uint32Le(bytes, 4) + 8L
if (declaredSize != bytes.size.toLong()) {
throw IOException("WebP 文件长度异常: declared=$declaredSize, actual=${bytes.size}")
}
"WEBP"
}
isGif(bytes) -> {
if (byteAt(bytes, bytes.lastIndex) != 0x3B) {
throw IOException("GIF 缺少结束标记,文件可能被截断")
}
"GIF"
}
isBmp(bytes) -> {
val declaredSize = uint32Le(bytes, 2)
if (declaredSize != bytes.size.toLong()) {
throw IOException("BMP 文件长度异常: declared=$declaredSize, actual=${bytes.size}")
}
"BMP"
}
isIsoBaseMedia(bytes) -> "HEIF/AVIF"
else -> throw IOException("不支持或无法识别的图片格式")
}
}
private fun isJpeg(bytes: ByteArray): Boolean =
bytes.size >= 4 && byteAt(bytes, 0) == 0xFF && byteAt(bytes, 1) == 0xD8
private fun isPng(bytes: ByteArray): Boolean =
bytes.size >= 8 &&
byteAt(bytes, 0) == 0x89 && bytes[1] == 'P'.code.toByte() &&
bytes[2] == 'N'.code.toByte() && bytes[3] == 'G'.code.toByte() &&
byteAt(bytes, 4) == 0x0D && byteAt(bytes, 5) == 0x0A &&
byteAt(bytes, 6) == 0x1A && byteAt(bytes, 7) == 0x0A
private fun isWebp(bytes: ByteArray): Boolean =
bytes.size >= 12 && ascii(bytes, 0, 4) == "RIFF" && ascii(bytes, 8, 4) == "WEBP"
private fun isGif(bytes: ByteArray): Boolean =
bytes.size >= 6 && (ascii(bytes, 0, 6) == "GIF87a" || ascii(bytes, 0, 6) == "GIF89a")
private fun isBmp(bytes: ByteArray): Boolean =
bytes.size >= 14 && bytes[0] == 'B'.code.toByte() && bytes[1] == 'M'.code.toByte()
private fun isIsoBaseMedia(bytes: ByteArray): Boolean =
bytes.size >= 12 && ascii(bytes, 4, 4) == "ftyp"
private fun ascii(bytes: ByteArray, offset: Int, length: Int): String =
bytes.copyOfRange(offset, offset + length).toString(Charsets.US_ASCII)
private fun byteAt(bytes: ByteArray, index: Int): Int = bytes[index].toInt() and 0xFF
private fun uint16Le(bytes: ByteArray, offset: Int): Int =
byteAt(bytes, offset) or (byteAt(bytes, offset + 1) shl 8)
private fun int32Le(bytes: ByteArray, offset: Int): Int =
byteAt(bytes, offset) or
(byteAt(bytes, offset + 1) shl 8) or
(byteAt(bytes, offset + 2) shl 16) or
(byteAt(bytes, offset + 3) shl 24)
private fun uint32Le(bytes: ByteArray, offset: Int): Long =
int32Le(bytes, offset).toLong() and 0xFFFF_FFFFL
private fun sha256(bytes: ByteArray): String =
MessageDigest.getInstance("SHA-256").digest(bytes).toHex()
private fun sha256(file: File): String {
val digest = MessageDigest.getInstance("SHA-256")
FileInputStream(file).use { input ->
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
while (true) {
val count = input.read(buffer)
if (count < 0) break
if (count > 0) digest.update(buffer, 0, count)
}
}
return digest.digest().toHex()
}
private fun ByteArray.toHex(): String = joinToString(separator = "") { byte -> "%02x".format(byte) }
}
@@ -5,7 +5,6 @@ import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Paint
import com.yzx.kiosk.App
import com.yzx.kiosk.datastore.AppStoreDataSource
import com.yzx.kiosk.utils.LogUtils
@@ -23,8 +22,11 @@ import jp.co.dnpLib.print.PrintManager
import jp.co.dnpLib.print.PrintQueue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import java.io.File
import java.io.IOException
import javax.inject.Singleton
import kotlin.math.min
@@ -43,6 +45,7 @@ class PrinterService(
}
private var mDnpPhotoPrint: DNPPhotoPrint? = null
private val printMutex = Mutex()
/**
* 初始化打印机(耗时操作,需在IO线程调用)
@@ -182,12 +185,29 @@ class PrinterService(
* @param originalBitmap 要打印的原始图片
* @param callback 打印结果回调
*/
suspend fun startPrint(originalBitmap: Bitmap?, callback: PrintCallback) = withContext(Dispatchers.IO) {
suspend fun startPrint(originalBitmap: Bitmap?, callback: PrintCallback) = printMutex.withLock {
startPrintInternal(originalBitmap, callback)
}
private suspend fun startPrintInternal(
originalBitmap: Bitmap?,
callback: PrintCallback
) = withContext(Dispatchers.IO) {
if (originalBitmap == null) {
LogUtils.e(TAG, "获取图片失败")
callback.onPrintResult(false,"获取图片失败")
return@withContext
}
if (
originalBitmap.isRecycled ||
originalBitmap.width <= 0 ||
originalBitmap.height <= 0 ||
originalBitmap.config == Bitmap.Config.HARDWARE
) {
LogUtils.e(TAG, "图片 Bitmap 无效或为硬件 Bitmap,禁止打印")
callback.onPrintResult(false, "图片数据无效")
return@withContext
}
LogUtils.e(TAG, "获取图片成功")
// 在开始打印前,检查打印机是否在线
@@ -224,17 +244,22 @@ class PrinterService(
LogUtils.d(TAG, "打印尺寸: ${printSize.width} x ${printSize.height}")
// 准备打印图片(保持原比例,居中,白色背景)
val outputBitmap: Bitmap
var outputBitmap: Bitmap? = null
val outFile = File(
context.getExternalFilesDir(null),
"ProcessedPhotos/${System.currentTimeMillis()}_print.bmp"
)
outFile.parentFile?.mkdirs()
val outputDirectory = outFile.parentFile
if (outputDirectory == null || (!outputDirectory.exists() && !outputDirectory.mkdirs())) {
printStatusManager.setIdle()
callback.onPrintResult(false, "无法创建打印文件目录")
return@withContext
}
try {
outputBitmap = preparePrintBitmap(originalBitmap, printSize, printId)
if (printId != PrintManager.EPrinter.QW410_DEF.id) {
val saved = if (printId != PrintManager.EPrinter.QW410_DEF.id) {
BmpUtil.save(outputBitmap, outFile.absolutePath, EResolution.RESO300.mValue)
} else {
AndroidBmpUtil.save(
@@ -244,12 +269,35 @@ class PrinterService(
PRINTSIZE.QW410.height
)
}
if (!saved) {
throw IOException("BMP 保存接口返回失败")
}
val validation = PrintImageIntegrityValidator.validateBmp(
file = outFile,
expectedWidth = printSize.width,
expectedHeight = printSize.height
)
LogUtils.d(
TAG,
"打印 BMP 校验成功 - path=${outFile.absolutePath}, " +
"size=${validation.width}x${validation.height}, " +
"bpp=${validation.bitsPerPixel}, bytes=${validation.fileSize}, " +
"sha256=${validation.sha256.take(16)}"
)
} catch (e: Exception) {
e.printStackTrace()
callback.onPrintResult(false,"图片处理失败: ${e.message}")
LogUtils.e(TAG, "图片处理或 BMP 完整性校验失败: ${e.message}")
outputBitmap?.let { bitmap ->
if (!bitmap.isRecycled) bitmap.recycle()
}
printStatusManager.setIdle()
callback.onPrintResult(false, "图片处理失败: ${e.message}")
return@withContext
}
val validatedOutputBitmap = checkNotNull(outputBitmap)
// 创建打印任务
val job = PrintJob(
outFile.absolutePath,
@@ -269,8 +317,8 @@ class PrinterService(
printStatusManager.setIdle()
// 回收 outputBitmap
try {
if (!outputBitmap.isRecycled) {
outputBitmap.recycle()
if (!validatedOutputBitmap.isRecycled) {
validatedOutputBitmap.recycle()
LogUtils.d(TAG, "outputBitmap 已回收")
}
} catch (ex: Exception) {
@@ -285,8 +333,8 @@ class PrinterService(
printStatusManager.setIdle()
// 回收 outputBitmap
try {
if (!outputBitmap.isRecycled) {
outputBitmap.recycle()
if (!validatedOutputBitmap.isRecycled) {
validatedOutputBitmap.recycle()
LogUtils.d(TAG, "outputBitmap 已回收")
}
} catch (ex: Exception) {
@@ -1,5 +1,6 @@
package com.yzx.kiosk.ui.face.view
import android.graphics.Bitmap
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
@@ -24,6 +25,7 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
@@ -35,6 +37,7 @@ import coil.compose.AsyncImage
import coil.compose.SubcomposeAsyncImage
import coil.compose.SubcomposeAsyncImageContent
import coil.compose.SubcomposeAsyncImageScope
import coil.request.ImageRequest
import com.yzx.kiosk.R
import com.yzx.kiosk.component.appbar.AppTitleBar
import com.yzx.kiosk.component.appbar.FaceBarNoStatusBarPadding
@@ -46,7 +49,7 @@ import com.yzx.kiosk.ui.face.viewmodel.FaceRecognitionResultViewModel
import com.yzx.kiosk.ui.upload.view.PhotoPreviewDialog
import com.yzx.kiosk.ui.upload.view.RetryableAsyncImage
import com.yzx.kiosk.utils.QrCodeUtils
import kotlinx.coroutines.launch
import kotlinx.coroutines.flow.collect
@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
@Composable
@@ -65,6 +68,7 @@ fun FaceRecognitionResultScreen(
val selectedPhotos by viewModel.selectedPhotos.collectAsState()
val pricePerPhoto by viewModel.pricePerPhoto.collectAsState()
val totalPrice by viewModel.totalPrice.collectAsState()
val imageLoadStates by viewModel.imageLoadStates.collectAsState()
val selectedCount = selectedPhotos.size
val isAllSelected = photoList.isNotEmpty() && selectedPhotos.size == photoList.size
@@ -77,6 +81,12 @@ fun FaceRecognitionResultScreen(
var showPayDialog by remember { mutableStateOf(false) }
val payQrCodeUrl by viewModel.payQrCodeUrl.collectAsState()
LaunchedEffect(viewModel) {
viewModel.dismissPayDialogEvents.collect {
showPayDialog = false
}
}
FullScreenMode()
AppScaffold(
@@ -224,12 +234,15 @@ fun FaceRecognitionResultScreen(
horizontalArrangement = Arrangement.spacedBy(18.dp),
verticalItemSpacing = 18.dp
) {
itemsIndexed(photoList) { index, photo ->
itemsIndexed(
items = photoList,
key = { _, photo -> photo.id }
) { _, photo ->
val isSelected = selectedPhotos.contains(photo.url)
val imageLoadStates by viewModel.imageLoadStates.collectAsState()
val loadState = imageLoadStates[photo.url] ?: FaceRecognitionResultViewModel.ImageLoadState()
FaceRecognitionPhotoItem(
photoId = photo.id,
photoUrl = photo.url,
aspectRatio = photo.aspectRatio,
isSelected = isSelected,
@@ -246,6 +259,9 @@ fun FaceRecognitionResultScreen(
},
onLoadError = { viewModel.handleImageLoadError(photo.url) },
onLoadSuccess = { viewModel.handleImageLoadSuccess(photo.url) },
onImageDimensionsResolved = { width, height ->
viewModel.updatePhotoAspectRatio(photo.id, width, height)
},
onRetryClick = { viewModel.retryLoadImage(photo.url) }
)
}
@@ -265,15 +281,19 @@ fun FaceRecognitionResultScreen(
// 支付弹框
if (showPayDialog && selectedCount > 0 && payQrCodeUrl != null) {
val selectedUrls = photoList.filter { selectedPhotos.contains(it.url) }.map { it.url }
DisposableEffect(payQrCodeUrl) {
viewModel.startPayStatusPolling()
onDispose {
viewModel.stopPayStatusPolling()
}
}
WechatPayDialog(
qrCodeUrl = payQrCodeUrl!!,
totalPrice = totalPrice,
onDismiss = { showPayDialog = false },
onPaySuccess = {
onDismiss = {
showPayDialog = false
// 支付成功,等待 WebSocket 消息(code=5)触发跳转到支付成功页面
// 跳转逻辑由 FaceRecognitionResultViewModel 中的 WebSocket 监听处理
viewModel.onPayDialogDismissed()
}
)
}
@@ -281,6 +301,7 @@ fun FaceRecognitionResultScreen(
@Composable
fun FaceRecognitionPhotoItem(
photoId: Int,
photoUrl: String,
aspectRatio: Float,
isSelected: Boolean,
@@ -289,6 +310,7 @@ fun FaceRecognitionPhotoItem(
onPreviewClick: () -> Unit,
onLoadError: () -> Unit = {},
onLoadSuccess: () -> Unit = {},
onImageDimensionsResolved: (width: Int, height: Int) -> Unit = { _, _ -> },
onRetryClick: () -> Unit = {}
) {
// 确保 aspectRatio 是有效值
@@ -306,11 +328,20 @@ fun FaceRecognitionPhotoItem(
.clickable { onToggleSelection() }
) {
// 图片 - 按原比例显示
var lastErrorState by remember { mutableStateOf(false) }
var lastSuccessState by remember { mutableStateOf(false) }
var lastErrorState by remember(photoId, photoUrl) { mutableStateOf(false) }
var lastSuccessState by remember(photoId, photoUrl) { mutableStateOf(false) }
val context = LocalContext.current
val imageRequest = remember(context, photoUrl) {
ImageRequest.Builder(context)
.data(photoUrl)
.allowHardware(false)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.crossfade(false)
.build()
}
SubcomposeAsyncImage(
model = photoUrl,
model = imageRequest,
contentDescription = null,
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Fit
@@ -390,6 +421,11 @@ fun FaceRecognitionPhotoItem(
if (state is coil.compose.AsyncImagePainter.State.Success && !lastSuccessState) {
lastSuccessState = true
lastErrorState = false
val drawable = state.result.drawable
onImageDimensionsResolved(
drawable.intrinsicWidth,
drawable.intrinsicHeight
)
onLoadSuccess()
}
}
@@ -453,8 +489,7 @@ fun FaceRecognitionPhotoItem(
fun WechatPayDialog(
qrCodeUrl: String,
totalPrice: String,
onDismiss: () -> Unit,
onPaySuccess: () -> Unit
onDismiss: () -> Unit
) {
// 使用接口返回的URL生成支付二维码
val qrCodeBitmap = remember(qrCodeUrl) {
@@ -0,0 +1,26 @@
package com.yzx.kiosk.ui.face.viewmodel
import com.yzx.kiosk.network.model.response.PaySuccessMessageResponse
internal enum class FacePayStatusDecision {
CONTINUE_POLLING,
COMPLETE_PAYMENT,
STOP_WITH_TERMINAL_STATUS
}
internal fun decideFacePayStatus(orderStatus: Int?): FacePayStatusDecision = when (orderStatus) {
30 -> FacePayStatusDecision.COMPLETE_PAYMENT
40, 50, 60 -> FacePayStatusDecision.STOP_WITH_TERMINAL_STATUS
else -> FacePayStatusDecision.CONTINUE_POLLING
}
internal fun isValidFacePaySuccessMessage(
expectedOrderNumber: String,
message: PaySuccessMessageResponse?
): Boolean {
val paymentData = message?.data ?: return false
return message.type == 5 &&
paymentData.orderNumber == expectedOrderNumber &&
paymentData.captureType == 2 &&
!paymentData.imageIds.isNullOrEmpty()
}
@@ -1,16 +1,11 @@
package com.yzx.kiosk.ui.face.viewmodel
import android.graphics.ImageDecoder
import android.os.Build
import androidx.lifecycle.viewModelScope
import coil.ImageLoader
import coil.request.ImageRequest
import coil.size.Size
import com.yzx.kiosk.App
import com.yzx.kiosk.base.BaseViewModel
import com.yzx.kiosk.datastore.AppState
import com.yzx.kiosk.navigation.AppNavigator
import com.yzx.kiosk.network.model.request.GetPayUrlRequest
import com.yzx.kiosk.network.model.request.PaySuccessMessageRequest
import com.yzx.kiosk.network.model.request.VerifyResultRequest
import com.yzx.kiosk.network.model.response.FaceSearchResult
import com.yzx.kiosk.network.repository.NetWorkRepository
@@ -25,15 +20,21 @@ import com.yzx.kiosk.datastore.AppStoreDataSource
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withTimeoutOrNull
import com.yzx.kiosk.utils.LogUtils
import java.util.concurrent.atomic.AtomicBoolean
import javax.inject.Inject
@HiltViewModel
@@ -73,12 +74,22 @@ class FaceRecognitionResultViewModel @Inject constructor(
private val _payQrCodeUrl = MutableStateFlow<String?>(null)
val payQrCodeUrl: StateFlow<String?> = _payQrCodeUrl.asStateFlow()
// 支付弹框关闭事件(支付完成、取消或退款时由 ViewModel 驱动关闭)
private val _dismissPayDialogEvents = MutableSharedFlow<Unit>(extraBufferCapacity = 1)
val dismissPayDialogEvents: SharedFlow<Unit> = _dismissPayDialogEvents.asSharedFlow()
private var activePaymentOrderNumber: String? = null
private var paymentPollingJob: Job? = null
private val paymentHandled = AtomicBoolean(false)
// 图片加载状态:URL -> (重试次数, 是否加载失败)
private val _imageLoadStates = MutableStateFlow<Map<String, ImageLoadState>>(emptyMap())
val imageLoadStates: StateFlow<Map<String, ImageLoadState>> = _imageLoadStates.asStateFlow()
companion object {
private const val TAG = "FaceRecognitionResultViewModel"
private const val MAX_RETRY_COUNT = 3
private const val PAY_STATUS_POLL_INTERVAL_MS = 2_000L
}
data class ImageLoadState(
@@ -93,9 +104,13 @@ class FaceRecognitionResultViewModel @Inject constructor(
.onEach { event ->
when (event) {
is UploadPhotoEvent.PaySuccess -> {
// 支付成功,跳转到支付成功页面
LogUtils.d("FaceRecognitionResultViewModel", "收到支付成功事件 - order_number: ${event.orderNumber}, capture_type: ${event.captureType}, image_ids: ${event.imageIds}")
navigateToPaySuccess(event.orderNumber, event.captureType, event.imageIds)
LogUtils.d(TAG, "收到 WebSocket 支付成功事件 - order_number: ${event.orderNumber}, capture_type: ${event.captureType}, image_ids: ${event.imageIds}")
handlePaymentSuccess(
orderNumber = event.orderNumber,
captureType = event.captureType,
imageIds = event.imageIds,
source = "WebSocket"
)
}
else -> {
// 其他事件不处理
@@ -112,6 +127,7 @@ class FaceRecognitionResultViewModel @Inject constructor(
viewModelScope.launch {
// 保存原始结果列表
originalResults = results
urlToIdMap.clear()
// 先使用默认宽高比创建列表
val initialPhotos = results.mapNotNull { result ->
@@ -145,70 +161,35 @@ class FaceRecognitionResultViewModel @Inject constructor(
// 页面初始化时调用验证接口(空列表,type: 2),获取默认价格
verifyResult(emptyList())
// 并发获取所有图片的真实尺寸并更新宽高比
val updatedPhotos = initialPhotos.mapIndexed { index, photo ->
async(Dispatchers.IO) {
try {
val aspectRatio = getImageAspectRatio(photo.url)
// 确保 aspectRatio 是有效值
val validAspectRatio = if (aspectRatio > 0 && aspectRatio.isFinite()) {
aspectRatio
} else {
LogUtils.i("FaceRecognitionResultViewModel", "图片 ${photo.url} 的宽高比无效: $aspectRatio,使用默认值")
1f
}
photo.copy(aspectRatio = validAspectRatio)
} catch (e: Exception) {
LogUtils.e("FaceRecognitionResultViewModel", "获取图片 ${photo.url} 尺寸失败: ${e.message}")
// 获取失败,保持默认值
photo
}
}
}.awaitAll()
_photoList.value = updatedPhotos
LogUtils.d("FaceRecognitionResultViewModel", "图片列表初始化完成,共 ${updatedPhotos.size} 张")
LogUtils.d(TAG, "图片列表初始化完成,共 ${initialPhotos.size} 张,宽高比将在图片加载成功后逐张更新")
}
}
/**
* 获取图片的宽高比
* 使用网格中已经加载成功的 Drawable 尺寸更新宽高比。
* 这样不会为了获取尺寸额外下载、解码一次图片。
*/
private suspend fun getImageAspectRatio(imageUrl: String): Float {
return withTimeoutOrNull(10000) { // 10秒超时
try {
val imageLoader = ImageLoader(App.instance)
val request = ImageRequest.Builder(App.instance)
.data(imageUrl)
.size(Size.ORIGINAL)
.allowHardware(false) // 禁用硬件加速,避免某些图片无法获取尺寸
.build()
fun updatePhotoAspectRatio(photoId: Int, width: Int, height: Int) {
if (width <= 0 || height <= 0) {
LogUtils.i(TAG, "忽略无效图片尺寸 - id: $photoId, size: ${width}x${height}")
return
}
val result = imageLoader.execute(request)
val drawable = result.drawable
val aspectRatio = width.toFloat() / height.toFloat()
if (!aspectRatio.isFinite() || aspectRatio !in 0.05f..20f) {
LogUtils.i(TAG, "忽略异常图片宽高比 - id: $photoId, ratio: $aspectRatio")
return
}
if (drawable != null) {
val width = drawable.intrinsicWidth
val height = drawable.intrinsicHeight
if (width > 0 && height > 0) {
val aspectRatio = width.toFloat() / height.toFloat()
LogUtils.d("FaceRecognitionResultViewModel", "图片 $imageUrl 尺寸: ${width}x${height}, 宽高比: $aspectRatio")
return@withTimeoutOrNull aspectRatio
} else {
LogUtils.i("FaceRecognitionResultViewModel", "图片 $imageUrl 尺寸无效: ${width}x${height}")
}
_photoList.update { photos ->
photos.map { photo ->
if (photo.id == photoId && kotlin.math.abs(photo.aspectRatio - aspectRatio) > 0.01f) {
LogUtils.d(TAG, "更新图片比例 - id: $photoId, size: ${width}x${height}, ratio: $aspectRatio")
photo.copy(aspectRatio = aspectRatio)
} else {
LogUtils.i("FaceRecognitionResultViewModel", "图片 $imageUrl 加载失败,drawable为null")
photo
}
} catch (e: Exception) {
LogUtils.e("FaceRecognitionResultViewModel"+"获取图片 $imageUrl 尺寸异常: ${e.message}")
}
null
} ?: run {
LogUtils.i("FaceRecognitionResultViewModel", "获取图片 $imageUrl 尺寸超时")
1f // 超时或失败,返回默认值
}
}
@@ -330,10 +311,16 @@ class FaceRecognitionResultViewModel @Inject constructor(
flow = netWorkRepository.getPayUrl(request).asResult(),
showToast = false,
onData = { response ->
val url = response.url
if (url.isNullOrEmpty()) {
val url = response.url?.trim()
val orderNumber = response.orderNumber?.trim()
if (url.isNullOrEmpty() || orderNumber.isNullOrEmpty()) {
_payQrCodeUrl.value = null
LogUtils.e(TAG, "获取支付二维码成功,但 url 或 order_number 为空")
ToastUtils.show("获取支付二维码失败")
} else {
stopPayStatusPolling()
activePaymentOrderNumber = orderNumber
paymentHandled.set(false)
_payQrCodeUrl.value = url
onSuccess(url)
}
@@ -353,6 +340,149 @@ class FaceRecognitionResultViewModel @Inject constructor(
}
}
/**
* 二维码弹框显示时启动支付状态轮询。重复调用不会创建多个轮询任务。
*/
fun startPayStatusPolling() {
val orderNumber = activePaymentOrderNumber
if (orderNumber.isNullOrEmpty() || paymentHandled.get()) {
LogUtils.i(TAG, "未启动支付状态轮询:当前没有有效待支付订单")
return
}
if (paymentPollingJob?.isActive == true) {
return
}
paymentPollingJob = viewModelScope.launch {
LogUtils.d(TAG, "开始轮询支付状态 - order_number: $orderNumber")
while (
isActive &&
!paymentHandled.get() &&
activePaymentOrderNumber == orderNumber
) {
queryPayStatus(orderNumber)
delay(PAY_STATUS_POLL_INTERVAL_MS)
}
}
}
/** 二维码弹框关闭时停止 HTTP 轮询,WebSocket 监听仍保持有效。 */
fun stopPayStatusPolling() {
paymentPollingJob?.cancel()
paymentPollingJob = null
}
fun onPayDialogDismissed() {
stopPayStatusPolling()
}
private suspend fun queryPayStatus(orderNumber: String) {
try {
val response = netWorkRepository
.getPaySuccessMessage(PaySuccessMessageRequest(orderNumber))
.first()
if (!response.isSucceeded) {
LogUtils.e(
TAG,
"查询支付状态业务失败 - order_number: $orderNumber, code: ${response.code}, msg: ${response.message}"
)
return
}
val message = response.data
when (decideFacePayStatus(message?.orderStatus)) {
FacePayStatusDecision.CONTINUE_POLLING -> {
LogUtils.d(
TAG,
"订单尚未完成,继续轮询 - order_number: $orderNumber, status: ${message?.orderStatus}, name: ${message?.orderStatusName}"
)
}
FacePayStatusDecision.COMPLETE_PAYMENT -> {
if (!isValidFacePaySuccessMessage(orderNumber, message)) {
LogUtils.e(TAG, "支付完成消息校验失败,等待下一次查询或 WebSocket - order_number: $orderNumber, message: $message")
return
}
val paymentData = checkNotNull(message?.data)
handlePaymentSuccess(
orderNumber = paymentData.orderNumber,
captureType = paymentData.captureType,
imageIds = paymentData.imageIds.orEmpty(),
source = "HTTP"
)
}
FacePayStatusDecision.STOP_WITH_TERMINAL_STATUS -> {
handleTerminalPayStatus(message?.orderStatus, message?.orderStatusName)
}
}
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
LogUtils.e(TAG, "查询支付状态异常 - order_number: $orderNumber, error: ${e.message}")
}
}
private fun handleTerminalPayStatus(orderStatus: Int?, orderStatusName: String?) {
if (!paymentHandled.compareAndSet(false, true)) {
return
}
stopPayStatusPolling()
activePaymentOrderNumber = null
_payQrCodeUrl.value = null
_dismissPayDialogEvents.tryEmit(Unit)
val statusText = orderStatusName?.takeIf { it.isNotBlank() } ?: when (orderStatus) {
40 -> "已取消"
50 -> "已退款"
60 -> "部分退款"
else -> "状态异常"
}
ToastUtils.show("订单$statusText")
LogUtils.i(TAG, "订单进入终态,停止支付轮询 - status: $orderStatus, name: $statusText")
}
private fun handlePaymentSuccess(
orderNumber: String?,
captureType: Int?,
imageIds: List<Int>,
source: String
) {
val expectedOrderNumber = activePaymentOrderNumber
if (
expectedOrderNumber.isNullOrEmpty() ||
orderNumber != expectedOrderNumber ||
captureType != 2 ||
imageIds.isEmpty()
) {
LogUtils.i(
TAG,
"忽略不匹配的支付成功消息 - source: $source, expected: $expectedOrderNumber, actual: $orderNumber, capture_type: $captureType, image_ids: $imageIds"
)
return
}
val knownImageIds = originalResults.map { it.id }.toSet()
if (!knownImageIds.containsAll(imageIds)) {
LogUtils.e(TAG, "支付成功消息包含未知图片,暂不跳转 - source: $source, image_ids: $imageIds")
return
}
if (!paymentHandled.compareAndSet(false, true)) {
LogUtils.d(TAG, "支付成功已处理,忽略重复消息 - source: $source, order_number: $orderNumber")
return
}
LogUtils.d(TAG, "确认支付成功 - source: $source, order_number: $orderNumber")
stopPayStatusPolling()
_payQrCodeUrl.value = null
_dismissPayDialogEvents.tryEmit(Unit)
navigateToPaySuccess(orderNumber, captureType, imageIds)
}
/**
* 跳转到支付成功页面
* @param orderNumber 订单号
@@ -26,6 +26,7 @@ import com.yzx.kiosk.utils.LogUtils
import com.yzx.kiosk.utils.ToastUtils
import java.net.URLEncoder
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
@@ -220,6 +221,9 @@ class FaceRecognitionViewModel @Inject constructor(
try {
// 直接调用人脸识别接口
searchFace(photoFile)
} catch (e: CancellationException) {
// 页面跳转或 ViewModel 销毁时的正常协程取消,不向用户报错
throw e
} catch (e: Exception) {
LogUtils.e(TAG, "处理失败: ${e.message}")
ToastUtils.show("处理失败: ${e.message}")
@@ -315,6 +319,9 @@ class FaceRecognitionViewModel @Inject constructor(
localAudioPlayService.playByRoute("face_recognition_failed")
}
}
} catch (e: CancellationException) {
// 页面离开时保持协程取消语义,避免误显示 "Job was cancelled"
throw e
} catch (e: Exception) {
LogUtils.e("人脸识别失败: ${e.message}")
ToastUtils.show("识别失败: ${e.message}")
@@ -1,12 +1,12 @@
package com.yzx.kiosk.ui.upload.viewmodel
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.lifecycle.viewModelScope
import com.yzx.kiosk.base.BaseViewModel
import com.yzx.kiosk.datastore.AppState
import com.yzx.kiosk.navigation.AppNavigator
import com.yzx.kiosk.navigation.routes.AppRoutes
import com.yzx.kiosk.network.di.NetworkModule.CLIENT_DOWNLOAD
import com.yzx.kiosk.network.model.request.PrintCompleteRequest
import com.yzx.kiosk.network.model.request.PrintNotifyRequest
import com.yzx.kiosk.network.model.request.SaveAlbumUrlRequest
@@ -17,6 +17,7 @@ import com.yzx.kiosk.priter.PrintCallback
import com.yzx.kiosk.priter.PrintRecord
import com.yzx.kiosk.priter.PrintStatusManager
import com.yzx.kiosk.priter.PrinterService
import com.yzx.kiosk.priter.PrintImageIntegrityValidator
import com.yzx.kiosk.utils.LogUtils
import com.yzx.kiosk.utils.ToastUtils
import com.google.gson.Gson
@@ -35,9 +36,12 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import java.net.URL
import okhttp3.CacheControl
import okhttp3.OkHttpClient
import okhttp3.Request
import kotlin.coroutines.resume
import javax.inject.Inject
import javax.inject.Named
@HiltViewModel
class PrintingViewModel @Inject constructor(
@@ -46,7 +50,8 @@ class PrintingViewModel @Inject constructor(
private val printerService: PrinterService,
private val appStoreDataSource: AppStoreDataSource,
private val printStatusManager: PrintStatusManager,
private val netWorkRepository: NetWorkRepository
private val netWorkRepository: NetWorkRepository,
@Named(CLIENT_DOWNLOAD) private val printDownloadClient: OkHttpClient
) : BaseViewModel(
navigator = navigator,
appState = appState
@@ -55,6 +60,7 @@ class PrintingViewModel @Inject constructor(
private const val TAG = "PrintingViewModel"
private const val PRINT_TIME_PER_PHOTO = 15 // 每张照片预计打印时间(秒)
private const val COUNTDOWN_AFTER_PRINT = 90 // 打印完成后倒计时(秒)
private const val IMAGE_DOWNLOAD_RETRY_COUNT = 3
}
/**
@@ -502,17 +508,64 @@ class PrintingViewModel @Inject constructor(
* 下载网络图片
*/
private suspend fun downloadImage(url: String): Bitmap? = withContext(Dispatchers.IO) {
try {
val connection = URL(url).openConnection()
connection.connectTimeout = 10000
connection.readTimeout = 10000
connection.getInputStream().use { inputStream ->
BitmapFactory.decodeStream(inputStream)
val logUrl = url.substringBefore('?')
for (attempt in 1..IMAGE_DOWNLOAD_RETRY_COUNT) {
try {
val request = Request.Builder()
.url(url)
.get()
.cacheControl(CacheControl.FORCE_NETWORK)
.build()
printDownloadClient.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw java.io.IOException("HTTP ${response.code}")
}
val body = response.body ?: throw java.io.IOException("响应体为空")
val declaredLength = body.contentLength()
if (declaredLength > PrintImageIntegrityValidator.MAX_DOWNLOAD_BYTES) {
throw java.io.IOException("图片超过大小上限: $declaredLength bytes")
}
// bytes() 会完整读取响应;服务端声明了 Content-Length 时,OkHttp 会校验实际长度。
val bytes = body.bytes()
if (declaredLength >= 0L && declaredLength != bytes.size.toLong()) {
throw java.io.IOException(
"响应长度不一致: declared=$declaredLength, actual=${bytes.size}"
)
}
val decoded = PrintImageIntegrityValidator.decodeDownloadedImage(bytes)
LogUtils.d(
TAG,
"打印图片校验成功 - url=$logUrl, attempt=$attempt, bytes=${bytes.size}, " +
"format=${decoded.format}, mime=${decoded.mimeType}, " +
"source=${decoded.sourceWidth}x${decoded.sourceHeight}, " +
"decoded=${decoded.bitmap.width}x${decoded.bitmap.height}, " +
"sample=${decoded.sampleSize}, sha256=${decoded.sha256.take(16)}"
)
return@withContext decoded.bitmap
}
} catch (e: CancellationException) {
throw e
} catch (e: OutOfMemoryError) {
LogUtils.e(TAG, "打印图片解码内存不足 - url=$logUrl, attempt=$attempt")
return@withContext null
} catch (e: Exception) {
LogUtils.e(
TAG,
"打印图片下载或校验失败 - url=$logUrl, attempt=$attempt/$IMAGE_DOWNLOAD_RETRY_COUNT, error=${e.message}"
)
if (attempt < IMAGE_DOWNLOAD_RETRY_COUNT) {
delay(500L * attempt)
}
}
} catch (e: Exception) {
LogUtils.e(TAG, "下载图片失败: $url, ${e.message}")
null
}
LogUtils.e(TAG, "打印图片连续 $IMAGE_DOWNLOAD_RETRY_COUNT 次校验失败,禁止送入打印机 - url=$logUrl")
null
}
/**
@@ -0,0 +1,102 @@
package com.yzx.kiosk.ui.face.viewmodel
import com.google.gson.Gson
import com.yzx.kiosk.network.model.request.PaySuccessMessageRequest
import com.yzx.kiosk.network.model.response.PaySuccessMessageData
import com.yzx.kiosk.network.model.response.PaySuccessMessageResponse
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class FacePayStatusDecisionTest {
@Test
fun `pending and unknown statuses continue polling`() {
listOf(null, 10, 20, 0, 70).forEach { status ->
assertEquals(
FacePayStatusDecision.CONTINUE_POLLING,
decideFacePayStatus(status)
)
}
}
@Test
fun `completed status completes payment`() {
assertEquals(
FacePayStatusDecision.COMPLETE_PAYMENT,
decideFacePayStatus(30)
)
}
@Test
fun `cancel and refund statuses stop polling`() {
listOf(40, 50, 60).forEach { status ->
assertEquals(
FacePayStatusDecision.STOP_WITH_TERMINAL_STATUS,
decideFacePayStatus(status)
)
}
}
@Test
fun `valid face payment message is accepted`() {
assertTrue(isValidFacePaySuccessMessage("ORDER-1", message()))
}
@Test
fun `empty or mismatched payment data is rejected`() {
assertFalse(isValidFacePaySuccessMessage("ORDER-1", null))
assertFalse(isValidFacePaySuccessMessage("OTHER", message()))
assertFalse(isValidFacePaySuccessMessage("ORDER-1", message(type = 4)))
assertFalse(isValidFacePaySuccessMessage("ORDER-1", message(captureType = 1)))
assertFalse(isValidFacePaySuccessMessage("ORDER-1", message(imageIds = emptyList())))
}
@Test
fun `pay status request uses backend order number field`() {
val json = Gson().toJson(PaySuccessMessageRequest("ORDER-1"))
assertEquals("{\"order_number\":\"ORDER-1\"}", json)
}
@Test
fun `pay status response deserializes nested websocket payload`() {
val json = """
{
"order_status": 30,
"order_status_name": "已完成",
"sn": "OSCAR_DEVICE_SN",
"type": 5,
"data": {
"order_number": "ORDER-1",
"capture_type": 2,
"image_id": [335, 340]
}
}
""".trimIndent()
val response = Gson().fromJson(json, PaySuccessMessageResponse::class.java)
assertEquals(30, response.orderStatus)
assertEquals("ORDER-1", response.data?.orderNumber)
assertEquals(listOf(335, 340), response.data?.imageIds)
assertTrue(isValidFacePaySuccessMessage("ORDER-1", response))
}
private fun message(
type: Int = 5,
captureType: Int = 2,
imageIds: List<Int> = listOf(335, 340)
) = PaySuccessMessageResponse(
orderStatus = 30,
orderStatusName = "已完成",
sn = "OSCAR_DEVICE_SN",
type = type,
data = PaySuccessMessageData(
orderNumber = "ORDER-1",
captureType = captureType,
imageIds = imageIds
)
)
}