左晓为主开发手持机充值管理机
generallibrary/src/main/java/com/dayu/general/activity/RechargeDetailActivity.kt
@@ -23,6 +23,7 @@
import com.dayu.general.bean.net.RechargeRequest
import com.dayu.general.bean.net.RechargeResult
import com.dayu.general.bean.card.UserCard
import com.dayu.general.bean.net.PaymentMethodListResponse
import com.dayu.general.databinding.ActivityRechargeDetailBinding
import com.dayu.general.net.ApiManager
import com.dayu.general.net.BaseResponse
@@ -149,23 +150,60 @@
    }
    /**
     * 获取支付方式列表
     * 获取支付方式列表 - 使用Object类型安全处理JSON数组
     */
    private fun getPaymentMethods() {
        ApiManager.getInstance().requestGetLoading(
            this,
            "terminal/paymentmethod/get",
            Array<PaymentMethod>::class.java,
            Any::class.java,
            null,
            object : SubscriberListener<BaseResponse<Array<PaymentMethod>>>() {
                override fun onNext(response: BaseResponse<Array<PaymentMethod>>) {
            object : SubscriberListener<BaseResponse<Any>>() {
                override fun onNext(response: BaseResponse<Any>) {
                    if (response.success) {
                        // 获取支付方式列表,现在content直接是PaymentMethod数组
                        val paymentMethods = response.content?.toList() ?: listOf()
                        if (paymentMethods.isNotEmpty()) {
                            paymentMethodList = paymentMethods
                            // 更新支付方式显示
                            updatePaymentMethodRadioGroup()
                        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()
                                // 调试日志
                                android.util.Log.d("RechargeDetail", "成功获取${paymentMethods.size}个支付方式:")
                                paymentMethods.forEach { method ->
                                    android.util.Log.d("RechargeDetail", "- ID: ${method.id}, Name: ${method.name}")
                                }
                            } else {
                                Toast.makeText(
                                    this@RechargeDetailActivity,
                                    "获取支付方式失败:返回数据为空",
                                    Toast.LENGTH_SHORT
                                ).show()
                            }
                        } catch (e: Exception) {
                            android.util.Log.e("RechargeDetail", "解析支付方式数据失败", e)
                            android.util.Log.e("RechargeDetail", "原始数据类型: ${response.content?.javaClass?.name}")
                            android.util.Log.e("RechargeDetail", "原始数据内容: $response.content")
                            Toast.makeText(
                                this@RechargeDetailActivity,
                                "解析支付方式数据失败: ${e.message}",
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    } else {
                        Toast.makeText(
@@ -178,6 +216,7 @@
                override fun onError(e: Throwable?) {
                    super.onError(e)
                    android.util.Log.e("RechargeDetail", "网络请求失败", e)
                    Toast.makeText(
                        this@RechargeDetailActivity,
                        "获取支付方式失败: ${e?.message ?: "网络异常"}",
@@ -341,30 +380,42 @@
            return
        }
        // 调用充值接口
        callRechargeApi(rechargeAmount, bonusAmount)
        // 调用充值接口,传递当前余额
        callRechargeApi(currentBalance, rechargeAmount, bonusAmount)
    }
    /**
     * 调用充值接口
     */
    private fun callRechargeApi(rechargeAmount: Double, bonusAmount: Double) {
    private fun callRechargeApi(currentBalance: Double, rechargeAmount: Double, bonusAmount: Double) {
        val cardNum = cardInfo?.cardNum ?: cardAddress ?: ""
        if (cardNum.isEmpty()) {
            ToastUtil.show("卡号信息缺失")
            return
        }
        // 验证支付方式是否已选择,如果为空则重新获取支付方式
        if (paymentId.isEmpty()) {
            ToastUtil.show("支付方式未加载,正在重新获取...")
            // 重新获取支付方式,成功后自动重试充值
            getPaymentMethodsAndRetryRecharge(currentBalance, rechargeAmount, bonusAmount)
            return
        }
        // 获取水价(如果为空会自动触发MainActivity获取)
        val currentWaterPrice = BaseApplication.requestWaterPrice()
        // 构建充值请求参数
        // 打印调试信息
        android.util.Log.d("RechargeDetail", "充值参数 - paymentMethod: $paymentMethod, paymentId: $paymentId")
        android.util.Log.d("RechargeDetail", "字段含义 - money(当前余额): ${String.format("%.2f", currentBalance)}元, amount(充值金额): ${String.format("%.2f", rechargeAmount)}元, gift(赠送金额): ${String.format("%.2f", bonusAmount)}元")
        // 构建充值请求参数 - 修正字段含义
        val rechargeRequest = RechargeRequest(
            rechargeType = 2,
            cardNum = cardNum,
            money = String.format("%.0f", rechargeAmount),
            amount = String.format("%.0f", bonusAmount),
            gift = String.format("%.0f", bonusAmount),
            money = String.format("%.2f", currentBalance), // money为当前卡余额
            amount = String.format("%.2f", rechargeAmount), // amount为充值金额
            gift = String.format("%.2f", bonusAmount), // gift为赠送金额
            paymentId = paymentId,
            price = String.format("%.2f", currentWaterPrice), // 使用统一获取的水价
            remarks = "充值",
@@ -412,6 +463,64 @@
    }
    /**
     * 重新获取支付方式并重试充值
     */
    private fun getPaymentMethodsAndRetryRecharge(currentBalance: Double, rechargeAmount: Double, bonusAmount: Double) {
        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()
                                // 支付方式加载成功后,自动重试充值
                                ToastUtil.show("支付方式加载成功,正在重试充值...")
                                callRechargeApi(currentBalance, rechargeAmount, bonusAmount)
                            } else {
                                ToastUtil.show("获取支付方式失败:返回数据为空")
                            }
                        } catch (e: Exception) {
                            android.util.Log.e("RechargeDetail", "解析支付方式数据失败", e)
                            android.util.Log.e("RechargeDetail", "原始数据类型: ${response.content?.javaClass?.name}")
                            android.util.Log.e("RechargeDetail", "原始数据内容: ${response.content}")
                            ToastUtil.show("解析支付方式数据失败: ${e.message}")
                        }
                    } else {
                        ToastUtil.show("获取支付方式失败: ${response.msg}")
                    }
                }
                override fun onError(e: Throwable?) {
                    super.onError(e)
                    ToastUtil.show("获取支付方式失败: ${e?.message ?: "网络异常"}")
                }
            }
        )
    }
    /**
     * 启动写卡界面
     */
    private fun startWriteCardActivity(