| | |
| | | import com.dayu.baselibrary.tools.nfc.NfcReadAdapter |
| | | import com.dayu.baselibrary.utils.ToastUtil |
| | | import com.dayu.baselibrary.view.ConfirmDialog |
| | | import com.dayu.baselibrary.view.TipDialog |
| | | import com.dayu.general.bean.net.CardInfoResult |
| | | import com.dayu.general.databinding.FragmentRechargeBinding |
| | | import com.dayu.general.net.ApiManager |
| | | import com.dayu.general.net.BaseResponse |
| | | import com.dayu.general.tool.NfcReadHelper |
| | | import com.dayu.general.bean.card.UserCard |
| | | import com.dayu.general.tool.CardCommon |
| | | |
| | | class RechargeFragment : Fragment() { |
| | | var binding: FragmentRechargeBinding? = null |
| | |
| | | binding = FragmentRechargeBinding.inflate(inflater, container, false) |
| | | return binding?.root |
| | | } |
| | | |
| | | |
| | | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| | | super.onViewCreated(view, savedInstanceState) |
| | | initView() |
| | | |
| | | } |
| | | |
| | | private fun initView() { |
| | | // 初始化界面显示读卡状态 |
| | | binding?.rechargeReadLL?.visibility = View.VISIBLE |
| | | } |
| | | |
| | | |
| | | |
| | | private fun resetView() { |
| | | // 重置界面显示读卡状态 |
| | | binding?.rechargeReadLL?.visibility = View.VISIBLE |
| | | |
| | | /** |
| | | * 显示确认对话框 |
| | | */ |
| | | private fun showConfirmDialog(message: String, onConfirm: () -> Unit) { |
| | | activity?.let { activity -> |
| | | val confirmDialog = TipDialog(activity, message) { |
| | | onConfirm() |
| | | } |
| | | confirmDialog.show() |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 处理NFC刷卡信息 |
| | | * 该方法由MainActivity调用 |
| | |
| | | try { |
| | | // 检查intent中是否包含NFC Tag |
| | | if (intent.getParcelableExtra<android.nfc.Tag>(android.nfc.NfcAdapter.EXTRA_TAG) == null) { |
| | | ToastUtil.show("未检测到NFC卡片,请确保卡片已正确放置") |
| | | showConfirmDialog("未检测到NFC卡片,请确保卡片已正确放置") { |
| | | } |
| | | return |
| | | } |
| | | |
| | | // 使用NfcReadAdapter读取卡号 |
| | | |
| | | val nfcAdapter = NfcReadHelper.getInstance(intent, activity) |
| | | cardNumber = nfcAdapter.getCardNumber() |
| | | |
| | | if (cardNumber.isNullOrEmpty()) { |
| | | ToastUtil.show("读卡失败,请重新刷卡") |
| | | val cardTypeAndCardNumber = nfcAdapter.getCardTypeAndCardNumber() |
| | | if (cardTypeAndCardNumber.isNullOrBlank() || !cardTypeAndCardNumber.contains(",")) { |
| | | showConfirmDialog("卡片信息读取失败,请重新刷卡") { |
| | | } |
| | | return |
| | | } |
| | | val parts = cardTypeAndCardNumber.split(",") |
| | | if (parts.size < 2) { |
| | | showConfirmDialog("卡片信息格式异常,请重新刷卡") { |
| | | } |
| | | return |
| | | } |
| | | val cardNumber = parts[0] |
| | | val cardType = parts[1] |
| | | this.cardNumber = cardNumber |
| | | if (cardNumber.isBlank()) { |
| | | showConfirmDialog("卡号为空,无法进行充值,请重新刷卡") { |
| | | } |
| | | return |
| | | } |
| | | if (cardType != CardCommon.USER_CARD_TYPE_1) { |
| | | showConfirmDialog("该卡片不是用户卡,请使用正确的用户卡进行充值操作。") { |
| | | } |
| | | return |
| | | } |
| | | |
| | | // 解析用户卡数据 |
| | | val userCard = nfcAdapter.getUserCardData() |
| | | if (userCard == null) { |
| | | showConfirmDialog("解析卡片数据失败,请重新刷卡") { |
| | | } |
| | | return |
| | | } |
| | | |
| | | // 根据卡号获取卡片详细信息 |
| | | getCardInfo(cardNumber!!) |
| | | getCardInfo(cardNumber, userCard) |
| | | } catch (e: Exception) { |
| | | ToastUtil.show("读卡异常:${e.message}") |
| | | showConfirmDialog("读卡异常:${e.message}") { |
| | | } |
| | | e.printStackTrace() |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取卡片详细信息 |
| | | */ |
| | | private fun getCardInfo(cardNumber: String) { |
| | | private fun getCardInfo(cardNumber: String, userCard: UserCard) { |
| | | activity?.let { activity -> |
| | | val map = mutableMapOf<String, Any>() |
| | | map["cardAddr"] = cardNumber |
| | |
| | | object : SubscriberListener<BaseResponse<CardInfoResult>>() { |
| | | override fun onNext(t: BaseResponse<CardInfoResult>) { |
| | | if (t.success) { |
| | | // 跳转到充值详情页面 |
| | | RechargeDetailActivity.start(activity, t.content, cardNumber) |
| | | // 跳转到充值详情页面,传递用户卡信息 |
| | | RechargeDetailActivity.start(activity, t.content, cardNumber, userCard) |
| | | } else { |
| | | // 处理获取失败的情况 |
| | | handleCardInfoError(t.code, t.msg) |
| | |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | ToastUtil.show("获取卡信息失败: ${e?.message ?: "网络异常,请检查网络连接"}") |
| | | // 重置界面状态 |
| | | resetView() |
| | | showConfirmDialog("获取卡信息失败: ${e?.message ?: "网络异常,请检查网络连接"}") { |
| | | } |
| | | } |
| | | } |
| | | ) |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 处理卡信息获取错误 |
| | | */ |
| | | private fun handleCardInfoError(code: String?, msg: String?) { |
| | | val errorTitle: String |
| | | val errorMessage: String |
| | | |
| | | |
| | | when (code) { |
| | | "1001" -> { |
| | | // 数据不存在的特殊处理 |
| | | errorTitle = "卡片未注册" |
| | | errorMessage = "该卡片未在系统中注册,请先进行开卡操作后再充值。" |
| | | } |
| | | |
| | | else -> { |
| | | // 其他错误的通用处理 |
| | | errorTitle = "获取卡信息失败" |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | // 显示确认对话框 |
| | | activity?.let { activity -> |
| | | val confirmDialog = ConfirmDialog(activity, errorTitle, errorMessage) { |
| | | // 点击确认按钮后关闭对话框并重置界面 |
| | | resetView() |
| | | } |
| | | confirmDialog.show() |
| | | showConfirmDialog(errorMessage) { |
| | | } |
| | | } |
| | | } |