| | |
| | | package com.dayu.general.activity |
| | | |
| | | import android.content.Intent |
| | | import android.os.Bundle |
| | | import android.view.LayoutInflater |
| | | import android.view.View |
| | | import android.view.ViewGroup |
| | | import androidx.fragment.app.Fragment |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import com.dayu.baselibrary.tools.nfc.NfcReadAdapter |
| | | import com.dayu.baselibrary.utils.ToastUtil |
| | | 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 |
| | | |
| | | class RechargeFragment:Fragment() { |
| | | class RechargeFragment : Fragment() { |
| | | var binding: FragmentRechargeBinding? = null |
| | | private var cardNumber: String? = null |
| | | private var cardInfo: CardInfoResult? = null |
| | | |
| | | override fun onCreateView( |
| | | inflater: LayoutInflater, |
| | | container: ViewGroup?, |
| | | savedInstanceState: Bundle? |
| | | ): View? { |
| | | 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 |
| | | binding?.rechargeTextLL?.visibility = View.GONE |
| | | } |
| | | |
| | | |
| | | |
| | | private fun resetView() { |
| | | // 重置界面显示读卡状态 |
| | | binding?.rechargeReadLL?.visibility = View.VISIBLE |
| | | binding?.rechargeTextLL?.visibility = View.GONE |
| | | cardInfo = null |
| | | } |
| | | |
| | | /** |
| | | * 处理NFC刷卡信息 |
| | | * 该方法由MainActivity调用 |
| | | */ |
| | | fun handleNfcIntent(intent: Intent) { |
| | | activity?.let { activity -> |
| | | try { |
| | | // 检查intent中是否包含NFC Tag |
| | | if (intent.getParcelableExtra<android.nfc.Tag>(android.nfc.NfcAdapter.EXTRA_TAG) == null) { |
| | | ToastUtil.show("未检测到NFC卡片,请确保卡片已正确放置") |
| | | return |
| | | } |
| | | |
| | | // 使用NfcReadAdapter读取卡号 |
| | | val nfcAdapter = NfcReadHelper.getInstance(intent, activity) |
| | | cardNumber = nfcAdapter.getCardNumber() |
| | | |
| | | if (cardNumber.isNullOrEmpty()) { |
| | | ToastUtil.show("读卡失败,请重新刷卡") |
| | | return |
| | | } |
| | | |
| | | // 显示读到的卡号 |
| | | binding?.redInitCode?.text = cardNumber |
| | | |
| | | // 根据卡号获取卡片详细信息 |
| | | getCardInfo(cardNumber!!) |
| | | } catch (e: Exception) { |
| | | ToastUtil.show("读卡异常:${e.message}") |
| | | e.printStackTrace() |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取卡片详细信息 |
| | | */ |
| | | private fun getCardInfo(cardNumber: String) { |
| | | activity?.let { activity -> |
| | | ApiManager.getInstance().requestGetLoading( |
| | | activity, |
| | | "card/getCardInfo/$cardNumber", // 假设API路径 |
| | | CardInfoResult::class.java, |
| | | null, |
| | | object : SubscriberListener<BaseResponse<CardInfoResult>>() { |
| | | override fun onNext(t: BaseResponse<CardInfoResult>) { |
| | | if (t.success) { |
| | | // 保存卡片信息 |
| | | cardInfo = t.content |
| | | // 显示卡片信息 |
| | | displayCardInfo(t.content) |
| | | } else { |
| | | // 处理获取失败的情况 |
| | | ToastUtil.show(t.msg) |
| | | binding?.redStatu?.text = "卡状态异常" |
| | | } |
| | | } |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | ToastUtil.show("获取卡信息失败: ${e?.message ?: "未知错误"}") |
| | | } |
| | | } |
| | | ) |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 显示卡片信息 |
| | | */ |
| | | private fun displayCardInfo(cardInfo: CardInfoResult?) { |
| | | if (cardInfo == null) { |
| | | ToastUtil.show("卡信息为空") |
| | | return |
| | | } |
| | | |
| | | // 切换到显示信息界面 |
| | | binding?.rechargeReadLL?.visibility = View.GONE |
| | | binding?.rechargeTextLL?.visibility = View.VISIBLE |
| | | |
| | | // 设置卡片信息 |
| | | binding?.userName?.text = cardInfo.userName ?: "" |
| | | binding?.redUserCode?.text = cardInfo.userCode ?: "" |
| | | binding?.redRemainderBlance?.text = "${cardInfo.balance ?: 0} 元" |
| | | |
| | | // 设置卡状态 |
| | | val cardStatus = when(cardInfo.status) { |
| | | 1 -> "正常" |
| | | 2 -> "挂失" |
| | | 3 -> "锁定" |
| | | else -> "未知" |
| | | } |
| | | binding?.redStatu?.text = cardStatus |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | } |