From 2b02b6e854a56a511588e4865ddf2c6597675329 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期一, 16 六月 2025 16:04:54 +0800 Subject: [PATCH] feat(nfc): 添加读卡功能并优化写卡流程 --- generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt | 106 ++++++++++++++++++++++++++++++++-------------------- 1 files changed, 65 insertions(+), 41 deletions(-) diff --git a/generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt b/generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt index 7aa0eed..8e43c3d 100644 --- a/generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt +++ b/generallibrary/src/main/java/com/dayu/general/bean/card/UserCard.kt @@ -3,15 +3,16 @@ import com.dayu.baselibrary.bean.BaseUserCardCard import com.dayu.baselibrary.tools.BcdUtil import com.dayu.baselibrary.tools.HexUtil +import com.dayu.general.tool.CardCommon.Companion.USER_CARD_TYPE_1 import java.io.Serializable import java.util.* /** * 閫氱敤椤圭洰鐢ㄦ埛鍗$粨鏋� */ -class UserCard : BaseCard(), Serializable { - var cardType: String = "A1" // 鍗$被鍨嬶細A1缁堢鍐欏崱 A8鍒峰崱寮�娉靛悗鍊� A2鍙犲姞鍏呭�� - var areaNumber: Int = 0 // 鍥藉琛屾斂鍖哄煙鍙�(12浣岯CD,绮剧‘鍒版潙) +class UserCard : BaseUserCardCard(), Serializable { + var cardType: String = USER_CARD_TYPE_1 // 鍗$被鍨嬶細A1缁堢鍐欏崱 A8鍒峰崱寮�娉靛悗鍊� A2鍙犲姞鍏呭�� + var areaNumber: String = "" // 鍥藉琛屾斂鍖哄煙鍙�(12浣岯CD,绮剧‘鍒版潙) var userCode: String = "" // 鐢ㄦ埛缂栧彿BCD var userCodeNumber: Int = 0 // 鐢ㄦ埛鍗$紪鍙�(HEX) var phoneNumber: String = "" // 鎵嬫満鍙�(BCD) @@ -29,10 +30,10 @@ if (data == null || data.size < 3) { return false } - + val expectedBytes = arrayOf(getZeroBytes(), getOneBytes(), getTwoBytes()) - return data.zip(expectedBytes.toList()).all { (actual, expected) -> - actual.contentEquals(expected) + return data.zip(expectedBytes.toList()).all { (actual, expected) -> + actual.contentEquals(expected) } } @@ -46,7 +47,7 @@ /** * 閫氳繃byte杞琤ean */ - fun getBean(data: List<ByteArray>): UserCard? { + override fun getBean(data: List<ByteArray>): UserCard? { try { val userCard = UserCard() // 瑙f瀽绗�0鍧� @@ -54,7 +55,7 @@ userCard.apply { // 瑙f瀽鍥藉琛屾斂鍖哄煙鍙�(0-5浣�) val areaCodeBytes = zero.copyOfRange(0, 6) - areaNumber = BcdUtil.bcdToStr(areaCodeBytes).toInt() + areaNumber = BcdUtil.bcdToStr(areaCodeBytes) // 瑙f瀽鐢ㄦ埛鍗$紪鍙�(6-7浣�) val userCodeNumberBytes = zero.copyOfRange(6, 8) @@ -74,7 +75,7 @@ balance = HexUtil.get16To10LowHightByBytes(one.copyOfRange(1, 5)) surplusWater = HexUtil.get16To10LowHightByBytes(one.copyOfRange(5, 9)) electricPrice = HexUtil.hexToFloatLowHigh(one.copyOfRange(9, 11)) - + // 瑙f瀽鍏呭�兼椂闂� val year = HexUtil.getBcdToInt(one[11]) val month = HexUtil.getBcdToInt(one[12]) @@ -105,19 +106,26 @@ val data = ByteArray(16) try { // 璁剧疆鍥藉琛屾斂鍖哄煙鍙�(BCD鏍煎紡锛�6瀛楄妭锛�0-5浣�) - val areaCodeBytes = BcdUtil.strToBcd(String.format("%012d", areaNumber)) - System.arraycopy(areaCodeBytes, 0, data, 0, 6) + val areaNumberStr = if (areaNumber.isBlank() || !areaNumber.all { it.isDigit() }) { + "000000000000" + } else { + areaNumber.padStart(12, '0') + } + val areaCodeBytes = BcdUtil.strToBcd(areaNumberStr) + System.arraycopy(areaCodeBytes, 0, data, 0, minOf(areaCodeBytes.size, 6)) - // 璁剧疆鐢ㄦ埛鍗$紪鍙�(HEX鏍煎紡锛�2瀛楄妭锛�6-7浣�) + // 璁剧疆鐢ㄦ埛鍗$紪鍙�(HEX鏍煎紡锛�2瀛楄妭锛�6-7浣�) - 淇锛氱‘淇濇暟缁勯暱搴︽纭� val userCodeBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(userCodeNumber)) - System.arraycopy(userCodeBytes, 0, data, 6, 2) + val userCodePadded = ByteArray(2) + System.arraycopy(userCodeBytes, 0, userCodePadded, 0, minOf(userCodeBytes.size, 2)) + System.arraycopy(userCodePadded, 0, data, 6, 2) // 璁剧疆鍗$被鍨�(8浣�) data[8] = HexUtil.hexToByte(cardType) // 璁剧疆鎵嬫満鍙�(BCD鏍煎紡锛�6瀛楄妭锛�9-14浣�) val phoneBytes = BcdUtil.strToBcd(phoneNumber.padStart(12, '0')) - System.arraycopy(phoneBytes, 0, data, 9, 6) + System.arraycopy(phoneBytes, 0, data, 9, minOf(phoneBytes.size, 6)) // 璁剧疆鏍¢獙鍜�(15浣�) data[15] = getByteSum(data) @@ -133,19 +141,27 @@ val data = ByteArray(16) try { data[0] = projectCode.toByte() - - // 璁剧疆浣欓 - val balanceBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance)) - System.arraycopy(balanceBytes, 0, data, 1, 4) - - // 璁剧疆鍓╀綑姘撮噺 - val waterBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater)) - System.arraycopy(waterBytes, 0, data, 5, 4) - - // 璁剧疆鐢典环 + + // 璁剧疆浣欓 - 淇锛氱‘淇濇暟缁勯暱搴︽纭� + val balanceHex = HexUtil.get10To16LowHigh(balance) + val balanceBytes = HexUtil.hexToByteArray(balanceHex) + val balancePadded = ByteArray(4) + System.arraycopy(balanceBytes, 0, balancePadded, 0, minOf(balanceBytes.size, 4)) + System.arraycopy(balancePadded, 0, data, 1, 4) + + // 璁剧疆鍓╀綑姘撮噺 - 淇锛氱‘淇濇暟缁勯暱搴︽纭� + val waterHex = HexUtil.get10To16LowHigh(surplusWater) + val waterBytes = HexUtil.hexToByteArray(waterHex) + val waterPadded = ByteArray(4) + System.arraycopy(waterBytes, 0, waterPadded, 0, minOf(waterBytes.size, 4)) + System.arraycopy(waterPadded, 0, data, 5, 4) + + // 璁剧疆鐢典环 - 淇锛氱‘淇濇暟缁勯暱搴︽纭� val priceBytes = HexUtil.hexToByteArray(HexUtil.floatToHexLowHigh(electricPrice)) - System.arraycopy(priceBytes, 0, data, 9, 2) - + val pricePadded = ByteArray(2) + System.arraycopy(priceBytes, 0, pricePadded, 0, minOf(priceBytes.size, 2)) + System.arraycopy(pricePadded, 0, data, 9, 2) + // 璁剧疆鍏呭�兼椂闂� rechargeDate?.let { data[11] = HexUtil.getIntToBCD(it.get(Calendar.YEAR) % 100)[0] @@ -153,7 +169,7 @@ data[13] = HexUtil.getIntToBCD(it.get(Calendar.DAY_OF_MONTH))[0] data[14] = HexUtil.getIntToBCD(it.get(Calendar.HOUR_OF_DAY))[0] } - + data[15] = getByteSum(data) } catch (e: Exception) { e.printStackTrace() @@ -166,17 +182,25 @@ fun toBytes(): ByteArray { val data = ByteArray(16) try { - // 澶囦唤浣欓鍜屾按閲忔暟鎹� - val balanceBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(balance)) - System.arraycopy(balanceBytes, 0, data, 1, 4) - - val waterBytes = HexUtil.hexToByteArray(HexUtil.get10To16LowHigh(surplusWater)) - System.arraycopy(waterBytes, 0, data, 5, 4) - - // 璁剧疆姘翠环 + // 澶囦唤浣欓鍜屾按閲忔暟鎹� - 淇锛氱‘淇濇暟缁勯暱搴︽纭� + val balanceHex = HexUtil.get10To16LowHigh(balance) + val balanceBytes = HexUtil.hexToByteArray(balanceHex) + val balancePadded = ByteArray(4) + System.arraycopy(balanceBytes, 0, balancePadded, 0, minOf(balanceBytes.size, 4)) + System.arraycopy(balancePadded, 0, data, 1, 4) + + val waterHex = HexUtil.get10To16LowHigh(surplusWater) + val waterBytes = HexUtil.hexToByteArray(waterHex) + val waterPadded = ByteArray(4) + System.arraycopy(waterBytes, 0, waterPadded, 0, minOf(waterBytes.size, 4)) + System.arraycopy(waterPadded, 0, data, 5, 4) + + // 璁剧疆姘翠环 - 淇锛氱‘淇濇暟缁勯暱搴︽纭� val priceBytes = HexUtil.hexToByteArray(HexUtil.floatToHexLowHigh(waterPrice)) - System.arraycopy(priceBytes, 0, data, 9, 2) - + val pricePadded = ByteArray(2) + System.arraycopy(priceBytes, 0, pricePadded, 0, minOf(priceBytes.size, 2)) + System.arraycopy(pricePadded, 0, data, 9, 2) + // 璁剧疆鍏呭�兼椂闂� rechargeDate?.let { data[11] = HexUtil.getIntToBCD(it.get(Calendar.YEAR) % 100)[0] @@ -184,7 +208,7 @@ data[13] = HexUtil.getIntToBCD(it.get(Calendar.DAY_OF_MONTH))[0] data[14] = HexUtil.getIntToBCD(it.get(Calendar.HOUR_OF_DAY))[0] } - + data[15] = getByteSum(data) } catch (e: Exception) { e.printStackTrace() @@ -193,7 +217,7 @@ } } - fun getZeroBytes(): ByteArray = Zero().toBytes() - fun getOneBytes(): ByteArray = One().toBytes() - fun getTwoBytes(): ByteArray = Two().toBytes() + override fun getZeroBytes(): ByteArray = Zero().toBytes() + override fun getOneBytes(): ByteArray = One().toBytes() + override fun getTwoBytes(): ByteArray = Two().toBytes() } -- Gitblit v1.8.0