|  |  | 
 |  |  | package com.dayu.general.activity | 
 |  |  |  | 
 |  |  | import android.content.Intent | 
 |  |  | import android.os.Bundle | 
 |  |  | import android.view.LayoutInflater | 
 |  |  | import com.dayu.baselibrary.activity.BaseActivity | 
 |  |  | import android.text.Editable | 
 |  |  | import android.text.TextWatcher | 
 |  |  | import android.util.TypedValue | 
 |  |  | import android.view.View | 
 |  |  | import android.widget.RadioButton | 
 |  |  | import android.widget.Toast | 
 |  |  | import androidx.lifecycle.lifecycleScope | 
 |  |  | import com.dayu.baselibrary.net.subscribers.SubscriberListener | 
 |  |  | import com.dayu.baselibrary.utils.MornyUtil | 
 |  |  | import com.dayu.baselibrary.view.TitleBar.ClickType_LEFT_IMAGE | 
 |  |  | import com.dayu.general.BaseApplication | 
 |  |  | import com.dayu.general.R | 
 |  |  | import com.dayu.general.bean.card.UserCard | 
 |  |  | import com.dayu.general.bean.db.CardRegistrationBean | 
 |  |  | import com.dayu.general.bean.net.ClientInfo | 
 |  |  | import com.dayu.general.bean.net.NewCardDataResult | 
 |  |  | import com.dayu.general.bean.net.PaymentMethod | 
 |  |  | import com.dayu.general.dao.BaseDaoSingleton | 
 |  |  | import com.dayu.general.databinding.ActivityNewCardGeBinding | 
 |  |  | import com.dayu.general.net.ApiManager | 
 |  |  | import com.dayu.general.net.BaseResponse | 
 |  |  | import com.dayu.general.tool.BaseCommon.Companion.protocol | 
 |  |  | import com.dayu.general.tool.CardCommon.Companion.USER_CARD_TYPE_1 | 
 |  |  | import com.dayu.general.tool.CardOperationType | 
 |  |  | import com.dayu.general.tool.NfcReadHelper | 
 |  |  | import com.dayu.general.tool.NfcWreatHelper | 
 |  |  | import com.dayu.general.utils.DateUtils | 
 |  |  | import com.tencent.bugly.crashreport.CrashReport | 
 |  |  | import kotlinx.coroutines.launch | 
 |  |  |  | 
 |  |  | class NewCardActivity:BaseActivity() { | 
 |  |  |     var binding:ActivityNewCardGeBinding? = null | 
 |  |  |     override fun onCreate(savedInstanceState: Bundle?) { | 
 |  |  |         super.onCreate(savedInstanceState) | 
 |  |  |         binding=  ActivityNewCardGeBinding.inflate(LayoutInflater.from(this)) | 
 |  |  |         setContentView(binding?.root) | 
 |  |  | /** | 
 |  |  |  * Description: 用户开卡界面(同步修改白卡密码) | 
 |  |  |  * Author: zuo | 
 |  |  |  * Date: 2025/4/7 | 
 |  |  |  */ | 
 |  |  | class NewCardActivity : BaseNfcActivity() { | 
 |  |  |  | 
 |  |  |     private lateinit var binding: ActivityNewCardGeBinding | 
 |  |  |  | 
 |  |  |     // 支付方式 | 
 |  |  |     private var paymentMethod: String = "现金" | 
 |  |  |  | 
 |  |  |     // 支付方式ID | 
 |  |  |     private var paymentId: String = "" | 
 |  |  |  | 
 |  |  |     // 支付方式列表 | 
 |  |  |     private var paymentMethodList: List<PaymentMethod> = listOf() | 
 |  |  |  | 
 |  |  |     // 卡物理ID | 
 |  |  |     private var cardPhysicalId: String = "" | 
 |  |  |  | 
 |  |  |     // 用户ID | 
 |  |  |     private var userId: String = "" | 
 |  |  |  | 
 |  |  |     // 客户ID | 
 |  |  |     private var clientId: String = "" | 
 |  |  |  | 
 |  |  |     private lateinit var clientInfo: ClientInfo | 
 |  |  |  | 
 |  |  |     // 是否已读卡 | 
 |  |  |     private var isReadCard: Boolean = false | 
 |  |  |  | 
 |  |  |     private var orderId: String = "" | 
 |  |  |  | 
 |  |  |     companion object { | 
 |  |  |         private const val TAG = "NewCard2Activity" | 
 |  |  |         private const val REQUEST_CODE_NFC_WRITE = 1001 | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     override fun onCreate(savedInstanceState: Bundle?) { | 
 |  |  |         super.onCreate(savedInstanceState) | 
 |  |  |         binding = ActivityNewCardGeBinding.inflate(layoutInflater) | 
 |  |  |         setContentView(binding.root) | 
 |  |  |  | 
 |  |  |         // 获取传递的clientId参数 | 
 |  |  |         clientId = intent.getStringExtra("clientId") ?: "" | 
 |  |  |  | 
 |  |  |         initView() | 
 |  |  |  | 
 |  |  |         // 如果有clientId,获取客户信息 | 
 |  |  |         if (clientId.isNotEmpty()) { | 
 |  |  |             getClientInfo(clientId) | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 获取支付方式 | 
 |  |  |         getPaymentMethods() | 
 |  |  |         initListener() | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private fun initView() { | 
 |  |  |         // 初始化标题栏返回按钮 | 
 |  |  |         binding.titleBar.setOnItemclickListner(ClickType_LEFT_IMAGE) { | 
 |  |  |             finish() | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 初始化NFC读卡容器,初始隐藏 | 
 |  |  |         binding.nfcContainer.visibility = View.VISIBLE | 
 |  |  |  | 
 |  |  |         // 设置金额输入限制为两位小数 | 
 |  |  |         binding.newCardRechargeAmount.addTextChangedListener(createDecimalTextWatcher()) | 
 |  |  |         binding.newCardCardFee.addTextChangedListener(createDecimalTextWatcher()) | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取客户详细信息 | 
 |  |  |      */ | 
 |  |  |     private fun getClientInfo(clientId: String) { | 
 |  |  |         ApiManager.getInstance().requestGetLoading( | 
 |  |  |             this, | 
 |  |  |             "terminal/client/getTermOne/$clientId", | 
 |  |  |             ClientInfo::class.java, | 
 |  |  |             null, | 
 |  |  |             object : SubscriberListener<BaseResponse<ClientInfo>>() { | 
 |  |  |                 override fun onNext(response: BaseResponse<ClientInfo>) { | 
 |  |  |                     if (response.success) { | 
 |  |  |                         clientInfo = response.content!! | 
 |  |  |                         if (clientInfo != null) { | 
 |  |  |                             // 显示客户信息到界面 | 
 |  |  |                             displayClientInfo(clientInfo) | 
 |  |  |                         } else { | 
 |  |  |                             Toast.makeText( | 
 |  |  |                                 this@NewCardActivity, | 
 |  |  |                                 "获取客户信息失败: 返回数据为空", | 
 |  |  |                                 Toast.LENGTH_SHORT | 
 |  |  |                             ).show() | 
 |  |  |                         } | 
 |  |  |                     } else { | 
 |  |  |                         Toast.makeText( | 
 |  |  |                             this@NewCardActivity, | 
 |  |  |                             "获取客户信息失败: ${response.msg}", | 
 |  |  |                             Toast.LENGTH_SHORT | 
 |  |  |                         ).show() | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 override fun onError(e: Throwable?) { | 
 |  |  |                     super.onError(e) | 
 |  |  |                     Toast.makeText( | 
 |  |  |                         this@NewCardActivity, | 
 |  |  |                         "获取客户信息失败: ${e?.message ?: "网络异常"}", | 
 |  |  |                         Toast.LENGTH_SHORT | 
 |  |  |                     ).show() | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         ) | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 显示客户信息到界面 | 
 |  |  |      */ | 
 |  |  |     private fun displayClientInfo(clientInfo: ClientInfo) { | 
 |  |  |         // 保存用户ID供后续使用 | 
 |  |  |         userId = clientInfo.clientId | 
 |  |  |  | 
 |  |  |         // 显示用户基本信息 | 
 |  |  |         binding.newCardUserName.text = clientInfo.name | 
 |  |  |         binding.newCardIdCard.text = if (clientInfo.idCard.isBlank()) "无" else clientInfo.idCard | 
 |  |  |         binding.newCardFarmerCode.text = clientInfo.clientNum | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取支付方式列表 | 
 |  |  |      */ | 
 |  |  |     private fun getPaymentMethods() { | 
 |  |  |         ApiManager.getInstance().requestGetLoading( | 
 |  |  |             this, | 
 |  |  |             "terminal/paymentmethod/get", | 
 |  |  |             Any::class.java, | 
 |  |  |             null, | 
 |  |  |             object : SubscriberListener<BaseResponse<Any>>() { | 
 |  |  |                 override fun onNext(response: BaseResponse<Any>) { | 
 |  |  |                     if (response.success) { | 
 |  |  |                         try { | 
 |  |  |                             // 安全地处理返回的content,服务器返回的是ArrayList<LinkedHashMap> | 
 |  |  |                             val paymentMethods = mutableListOf<PaymentMethod>() | 
 |  |  |                             val content = response.content | 
 |  |  |                              | 
 |  |  |                             if (content is List<*>) { | 
 |  |  |                                 content.forEach { item -> | 
 |  |  |                                     if (item is Map<*, *>) { | 
 |  |  |                                         val id = item["id"]?.toString() ?: "" | 
 |  |  |                                         val name = item["name"]?.toString() ?: "" | 
 |  |  |                                         if (id.isNotEmpty() && name.isNotEmpty()) { | 
 |  |  |                                             paymentMethods.add(PaymentMethod(id, name)) | 
 |  |  |                                         } | 
 |  |  |                                     } | 
 |  |  |                                 } | 
 |  |  |                             } | 
 |  |  |                              | 
 |  |  |                             if (paymentMethods.isNotEmpty()) { | 
 |  |  |                                 paymentMethodList = paymentMethods | 
 |  |  |                                 // 更新支付方式显示 | 
 |  |  |                                 updatePaymentMethodRadioGroup() | 
 |  |  |                             } else { | 
 |  |  |                                 Toast.makeText( | 
 |  |  |                                     this@NewCardActivity, | 
 |  |  |                                     "获取支付方式失败:返回数据为空", | 
 |  |  |                                     Toast.LENGTH_SHORT | 
 |  |  |                                 ).show() | 
 |  |  |                             } | 
 |  |  |                         } catch (e: Exception) { | 
 |  |  |                             android.util.Log.e("NewCard2Activity", "解析支付方式数据失败", e) | 
 |  |  |                             android.util.Log.e("NewCard2Activity", "原始数据类型: ${response.content?.javaClass?.name}") | 
 |  |  |                             android.util.Log.e("NewCard2Activity", "原始数据内容: ${response.content}") | 
 |  |  |                             Toast.makeText( | 
 |  |  |                                 this@NewCardActivity, | 
 |  |  |                                 "解析支付方式数据失败: ${e.message}", | 
 |  |  |                                 Toast.LENGTH_SHORT | 
 |  |  |                             ).show() | 
 |  |  |                         } | 
 |  |  |                     } else { | 
 |  |  |                         Toast.makeText( | 
 |  |  |                             this@NewCardActivity, | 
 |  |  |                             "获取支付方式失败: ${response.msg}", | 
 |  |  |                             Toast.LENGTH_SHORT | 
 |  |  |                         ).show() | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 override fun onError(e: Throwable?) { | 
 |  |  |                     super.onError(e) | 
 |  |  |                     android.util.Log.e("NewCard2Activity", "网络请求失败", e) | 
 |  |  |                     Toast.makeText( | 
 |  |  |                         this@NewCardActivity, | 
 |  |  |                         "获取支付方式失败: ${e?.message ?: "网络异常"}", | 
 |  |  |                         Toast.LENGTH_SHORT | 
 |  |  |                     ).show() | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         ) | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 更新支付方式RadioGroup | 
 |  |  |      */ | 
 |  |  |     private fun updatePaymentMethodRadioGroup() { | 
 |  |  |         // 清空原有RadioButton | 
 |  |  |         binding.newCardPaymentMethod.removeAllViews() | 
 |  |  |  | 
 |  |  |         // 动态添加RadioButton | 
 |  |  |         paymentMethodList.forEachIndexed { index, method -> | 
 |  |  |             val radioButton = RadioButton(this) | 
 |  |  |             radioButton.id = View.generateViewId() // 生成唯一ID | 
 |  |  |             radioButton.layoutParams = android.widget.LinearLayout.LayoutParams( | 
 |  |  |                 0, | 
 |  |  |                 resources.getDimensionPixelSize(R.dimen.dimen_40), | 
 |  |  |                 1.0f | 
 |  |  |             ) | 
 |  |  |  | 
 |  |  |             // 如果不是最后一个按钮,添加右边距 | 
 |  |  |             if (index < paymentMethodList.size - 1) { | 
 |  |  |                 (radioButton.layoutParams as android.widget.LinearLayout.LayoutParams).rightMargin = | 
 |  |  |                     resources.getDimensionPixelSize(R.dimen.dimen_15) | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             radioButton.text = method.name | 
 |  |  |             radioButton.background = resources.getDrawable(R.drawable.radio_selector) | 
 |  |  |             radioButton.buttonDrawable = null | 
 |  |  |             radioButton.gravity = android.view.Gravity.CENTER | 
 |  |  |             radioButton.setTextColor(resources.getColorStateList(R.color.radio_button_text_color)) | 
 |  |  |             radioButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f) | 
 |  |  |  | 
 |  |  |             // 添加到RadioGroup | 
 |  |  |             binding.newCardPaymentMethod.addView(radioButton) | 
 |  |  |  | 
 |  |  |             // 默认选中第一个 | 
 |  |  |             if (index == 0) { | 
 |  |  |                 radioButton.isChecked = true | 
 |  |  |                 paymentMethod = method.name | 
 |  |  |                 paymentId = method.id | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 创建限制输入两位小数的TextWatcher | 
 |  |  |      */ | 
 |  |  |     private fun createDecimalTextWatcher(): TextWatcher { | 
 |  |  |         return object : TextWatcher { | 
 |  |  |             override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { | 
 |  |  |                 // 不需要实现 | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { | 
 |  |  |                 // 不需要实现 | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |             override fun afterTextChanged(s: Editable?) { | 
 |  |  |                 if (s == null || s.isEmpty()) return | 
 |  |  |  | 
 |  |  |                 val str = s.toString() | 
 |  |  |                 // 如果输入的不是有效的小数格式,进行处理 | 
 |  |  |                 if (str.contains(".")) { | 
 |  |  |                     val decimalIndex = str.indexOf(".") | 
 |  |  |                     // 如果小数点后超过两位,截取到两位 | 
 |  |  |                     if (decimalIndex >= 0 && str.length - decimalIndex > 3) { | 
 |  |  |                         s.delete(decimalIndex + 3, str.length) | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private fun initListener() { | 
 |  |  |         // 设置支付方式选择监听 | 
 |  |  |         binding.newCardPaymentMethod.setOnCheckedChangeListener { group, checkedId -> | 
 |  |  |             // 根据选中的ID获取支付方式 | 
 |  |  |             for (i in 0 until group.childCount) { | 
 |  |  |                 val radioButton = group.getChildAt(i) as RadioButton | 
 |  |  |                 if (radioButton.id == checkedId) { | 
 |  |  |                     paymentMethod = radioButton.text.toString() | 
 |  |  |                     paymentId = paymentMethodList[i].id | 
 |  |  |                     break | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         // 设置开卡按钮点击监听 | 
 |  |  |         binding.newCardRegistBtn.setOnClickListener { | 
 |  |  |             if (cardPhysicalId.isEmpty()) { | 
 |  |  |                 // 如果尚未读取到卡物理ID,显示NFC读卡界面 | 
 |  |  |                 binding.nfcContainer.visibility = View.VISIBLE | 
 |  |  |             } else { | 
 |  |  |                 // 已读取到卡物理ID,进行开卡操作 | 
 |  |  |                 registerNewCard() | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 注册新卡 | 
 |  |  |      */ | 
 |  |  |     private fun registerNewCard() { | 
 |  |  |         // 获取充值金额 | 
 |  |  |         val rechargeAmountStr = binding.newCardRechargeAmount.text.toString() | 
 |  |  |         val rechargeAmount = if (rechargeAmountStr.isEmpty()) 0.0 else rechargeAmountStr.toDouble() | 
 |  |  |  | 
 |  |  |         // 获取工本费 | 
 |  |  |         val cardFeeStr = binding.newCardCardFee.text.toString() | 
 |  |  |         val cardFee = if (cardFeeStr.isEmpty()) 0 else cardFeeStr.toInt() | 
 |  |  |  | 
 |  |  |         val remark = binding.newCardRemark.text.toString() | 
 |  |  |  | 
 |  |  |         // 构建请求参数 | 
 |  |  |         val params = HashMap<String, Any>() | 
 |  |  |         params["cardAddr"] = cardPhysicalId // 水卡地址(物理ID) | 
 |  |  |         params["clientId"] = clientId // 农户ID | 
 |  |  |         params["cardCost"] = cardFee  // 购卡金额(工本费)(元) | 
 |  |  |         params["amount"] = rechargeAmount  // 充值金额(元) | 
 |  |  |         params["paymentId"] = paymentId // 支付方式ID | 
 |  |  |         params["remarks"] = remark // 备注 | 
 |  |  |         params["protocol"] = protocol // 协议 | 
 |  |  |         params["operator"] = BaseApplication.userId // 操作人ID | 
 |  |  |  | 
 |  |  |         // 执行卡片激活API请求 | 
 |  |  |         ApiManager.getInstance().requestPostLoading( | 
 |  |  |             this, | 
 |  |  |             "terminal/card/termActiveCard", | 
 |  |  |             NewCardDataResult::class.java, | 
 |  |  |             params, | 
 |  |  |             object : SubscriberListener<BaseResponse<NewCardDataResult>>() { | 
 |  |  |                 override fun onNext(response: BaseResponse<NewCardDataResult>) { | 
 |  |  |                     if (response.success) { | 
 |  |  |                         orderId = response.content?.orderNo.toString() | 
 |  |  |                         // 保存开卡信息到数据库 | 
 |  |  |                         val cardRegistration = CardRegistrationBean( | 
 |  |  |                             cardNumber = cardPhysicalId, | 
 |  |  |                             userName = binding.newCardUserName.text.toString(), | 
 |  |  |                             idCard = binding.newCardIdCard.text.toString(), | 
 |  |  |                             clientId = clientId, | 
 |  |  |                             cardFee = cardFee, | 
 |  |  |                             remark = binding.newCardRemark.text.toString(), | 
 |  |  |                             paymentMethod = paymentId.toLongOrNull()?.toInt() ?: 0, | 
 |  |  |                             isReported = true, | 
 |  |  |                             isCardWritten = false, // 初始设置为false,写卡成功后再更新为true | 
 |  |  |                             operatorId = orderId, | 
 |  |  |  | 
 |  |  |                             ) | 
 |  |  |  | 
 |  |  |                         // 使用协程在后台线程中保存数据 | 
 |  |  |                         lifecycleScope.launch { | 
 |  |  |                             try { | 
 |  |  |                                 BaseDaoSingleton.getInstance(this@NewCardActivity) | 
 |  |  |                                     .cardRegistrationDao().insert(cardRegistration) | 
 |  |  |                                 setResult(RESULT_OK) | 
 |  |  |                                 Intent(this@NewCardActivity, NfcWreatActivity::class.java).apply { | 
 |  |  |                                     putExtra("cardType", USER_CARD_TYPE_1) | 
 |  |  |                                     putExtra("orderNumber", orderId) | 
 |  |  |                                     putExtra("cardAddr", cardPhysicalId) | 
 |  |  |                                     var userCard = UserCard() | 
 |  |  |                                     userCard.areaNumber = | 
 |  |  |                                         response.content?.cardNum?.substring(0, 12).toString() | 
 |  |  |                                     userCard.userCode = clientInfo.clientNum | 
 |  |  |                                     userCard.phoneNumber = clientInfo.phone | 
 |  |  |                                     userCard.userCodeNumber = | 
 |  |  |                                         response.content?.cardNum?.substring(12)?.toInt()!! | 
 |  |  |                                     userCard.projectCode = response.content?.projectNo!! | 
 |  |  |                                     userCard.balance = | 
 |  |  |                                         MornyUtil.changeY2F(response.content?.balance) | 
 |  |  | //                                    userCard.surplusWater = response.content?.surplusWater?.toInt()!! | 
 |  |  |                                     userCard.waterPrice = | 
 |  |  |                                         MornyUtil.changeY2F(response.content?.waterPrice) | 
 |  |  |                                     userCard.electricPrice = | 
 |  |  |                                         MornyUtil.changeY2F(response.content?.waterPrice) | 
 |  |  |                                     userCard.rechargeDate = | 
 |  |  |                                         DateUtils.parseStringToCalendar(response.content?.time) | 
 |  |  |  | 
 |  |  |                                     // 输出userCard的所有信息到日志 | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "=== 开卡UserCard信息 ===" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "卡片类型: ${userCard.cardType}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "国家行政区域号: ${userCard.areaNumber}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "用户编号: ${userCard.userCode}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "用户卡编号: ${userCard.userCodeNumber}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "完整用户编号: ${userCard.getMyUserCode()}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "手机号: ${userCard.phoneNumber}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "项目编码: ${userCard.projectCode}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "卡内余额: ${userCard.balance}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "剩余水量: ${userCard.surplusWater}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "水量单价: ${userCard.waterPrice}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "电量单价: ${userCard.electricPrice}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d( | 
 |  |  |                                         "NewCard2Activity", | 
 |  |  |                                         "充值时间: ${userCard.rechargeDate}" | 
 |  |  |                                     ) | 
 |  |  |                                     android.util.Log.d("NewCard2Activity", "==================") | 
 |  |  |  | 
 |  |  |                                     putExtra("userCard", userCard) | 
 |  |  |                                     putExtra("operationTypeCode", CardOperationType.OpenCard.code) | 
 |  |  |                                     putExtra("cardFee", cardFee) | 
 |  |  |                                     putExtra("clientInfo",clientInfo) | 
 |  |  |  | 
 |  |  |                                     startActivityForResult(this, REQUEST_CODE_NFC_WRITE) | 
 |  |  |                                 } | 
 |  |  |                             } catch (e: Exception) { | 
 |  |  |                                 CrashReport.postCatchedException(e) | 
 |  |  |                                 Toast.makeText( | 
 |  |  |                                     this@NewCardActivity, | 
 |  |  |                                     "保存开卡信息失败: ${e.message}", | 
 |  |  |                                     Toast.LENGTH_SHORT | 
 |  |  |                                 ).show() | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                     } else { | 
 |  |  |                         // 激活失败 | 
 |  |  |                         Toast.makeText( | 
 |  |  |                             this@NewCardActivity, | 
 |  |  |                             "开卡失败: ${response.msg}", | 
 |  |  |                             Toast.LENGTH_SHORT | 
 |  |  |                         ).show() | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |                 override fun onError(e: Throwable?) { | 
 |  |  |                     super.onError(e) | 
 |  |  |                     // 请求异常 | 
 |  |  |                     Toast.makeText( | 
 |  |  |                         this@NewCardActivity, | 
 |  |  |                         "开卡失败: ${e?.message ?: "网络异常"}", | 
 |  |  |                         Toast.LENGTH_SHORT | 
 |  |  |                     ).show() | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         ) | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     override fun onResume() { | 
 |  |  |         super.onResume() | 
 |  |  |         // 开启前台调度系统,优先处理NFC标签 | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     override fun onPause() { | 
 |  |  |         super.onPause() | 
 |  |  |         // 关闭前台调度系统 | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     override fun onNfcBack(intent: Intent) { | 
 |  |  |         if (!isReadCard) { | 
 |  |  |             val cardNumber = NfcReadHelper.getInstance(intent, this).getCardNumberNoClose() | 
 |  |  |             if (cardNumber.isNotEmpty()) { | 
 |  |  |                 try { | 
 |  |  |                     // 创建密钥列表的副本,避免ConcurrentModificationException | 
 |  |  |                     val keyList = ArrayList(NfcReadHelper.getInstance(intent, this).getKeyList()) | 
 |  |  |  | 
 |  |  |                     // 使用美化的加载动画来显示NFC操作的加载状态 | 
 |  |  |                     val nfcHelper = NfcWreatHelper.getInstance(intent, this) | 
 |  |  |                     nfcHelper.setLoadingView(binding.loadingContainer, binding.nfcProgressWheel) | 
 |  |  |  | 
 |  |  |                     // 异步修改密码 | 
 |  |  |                     nfcHelper.changePSAsync( | 
 |  |  |                         keyList, | 
 |  |  |                         false, | 
 |  |  |                         true | 
 |  |  |                     ) { success: Boolean, msg: String? -> | 
 |  |  |                         if (success) { | 
 |  |  |                             isReadCard = true | 
 |  |  |                             // 保存卡物理ID | 
 |  |  |                             cardPhysicalId = cardNumber | 
 |  |  |                             // 更新UI | 
 |  |  |                             binding.newCardArerNumber.text = cardNumber | 
 |  |  |                             // 隐藏NFC读卡界面 | 
 |  |  |                             binding.nfcContainer.visibility = View.GONE | 
 |  |  |                             binding.centerScroll.visibility = View.VISIBLE | 
 |  |  |                             binding.newCardRegistBtn.visibility = View.VISIBLE | 
 |  |  |                         } else { | 
 |  |  |                             // 密码修改失败,处理错误情况 | 
 |  |  |                             Toast.makeText( | 
 |  |  |                                 this@NewCardActivity, | 
 |  |  |                                 "卡片初始化失败:$msg", | 
 |  |  |                                 Toast.LENGTH_LONG | 
 |  |  |                             ).show() | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } catch (e: Exception) { | 
 |  |  |                     // 处理异常情况 | 
 |  |  |                     CrashReport.postCatchedException(e) | 
 |  |  |                     e.printStackTrace() | 
 |  |  |                 } | 
 |  |  |             } else { | 
 |  |  |                 Toast.makeText( | 
 |  |  |                     this@NewCardActivity, | 
 |  |  |                     "获取卡号失败", | 
 |  |  |                     Toast.LENGTH_SHORT | 
 |  |  |                 ).show() | 
 |  |  |             } | 
 |  |  |         } else { | 
 |  |  |             Toast.makeText( | 
 |  |  |                 this@NewCardActivity, | 
 |  |  |                 "当前已读卡,不可再次读卡", | 
 |  |  |                 Toast.LENGTH_SHORT | 
 |  |  |             ).show() | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     override fun onDestroy() { | 
 |  |  |         super.onDestroy() | 
 |  |  |         // 清理NFC工具类中的RxJava订阅,防止内存泄漏 | 
 |  |  |         try { | 
 |  |  |             NfcReadHelper.getInstance(intent, this).clearDisposables() | 
 |  |  |             NfcWreatHelper.getInstance(intent, this).clearDisposables() | 
 |  |  |             // 确保停止动画,避免内存泄漏 | 
 |  |  |             binding.nfcProgressWheel.stopSpinning() | 
 |  |  |         } catch (e: Exception) { | 
 |  |  |             e.printStackTrace() | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | 
 |  |  |         super.onActivityResult(requestCode, resultCode, data) | 
 |  |  |         if (requestCode == REQUEST_CODE_NFC_WRITE && resultCode == RESULT_OK) { | 
 |  |  |             // 写卡成功,关闭当前Activity | 
 |  |  |             finish() | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |