| | |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import com.dayu.baselibrary.tools.nfc.NfcReadAdapter |
| | | import com.dayu.baselibrary.utils.ToastUtil |
| | | import com.dayu.baselibrary.view.ConfirmDialog |
| | | import com.dayu.general.bean.net.CardInfoResult |
| | | import com.dayu.general.databinding.FragmentRechargeBinding |
| | | import com.dayu.general.net.ApiManager |
| | |
| | | class RechargeFragment : Fragment() { |
| | | var binding: FragmentRechargeBinding? = null |
| | | private var cardNumber: String? = null |
| | | private var cardInfo: CardInfoResult? = null |
| | | |
| | | override fun onCreateView( |
| | | inflater: LayoutInflater, |
| | |
| | | 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 |
| | | } |
| | | |
| | | /** |
| | |
| | | ToastUtil.show("读卡失败,请重新刷卡") |
| | | return |
| | | } |
| | | |
| | | // 显示读到的卡号 |
| | | binding?.redInitCode?.text = cardNumber |
| | | |
| | | |
| | | // 根据卡号获取卡片详细信息 |
| | | getCardInfo(cardNumber!!) |
| | | } catch (e: Exception) { |
| | |
| | | */ |
| | | private fun getCardInfo(cardNumber: String) { |
| | | activity?.let { activity -> |
| | | val map = mutableMapOf<String, Any>() |
| | | map["cardAddr"] = cardNumber |
| | | ApiManager.getInstance().requestGetLoading( |
| | | activity, |
| | | "card/getCardInfo/$cardNumber", // 假设API路径 |
| | | "terminal/card/readCard", // 假设API路径 |
| | | CardInfoResult::class.java, |
| | | null, |
| | | map, |
| | | object : SubscriberListener<BaseResponse<CardInfoResult>>() { |
| | | override fun onNext(t: BaseResponse<CardInfoResult>) { |
| | | if (t.success) { |
| | | // 保存卡片信息 |
| | | cardInfo = t.content |
| | | // 显示卡片信息 |
| | | displayCardInfo(t.content) |
| | | // 跳转到充值详情页面 |
| | | RechargeDetailActivity.start(activity, t.content, cardNumber) |
| | | } else { |
| | | // 处理获取失败的情况 |
| | | ToastUtil.show(t.msg) |
| | | binding?.redStatu?.text = "卡状态异常" |
| | | handleCardInfoError(t.code, t.msg) |
| | | } |
| | | } |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | ToastUtil.show("获取卡信息失败: ${e?.message ?: "未知错误"}") |
| | | ToastUtil.show("获取卡信息失败: ${e?.message ?: "网络异常,请检查网络连接"}") |
| | | // 重置界面状态 |
| | | resetView() |
| | | } |
| | | } |
| | | ) |
| | |
| | | } |
| | | |
| | | /** |
| | | * 显示卡片信息 |
| | | * 处理卡信息获取错误 |
| | | */ |
| | | private fun displayCardInfo(cardInfo: CardInfoResult?) { |
| | | if (cardInfo == null) { |
| | | ToastUtil.show("卡信息为空") |
| | | return |
| | | private fun handleCardInfoError(code: String?, msg: String?) { |
| | | val errorTitle: String |
| | | val errorMessage: String |
| | | |
| | | when (code) { |
| | | "1001" -> { |
| | | // 数据不存在的特殊处理 |
| | | errorTitle = "卡片未注册" |
| | | errorMessage = "该卡片未在系统中注册,请先进行开卡操作后再充值。" |
| | | } |
| | | else -> { |
| | | // 其他错误的通用处理 |
| | | errorTitle = "获取卡信息失败" |
| | | errorMessage = when { |
| | | msg.isNullOrBlank() -> "获取卡信息失败,请重新刷卡重试。" |
| | | msg.contains("数据不存在") -> "该卡片未在系统中注册,请先进行开卡操作后再充值。" |
| | | msg.contains("网络") -> "网络连接异常,请检查网络连接后重新刷卡。" |
| | | msg.contains("超时") -> "网络请求超时,请重新刷卡重试。" |
| | | else -> "获取卡信息失败:$msg\n\n请重新刷卡重试。" |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 切换到显示信息界面 |
| | | 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 -> "未知" |
| | | // 显示确认对话框 |
| | | activity?.let { activity -> |
| | | val confirmDialog = ConfirmDialog(activity, errorTitle, errorMessage) { |
| | | // 点击确认按钮后关闭对话框并重置界面 |
| | | resetView() |
| | | } |
| | | confirmDialog.show() |
| | | } |
| | | binding?.redStatu?.text = cardStatus |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | } |