| | |
| | | import android.text.Editable |
| | | import android.text.TextWatcher |
| | | 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.tools.nfc.NFCCallBack |
| | | import com.dayu.baselibrary.view.TitleBar.ClickType_LEFT_IMAGE |
| | | import com.dayu.general.BaseApplication |
| | | import com.dayu.general.R |
| | |
| | | import com.dayu.general.net.ApiManager |
| | | import com.dayu.general.net.BaseResponse |
| | | import com.dayu.general.tool.NfcReadHelper |
| | | import com.dayu.general.tool.NfcWreatHelper |
| | | import com.tencent.bugly.crashreport.CrashReport |
| | | import kotlinx.coroutines.launch |
| | | import android.util.TypedValue |
| | | |
| | | /** |
| | | * Description: 用户开卡界面(同步修改白卡密码) |
| | |
| | | |
| | | // 支付方式 |
| | | private var paymentMethod: String = "现金" |
| | | |
| | | // 支付方式ID |
| | | private var paymentId: Long = 0 |
| | | |
| | | // 支付方式列表 |
| | | private var paymentMethodList: List<PaymentMethod> = listOf() |
| | | |
| | | // 卡物理ID |
| | | private var cardPhysicalId: String = "" |
| | |
| | | private const val TAG = "NewCard2Activity" |
| | | } |
| | | |
| | | // 支付方式数据类 |
| | | data class PaymentMethod( |
| | | val id: Long, |
| | | val name: String, |
| | | val remarks: String, |
| | | val deleted: Int |
| | | ) |
| | | |
| | | // 支付方式接口返回数据类 |
| | | data class PaymentMethodResponse( |
| | | val itemTotal: Any?, |
| | | val obj: List<PaymentMethod>, |
| | | val pageCurr: Any?, |
| | | val pageSize: Any?, |
| | | val pageTotal: Any? |
| | | ) |
| | | |
| | | override fun onCreate(savedInstanceState: Bundle?) { |
| | | super.onCreate(savedInstanceState) |
| | | binding = ActivityNewCard1GeBinding.inflate(layoutInflater) |
| | | setContentView(binding.root) |
| | | |
| | | initView() |
| | | // 获取支付方式 |
| | | getPaymentMethods() |
| | | initListener() |
| | | } |
| | | |
| | |
| | | // 设置金额输入限制为两位小数 |
| | | binding.newCardRechargeAmount.addTextChangedListener(createDecimalTextWatcher()) |
| | | binding.newCardCardFee.addTextChangedListener(createDecimalTextWatcher()) |
| | | } |
| | | |
| | | /** |
| | | * 获取支付方式列表 |
| | | */ |
| | | private fun getPaymentMethods() { |
| | | ApiManager.getInstance().requestGetLoading( |
| | | this, |
| | | "sell/paymentmethod/get", |
| | | PaymentMethodResponse::class.java, |
| | | null, |
| | | object : SubscriberListener<BaseResponse<PaymentMethodResponse>>() { |
| | | override fun onNext(response: BaseResponse<PaymentMethodResponse>) { |
| | | if (response.success) { |
| | | // 获取支付方式列表 |
| | | val paymentMethods = response.content?.obj ?: listOf() |
| | | if (paymentMethods.isNotEmpty()) { |
| | | paymentMethodList = paymentMethods |
| | | // 更新支付方式显示 |
| | | updatePaymentMethodRadioGroup() |
| | | } |
| | | } else { |
| | | Toast.makeText( |
| | | this@NewCard2Activity, |
| | | "获取支付方式失败: ${response.msg}", |
| | | Toast.LENGTH_SHORT |
| | | ).show() |
| | | } |
| | | } |
| | | |
| | | override fun onError(e: Throwable?) { |
| | | super.onError(e) |
| | | Toast.makeText( |
| | | this@NewCard2Activity, |
| | | "获取支付方式失败: ${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 |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | private fun initListener() { |
| | | // 设置支付方式选择监听 |
| | | binding.newCardPaymentMethod.setOnCheckedChangeListener { group, checkedId -> |
| | | paymentMethod = when (checkedId) { |
| | | R.id.newCard_cashPayment -> "现金" |
| | | R.id.newCard_posPayment -> "POS机" |
| | | R.id.newCard_bankTransfer -> "银行转账" |
| | | else -> "现金" |
| | | // 根据选中的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 |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | val formattedRechargeAmount = String.format("%.2f", rechargeAmount) |
| | | val formattedCardFee = String.format("%.2f", cardFee) |
| | | |
| | | // 获取支付方式ID |
| | | val paymentId = when (paymentMethod) { |
| | | "现金" -> 1 |
| | | "POS机" -> 2 |
| | | "银行转账" -> 3 |
| | | else -> 1 |
| | | } |
| | | |
| | | val remark = binding.newCardRemark.text.toString() |
| | | |
| | | // 构建请求参数 |
| | |
| | | params["clientNum"] = binding.newCardFarmerCode.text.toString() // 农户编号 |
| | | params["cardCost"] = (cardFee * 100).toInt() // 购卡金额(工本费)转为分 |
| | | params["amount"] = (rechargeAmount * 100).toInt() // 充值金额转为分 |
| | | params["paymentId"] = paymentId // 支付方式 |
| | | params["paymentId"] = paymentId // 支付方式ID |
| | | params["remarks"] = remark // 备注 |
| | | params["operator"] = BaseApplication.userId // 操作人ID |
| | | |
| | |
| | | farmerCode = binding.newCardFarmerCode.text.toString(), |
| | | cardFee = cardFee, |
| | | remark = binding.newCardRemark.text.toString(), |
| | | paymentMethod = paymentId, |
| | | paymentMethod = paymentId.toInt(), |
| | | isReported = true, |
| | | isCardWritten = true |
| | | ) |
| | | |
| | | |
| | | // 使用协程在后台线程中保存数据 |
| | | lifecycleScope.launch { |
| | | try { |
| | | BaseDaoSingleton.getInstance(this@NewCard2Activity).cardRegistrationDao().insert(cardRegistration) |
| | | Toast.makeText(this@NewCard2Activity, "开卡成功", Toast.LENGTH_SHORT).show() |
| | | BaseDaoSingleton.getInstance(this@NewCard2Activity) |
| | | .cardRegistrationDao().insert(cardRegistration) |
| | | Toast.makeText( |
| | | this@NewCard2Activity, |
| | | "开卡成功", |
| | | Toast.LENGTH_SHORT |
| | | ).show() |
| | | setResult(RESULT_OK) |
| | | finish() |
| | | } catch (e: Exception) { |
| | | Toast.makeText(this@NewCard2Activity, "保存开卡信息失败: ${e.message}", Toast.LENGTH_SHORT).show() |
| | | CrashReport.postCatchedException(e) |
| | | Toast.makeText( |
| | | this@NewCard2Activity, |
| | | "保存开卡信息失败: ${e.message}", |
| | | Toast.LENGTH_SHORT |
| | | ).show() |
| | | } |
| | | } |
| | | } else { |
| | |
| | | } |
| | | |
| | | override fun onNfcBack(intent: Intent) { |
| | | var cardNumber = NfcReadHelper.getInstance(intent, this).getCardNumber() |
| | | var cardNumber = NfcReadHelper.getInstance(intent, this).getCardNumberNoClose() |
| | | if (!cardNumber.isEmpty()) { |
| | | // 保存卡物理ID |
| | | cardPhysicalId = cardNumber |
| | | // 更新UI |
| | | binding.newCardArerNumber.text = cardNumber |
| | | // 隐藏NFC读卡界面 |
| | | binding.nfcContainer.visibility = View.GONE |
| | | binding.centerScroll.visibility = View.VISIBLE |
| | | binding.newCardRegistBtn.visibility = View.VISIBLE |
| | | try { |
| | | // 创建密钥列表的副本,避免ConcurrentModificationException |
| | | val keyList = ArrayList(NfcReadHelper.getInstance(intent, this).getKeyList()) |
| | | |
| | | NfcWreatHelper.getInstance(intent, this).changePS(keyList, false, true, object : |
| | | NFCCallBack { |
| | | |
| | | override fun isSusses(flag: Boolean, msg: String?) { |
| | | if (flag) { |
| | | // 保存卡物理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@NewCard2Activity, |
| | | "卡片初始化失败:" + msg, |
| | | Toast.LENGTH_LONG |
| | | ) |
| | | .show() |
| | | } |
| | | } |
| | | }) |
| | | |
| | | |
| | | } catch (e: Exception) { |
| | | // 处理异常情况 |
| | | CrashReport.postCatchedException(e) |
| | | e.printStackTrace() |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | } |