| | |
| | | import com.dayu.baselibrary.tools.nfc.NFCCallBack |
| | | import com.dayu.baselibrary.utils.MornyUtil |
| | | import com.dayu.baselibrary.utils.ToastUtil |
| | | import com.dayu.general.BaseApplication |
| | | import com.dayu.general.bean.card.AreaCard |
| | | import com.dayu.general.bean.card.CheckCard |
| | | import com.dayu.general.bean.card.ClearCard |
| | | import com.dayu.general.bean.card.DebugCard |
| | | import com.dayu.general.bean.card.UserCard |
| | | import com.dayu.general.tool.CardCommon |
| | | import com.dayu.general.tool.CardOperationType |
| | | import com.dayu.general.dao.BaseDaoSingleton |
| | | import com.dayu.general.databinding.ActivityNfcWriteGeBinding |
| | | import com.dayu.general.net.ApiManager |
| | | import com.dayu.general.net.BaseResponse |
| | | import com.dayu.general.tool.CardOperationType |
| | | import com.dayu.general.tool.NfcReadHelper |
| | | import com.dayu.general.tool.NfcWreatHelper |
| | | import com.dayu.general.dao.BaseDaoSingleton |
| | | import com.tencent.bugly.crashreport.CrashReport |
| | | import kotlinx.coroutines.launch |
| | | import java.lang.StringBuilder |
| | | |
| | | /** |
| | | * NFC写卡操作界面 |
| | | * |
| | | * 功能说明: |
| | | * 1. 支持多种卡类型的写卡操作(开卡、充值、销卡、返还、补扣、补卡、检查卡、区域表号卡、调试卡等) |
| | | * 2. 通过NFC技术将数据写入IC卡 |
| | | * 3. 写卡成功后向服务器上报操作结果 |
| | | * 4. 更新本地数据库中的写卡状态 |
| | | * |
| | | * @author: zuo |
| | | * @date: 2021/3/30 |
| | | * @description:写卡界面 |
| | | * @description: 写卡界面 |
| | | */ |
| | | class NfcWreatActivity : BaseNfcActivity() { |
| | | |
| | | /** 数据绑定对象 */ |
| | | var binding: ActivityNfcWriteGeBinding? = null |
| | | |
| | | /** 卡类型标识 */ |
| | | var cardType = "" |
| | | |
| | | /** 卡地址/卡号 */ |
| | | var cardAddr = "" |
| | | |
| | | /** 卡工本费(分为单位) */ |
| | | var cardFee = 0 |
| | | |
| | | // 充值相关金额 |
| | | // ==================== 充值相关金额 ==================== |
| | | /** 充值金额(元) */ |
| | | private var rechargeAmount = 0.0 |
| | | |
| | | /** 赠送金额(元) */ |
| | | private var bonusAmount = 0.0 |
| | | |
| | | // 销卡相关信息 |
| | | // ==================== 销卡相关信息 ==================== |
| | | /** 退款金额(元) */ |
| | | private var refundAmount = 0.0 |
| | | |
| | | /** 卡内余额(元) */ |
| | | private var cardBalance = 0.0 |
| | | |
| | | //订单编号 |
| | | // ==================== 返还相关信息 ==================== |
| | | /** 返还金额(元) */ |
| | | private var returnAmount = 0.0 |
| | | |
| | | // ==================== 补扣相关信息 ==================== |
| | | /** 补扣金额(元) */ |
| | | private var deductAmount = 0.0 |
| | | |
| | | // ==================== 补卡相关信息 ==================== |
| | | /** 补卡工本费(元) */ |
| | | private var cardCost = 0.0 |
| | | |
| | | /** 补卡金额(元) */ |
| | | private var reissueAmount = 0.0 |
| | | |
| | | /** 订单编号 */ |
| | | var orderNumber = "" |
| | | |
| | | /** 用户卡对象,包含要写入卡内的所有数据 */ |
| | | private lateinit var userCard: UserCard |
| | | private var operationTypeCode = -1; |
| | | |
| | | /** 操作类型代码 */ |
| | | private var operationTypeCode = -1 |
| | | |
| | | /** 操作类型枚举 */ |
| | | private var operationType: CardOperationType? = null |
| | | |
| | | // ==================== 管理卡制作相关参数 ==================== |
| | | /** 区域号(12位数字) */ |
| | | private var regionNumber: String = "" |
| | | |
| | | /** 项目号(1-255) */ |
| | | private var projectNumber: String = "" |
| | | |
| | | override fun onCreate(savedInstanceState: Bundle?) { |
| | | super.onCreate(savedInstanceState) |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取数据 |
| | | * 获取传入的初始化数据 |
| | | * 从Intent中提取写卡所需的各种参数 |
| | | */ |
| | | private fun getInitData() { |
| | | // 获取基本卡信息 |
| | | cardType = intent?.getStringExtra("cardType") ?: "" |
| | | cardAddr = intent?.getStringExtra("cardAddr") ?: "" |
| | | operationTypeCode = intent?.getIntExtra("operationTypeCode", -1) ?: -1 |
| | |
| | | refundAmount = intent?.getDoubleExtra("refundAmount", 0.0) ?: 0.0 |
| | | cardBalance = intent?.getDoubleExtra("cardBalance", 0.0) ?: 0.0 |
| | | |
| | | // 获取返还相关信息 |
| | | returnAmount = intent?.getDoubleExtra("returnAmount", 0.0) ?: 0.0 |
| | | |
| | | // 获取补扣相关信息 |
| | | deductAmount = intent?.getDoubleExtra("deductAmount", 0.0) ?: 0.0 |
| | | |
| | | // 获取补卡相关信息 |
| | | cardCost = intent?.getDoubleExtra("cardCost", 0.0) ?: 0.0 |
| | | reissueAmount = intent?.getDoubleExtra("reissueAmount", 0.0) ?: 0.0 |
| | | |
| | | // 获取卡工本费 |
| | | if (intent?.hasExtra("cardFee") == true) { |
| | | cardFee = intent?.getIntExtra("cardFee", 0) ?: 0 |
| | | } |
| | | |
| | | // 获取用户卡对象(兼容不同Android版本) |
| | | if (intent?.hasExtra("userCard") == true) { |
| | | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { |
| | | userCard = intent?.getSerializableExtra("userCard", UserCard::class.java)!! |
| | | } else { |
| | | userCard = (intent?.getSerializableExtra("userCard") as? UserCard)!! |
| | | } |
| | | |
| | | } |
| | | |
| | | // 获取区域号和项目号 |
| | | regionNumber = intent?.getStringExtra("regionNumber") ?: "" |
| | | projectNumber = intent?.getStringExtra("projectNumber") ?: "" |
| | | |
| | | // 验证orderNumber不能为空 |
| | | if (orderNumber.isEmpty()) { |
| | | ToastUtil.show("订单号不能为空,请重新操作") |
| | | finish() |
| | | return |
| | | } |
| | | |
| | | // 根据操作类型设置界面显示内容 |
| | | if (operationTypeCode != -1) { |
| | | when (operationType) { |
| | | CardOperationType.CleanCard -> { |
| | | val textData = StringBuilder() |
| | | |
| | | // 判断是否来自销卡操作(有退款金额或卡内余额信息) |
| | | if (refundAmount > 0 || cardBalance > 0) { |
| | | textData.append("销卡清零操作\n") |
| | | textData.append("卡地址:$cardAddr\n") |
| | | |
| | | if (cardBalance > 0) { |
| | | textData.append("卡内余额:${String.format("%.2f", cardBalance)}元\n") |
| | | } |
| | | |
| | | if (refundAmount > 0) { |
| | | textData.append("退款金额:${String.format("%.2f", refundAmount)}元") |
| | | } else { |
| | | textData.append("无退款金额") |
| | | } |
| | | } else { |
| | | textData.append("清零卡写卡") |
| | | } |
| | | |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.OpenCard -> { |
| | | var textData = StringBuilder() |
| | | textData.append("用户开卡\n") |
| | | if (cardFee != 0) { |
| | | textData.append("工本费:" + cardFee + "元\n") |
| | | } |
| | | if (userCard.balance != 0) { |
| | | textData.append("充值金额:" + MornyUtil.changeF2Y(userCard.balance) + "元") |
| | | } |
| | | |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.Recharge -> { |
| | | var textData = StringBuilder() |
| | | textData.append("用户充值\n") |
| | | |
| | | // 显示充值金额 |
| | | if (rechargeAmount > 0) { |
| | | textData.append( |
| | | "充值金额:" + String.format( |
| | | "%.2f", |
| | | rechargeAmount |
| | | ) + "元\n" |
| | | ) |
| | | } |
| | | |
| | | // 显示赠送金额 |
| | | if (bonusAmount > 0) { |
| | | textData.append("赠送金额:" + String.format("%.2f", bonusAmount) + "元\n") |
| | | } |
| | | |
| | | // 显示总金额(写入卡内的总余额) |
| | | if (userCard.balance != 0) { |
| | | val totalBalanceInYuan = userCard.balance / 100.0 // 转换为元 |
| | | textData.append( |
| | | "卡内总余额:" + String.format( |
| | | "%.2f", |
| | | totalBalanceInYuan |
| | | ) + "元" |
| | | ) |
| | | } |
| | | |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.CancelCard -> { |
| | | var textData = StringBuilder() |
| | | textData.append("销卡\n") |
| | | textData.append("卡内余额:" + MornyUtil.changeF2Y(userCard.balance) + "元\n") |
| | | textData.append("退款金额:" + refundAmount + "元") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.CheckCard -> TODO() |
| | | CardOperationType.DeductCard -> TODO() |
| | | CardOperationType.ReplaceCard -> TODO() |
| | | null -> TODO() |
| | | } |
| | | setupUIForOperationType() |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 根据操作类型设置界面显示内容 |
| | | * 为不同的卡操作类型显示相应的提示信息 |
| | | */ |
| | | private fun setupUIForOperationType() { |
| | | when (operationType) { |
| | | CardOperationType.CleanCardMake -> { |
| | | val textData = StringBuilder() |
| | | |
| | | // 判断是否来自销卡操作(有退款金额或卡内余额信息) |
| | | if (refundAmount > 0 || cardBalance > 0) { |
| | | textData.append("销卡清零操作\n") |
| | | textData.append("卡地址:$cardAddr\n") |
| | | |
| | | if (cardBalance > 0) { |
| | | textData.append("卡内余额:${String.format("%.2f", cardBalance)}元\n") |
| | | } |
| | | |
| | | if (refundAmount > 0) { |
| | | textData.append("退款金额:${String.format("%.2f", refundAmount)}元") |
| | | } else { |
| | | textData.append("无退款金额") |
| | | } |
| | | } else { |
| | | textData.append("清零卡写卡") |
| | | } |
| | | |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.OpenCard -> { |
| | | // 开卡操作显示信息 |
| | | var textData = StringBuilder() |
| | | textData.append("用户开卡\n") |
| | | if (cardFee != 0) { |
| | | textData.append("工本费:" + cardFee + "元\n") |
| | | } |
| | | if (userCard.balance != 0) { |
| | | textData.append("充值金额:" + MornyUtil.changeF2Y(userCard.balance) + "元") |
| | | } |
| | | |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.Recharge -> { |
| | | // 充值操作显示信息 |
| | | var textData = StringBuilder() |
| | | textData.append("用户充值\n") |
| | | |
| | | // 显示充值金额 |
| | | if (rechargeAmount > 0) { |
| | | textData.append( |
| | | "充值金额:" + String.format( |
| | | "%.2f", |
| | | rechargeAmount |
| | | ) + "元\n" |
| | | ) |
| | | } |
| | | |
| | | // 显示赠送金额 |
| | | if (bonusAmount > 0) { |
| | | textData.append("赠送金额:" + String.format("%.2f", bonusAmount) + "元\n") |
| | | } |
| | | |
| | | // 显示总金额(写入卡内的总余额) |
| | | if (userCard.balance != 0) { |
| | | val totalBalanceInYuan = userCard.balance / 100.0 // 转换为元 |
| | | textData.append( |
| | | "充值后余额:" + MornyUtil.changeF2Y(userCard.balance) + "元" |
| | | ) |
| | | } |
| | | |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.CancelCard -> { |
| | | // 销卡操作显示信息 |
| | | var textData = StringBuilder() |
| | | textData.append("销卡\n") |
| | | textData.append("卡内余额:" + MornyUtil.changeF2Y(userCard.balance) + "元\n") |
| | | textData.append("退款金额:" + refundAmount + "元") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.SUPPLEMENT -> { |
| | | // 返还操作显示信息 |
| | | var textData = StringBuilder() |
| | | textData.append("返还\n") |
| | | textData.append("返还金额:" + returnAmount + "元\n") |
| | | textData.append("返还后卡内余额:" + MornyUtil.changeF2Y(userCard.balance) + "元\n") |
| | | |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.DeductCard -> { |
| | | // 补扣操作显示信息 |
| | | var textData = StringBuilder() |
| | | textData.append("补扣\n") |
| | | textData.append("补扣金额:" + deductAmount + "元\n") |
| | | textData.append("补扣后卡内余额:" + MornyUtil.changeF2Y(userCard.balance) + "元\n") |
| | | |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.ReplaceCard -> { |
| | | // 补卡操作显示信息 |
| | | var textData = StringBuilder() |
| | | textData.append("补卡\n") |
| | | textData.append("卡内余额:" + MornyUtil.changeF2Y(userCard.balance) + "元\n") |
| | | if (cardCost > 0) { |
| | | textData.append("工本费:" + String.format("%.2f", cardCost) + "元\n") |
| | | } |
| | | if (reissueAmount > 0) { |
| | | textData.append("补卡金额:" + String.format("%.2f", reissueAmount) + "元") |
| | | } |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.CheckCardMake -> { |
| | | // 检查卡制作显示信息 |
| | | var textData = StringBuilder() |
| | | textData.append("检查卡制作") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.RegionCardMake -> { |
| | | // 区域表号卡制作显示信息 |
| | | var textData = StringBuilder() |
| | | textData.append("区域表号卡制作\n") |
| | | if (regionNumber.isNotEmpty()) { |
| | | textData.append("区域号:$regionNumber\n") |
| | | } |
| | | if (projectNumber.isNotEmpty()) { |
| | | textData.append("项目号:$projectNumber") |
| | | } |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.DebugCardMake -> { |
| | | // 调试卡制作显示信息 |
| | | var textData = StringBuilder() |
| | | textData.append("调试卡制作") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | |
| | | CardOperationType.CheckCardMake -> { |
| | | var textData = StringBuilder() |
| | | textData.append("制作检查卡") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.DebugCardMake -> { |
| | | var textData = StringBuilder() |
| | | textData.append("制作调试卡") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.CleanCardMake -> { |
| | | var textData = StringBuilder() |
| | | textData.append("制作清零卡") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.IpCardMake -> { |
| | | var textData = StringBuilder() |
| | | textData.append("制作IP设置卡") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.AreaCardMake -> { |
| | | var textData = StringBuilder() |
| | | textData.append("制作域名设置卡") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.GpsCardMake -> { |
| | | var textData = StringBuilder() |
| | | textData.append("制作GPS卡") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.ValveTimeCardMake -> { |
| | | var textData = StringBuilder() |
| | | textData.append("制作时间配置卡") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | CardOperationType.ElectricPriceCardMake -> { |
| | | var textData = StringBuilder() |
| | | textData.append("制作取数卡") |
| | | binding?.cardData?.text = textData.toString() |
| | | } |
| | | |
| | | null -> TODO() |
| | | CardOperationType.MANAGEMENT_CARD_WRITE -> TODO() |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * NFC刷卡回调处理 |
| | | * 当用户将卡片贴近设备时触发此方法 |
| | | * |
| | | * @param intent NFC意图,包含卡片信息 |
| | | */ |
| | | override fun onNfcBack(intent: Intent) { |
| | | // 读取卡号 |
| | | val nfcReadHelper = NfcReadHelper.getInstance(intent, this) |
| | | // 使用正常的getCardNumber()方法,它会自动关闭连接 |
| | | val cardNumber = nfcReadHelper.getCardNumber() |
| | | |
| | | // 验证卡号是否与预期一致 |
| | | if (cardNumber.isNotEmpty() && cardNumber == cardAddr) { |
| | | val nfcWreatHelper = NfcWreatHelper.getInstance(intent, this) |
| | | |
| | | // 根据操作类型执行相应的写卡操作 |
| | | when (operationType) { |
| | | CardOperationType.CleanCard -> { |
| | | var clearCard = ClearCard() |
| | | nfcWreatHelper.writeData(clearCard.getZeroBytes(), 7, 0) { success, message -> |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (success) { |
| | | postCardData(cardAddr) |
| | | ToastUtil.show("写卡成功!") |
| | | } else { |
| | | // 处理写卡失败的情况 |
| | | ToastUtil.show(message) |
| | | } |
| | | } |
| | | } |
| | | CardOperationType.CleanCardMake -> { |
| | | // 清零卡操作(暂未实现具体逻辑) |
| | | } |
| | | |
| | | CardOperationType.OpenCard -> { |
| | | // 开卡操作:将用户信息写入卡片 |
| | | nfcWreatHelper.writeUserDataAsync(userCard, object : NFCCallBack { |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (flag) { |
| | | postCardData(cardAddr) |
| | | |
| | | // 写卡成功,更新写卡状态 |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | ToastUtil.show("写卡失败: ${msg ?: "未知错误"}") |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | |
| | | } |
| | | |
| | | CardOperationType.Recharge -> { |
| | | // 充值操作:更新卡内余额 |
| | | nfcWreatHelper.writeUserDataAsync(userCard, object : NFCCallBack { |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (flag) { |
| | | postCardData(cardAddr) |
| | | // 充值写卡成功,更新写卡状态 |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | ToastUtil.show("充值写卡失败: ${msg ?: "未知错误"}") |
| | | } |
| | |
| | | } |
| | | |
| | | CardOperationType.CancelCard -> { |
| | | // 销卡操作:将卡类型设置为无效 |
| | | var userCard = UserCard() |
| | | userCard.cardType = "00"; |
| | | userCard.cardType = "00" // 设置为无效卡类型 |
| | | nfcWreatHelper.writeUserDataAsync(userCard, object : NFCCallBack { |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (flag) { |
| | | postCardData(cardAddr) |
| | | // 销卡写卡成功,更新写卡状态 |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | ToastUtil.show("销卡写卡失败: ${msg ?: "未知错误"}") |
| | | } |
| | |
| | | }) |
| | | } |
| | | |
| | | CardOperationType.CheckCard -> TODO() |
| | | CardOperationType.DeductCard -> TODO() |
| | | CardOperationType.ReplaceCard -> TODO() |
| | | CardOperationType.SUPPLEMENT -> { |
| | | // 返还操作:更新卡内余额 |
| | | nfcWreatHelper.writeUserDataAsync(userCard, object : NFCCallBack { |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (flag) { |
| | | // 返还写卡成功,更新写卡状态 |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | ToastUtil.show("返还写卡失败: ${msg ?: "未知错误"}") |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | CardOperationType.DeductCard -> { |
| | | // 补扣操作:更新卡内余额 |
| | | nfcWreatHelper.writeUserDataAsync(userCard, object : NFCCallBack { |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (flag) { |
| | | // 补扣写卡成功,更新写卡状态 |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | ToastUtil.show("补扣写卡失败: ${msg ?: "未知错误"}") |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | CardOperationType.ReplaceCard -> { |
| | | // 补卡操作:将原卡数据写入新卡 |
| | | nfcWreatHelper.writeUserDataAsync(userCard, object : NFCCallBack { |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (flag) { |
| | | // 补卡写卡成功,更新写卡状态 |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | ToastUtil.show("补卡写卡失败: ${msg ?: "未知错误"}") |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | CardOperationType.CheckCardMake -> { |
| | | // 检查卡写卡逻辑(功能卡,无需写入用户数据) |
| | | var checkCard = CheckCard() |
| | | nfcWreatHelper.writeDataAsync( |
| | | checkCard.getZeroBytes(), |
| | | 7, |
| | | 0, |
| | | object : NFCCallBack { |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (flag) { |
| | | // 检查卡写卡成功,更新写卡状态 |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | ToastUtil.show("检查卡写卡失败: ${msg ?: "未知错误"}") |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | CardOperationType.RegionCardMake -> { |
| | | // 区域表号卡写卡逻辑(功能卡,无需写入用户数据) |
| | | var areaCard = AreaCard() |
| | | areaCard.areaNumber = regionNumber |
| | | areaCard.projectCode = projectNumber.toInt() |
| | | nfcWreatHelper.writeDataAsync( |
| | | areaCard.getZeroBytes(), |
| | | 7, |
| | | 0, |
| | | object : NFCCallBack { |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (flag) { |
| | | // 区域表号卡写卡成功,更新写卡状态 |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | ToastUtil.show("区域表号卡写卡失败: ${msg ?: "未知错误"}") |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | |
| | | |
| | | CardOperationType.DebugCardMake -> { |
| | | // 调试卡写卡逻辑(功能卡,无需写入用户数据) |
| | | var debugCard = DebugCard() |
| | | nfcWreatHelper.writeDataAsync( |
| | | debugCard.getZeroBytes(), |
| | | 7, |
| | | 0, |
| | | object : NFCCallBack { |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | // 确保Toast在主线程中调用 |
| | | runOnUiThread { |
| | | if (flag) { |
| | | // 调试卡写卡成功,更新写卡状态 |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | ToastUtil.show("调试卡写卡失败: ${msg ?: "未知错误"}") |
| | | } |
| | | } |
| | | } |
| | | }) |
| | | } |
| | | // 新的管理卡制作操作类型处理 |
| | | CardOperationType.RegionCardMake, |
| | | CardOperationType.CheckCardMake, |
| | | CardOperationType.CleanCardMake, |
| | | CardOperationType.IpCardMake, |
| | | CardOperationType.AreaCardMake, |
| | | CardOperationType.GpsCardMake, |
| | | CardOperationType.ValveTimeCardMake, |
| | | CardOperationType.ElectricPriceCardMake -> { |
| | | // 管理卡写卡逻辑(功能卡,无需写入用户数据) |
| | | val operationName = when (operationType) { |
| | | CardOperationType.RegionCardMake -> "区域表号卡" |
| | | CardOperationType.CheckCardMake -> "检查卡" |
| | | CardOperationType.DebugCardMake -> "调试卡" |
| | | CardOperationType.CleanCardMake -> "清零卡" |
| | | CardOperationType.IpCardMake -> "IP设置卡" |
| | | CardOperationType.AreaCardMake -> "域名设置卡" |
| | | CardOperationType.GpsCardMake -> "GPS卡" |
| | | CardOperationType.ValveTimeCardMake -> "时间配置卡" |
| | | CardOperationType.ElectricPriceCardMake -> "取数卡" |
| | | else -> "管理卡" |
| | | } |
| | | ToastUtil.show("${operationName}写卡成功!") |
| | | updateCardWrittenStatus(cardAddr) |
| | | } |
| | | |
| | | null -> TODO() |
| | | CardOperationType.MANAGEMENT_CARD_WRITE -> TODO() |
| | | } |
| | | } else { |
| | | // 卡号不匹配,提示用户 |
| | | ToastUtil.show("卡片错误,当前刷的卡与刚刚的卡不一致") |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 更新CardRegistrationBean中的isCardWritten状态为true |
| | | * 更新本地数据库中的写卡状态 |
| | | * 根据操作类型判断是更新ManagerCardBean还是CardRegistrationBean的isCardWritten状态为true |
| | | * 然后跳转到写卡成功界面,并通知MainActivity调用postCardData |
| | | * |
| | | * @param cardNumber 卡号 |
| | | */ |
| | | private fun updateCardWrittenStatus(cardNumber: String) { |
| | | lifecycleScope.launch { |
| | | try { |
| | | val cardRegistrationDao = BaseDaoSingleton.getInstance(this@NfcWreatActivity) |
| | | .cardRegistrationDao() |
| | | |
| | | // 根据卡号查找CardRegistrationBean记录 |
| | | val cardRegistration = cardRegistrationDao.getByCardNumber(cardNumber) |
| | | if (cardRegistration != null) { |
| | | // 创建更新后的CardRegistrationBean对象,将isCardWritten设置为true |
| | | val updatedCardRegistration = cardRegistration.copy(isCardWritten = true) |
| | | // 更新数据库记录 |
| | | cardRegistrationDao.update(updatedCardRegistration) |
| | | |
| | | // 在主线程中关闭Activity |
| | | runOnUiThread { |
| | | setResult(RESULT_OK) |
| | | finish() |
| | | Intent(this@NfcWreatActivity, CardWriteSuccessActivity::class.java).apply { |
| | | putExtra("cardNumber", cardNumber) |
| | | startActivity(this) |
| | | } |
| | | val baseDaoSingleton = BaseDaoSingleton.getInstance(this@NfcWreatActivity) |
| | | |
| | | // 根据操作类型判断是管理卡还是用户卡操作 |
| | | val isManagerCardOperation = operationTypeCode in 100..108 |
| | | |
| | | var updateSuccess = false |
| | | |
| | | if (isManagerCardOperation) { |
| | | // 管理卡制作操作类型,查询和更新ManagerCardBean |
| | | val managerCardDao = baseDaoSingleton.managerCardDao() |
| | | val managerCard = managerCardDao.getByOrderId(orderNumber) |
| | | |
| | | if (managerCard != null) { |
| | | val updatedManagerCard = managerCard.copy(isCardWritten = true) |
| | | managerCardDao.update(updatedManagerCard) |
| | | updateSuccess = true |
| | | } |
| | | } else { |
| | | // 用户卡操作类型,查询和更新CardRegistrationBean |
| | | val cardRegistrationDao = baseDaoSingleton.cardRegistrationDao() |
| | | val cardRegistration = cardRegistrationDao.getByOrderId(orderNumber) |
| | | |
| | | if (cardRegistration != null) { |
| | | val updatedCardRegistration = cardRegistration.copy(isCardWritten = true) |
| | | cardRegistrationDao.update(updatedCardRegistration) |
| | | updateSuccess = true |
| | | } |
| | | } |
| | | |
| | | // 无论是否找到记录,都跳转到成功界面 |
| | | runOnUiThread { |
| | | setResult(RESULT_OK) |
| | | finish() |
| | | |
| | | // 跳转到写卡成功界面 |
| | | Intent(this@NfcWreatActivity, CardWriteSuccessActivity::class.java).apply { |
| | | putExtra("cardNumber", cardNumber) |
| | | if (::userCard.isInitialized) { |
| | | putExtra("userCard", userCard) |
| | | } |
| | | putExtra("operationTypeCode", operationTypeCode) |
| | | startActivity(this) |
| | | } |
| | | |
| | | // 通知MainActivity调用postCardData |
| | | notifyMainActivityToPostCardData(cardNumber) |
| | | } |
| | | |
| | | } catch (e: Exception) { |
| | | // 记录异常信息 |
| | | CrashReport.postCatchedException(e) |
| | | e.printStackTrace() |
| | | runOnUiThread { |
| | |
| | | } |
| | | } |
| | | |
| | | fun postCardData(cardAddr: String) { |
| | | |
| | | val map = mutableMapOf<String, Any>() |
| | | |
| | | if (cardAddr.isNotEmpty()) { |
| | | map["cardAddr"] = cardAddr |
| | | } |
| | | |
| | | map["operateType"] = operationTypeCode |
| | | if (orderNumber.isNotEmpty()) { |
| | | map["orderNumber"] = orderNumber |
| | | } |
| | | // 使用正确的类型参数 |
| | | ApiManager.getInstance().requestPostLoading( |
| | | this, |
| | | "terminal/card/termCallBack", |
| | | String::class.java, |
| | | map, |
| | | object : SubscriberListener<BaseResponse<String>>() { |
| | | override fun onNext(t: BaseResponse<String>) { |
| | | if (t.success) { |
| | | updateCardWrittenStatus(cardAddr) |
| | | } else { |
| | | // 处理搜索失败的情况 |
| | | ToastUtil.show(t.msg) |
| | | } |
| | | } |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | ToastUtil.show("上报失败: ${e?.message ?: "未知错误"}") |
| | | } |
| | | /** |
| | | * 通知MainActivity调用postCardData上报写卡结果 |
| | | * |
| | | * @param cardNumber 卡号 |
| | | */ |
| | | private fun notifyMainActivityToPostCardData(cardNumber: String) { |
| | | try { |
| | | val mainActivity = BaseApplication.getMainActivity() |
| | | if (mainActivity != null) { |
| | | // 调用MainActivity的postCardData方法 |
| | | mainActivity.postCardData( |
| | | cardAddr = cardNumber, |
| | | operationTypeCode = operationTypeCode, |
| | | orderNumber = orderNumber, |
| | | regionNumber = regionNumber, |
| | | projectNumber = projectNumber |
| | | ) |
| | | } |
| | | ) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | CrashReport.postCatchedException(e) |
| | | } |
| | | } |
| | | |
| | | |