cloudroam
2025-04-16 66a4a5c3aa78869a3c51e79f2a6a610c99016c24
app/src/main/java/com/example/firstapp/activity/VipActivity.kt
@@ -1,24 +1,64 @@
package com.example.firstapp.activity
import android.app.AlertDialog
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.lifecycleScope
import androidx.navigation.ui.AppBarConfiguration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.firstapp.R
import com.example.firstapp.adapter.CardAdapter
import com.example.firstapp.database.request.ProductOrdersRequest
import com.example.firstapp.database.response.UserInfo
import com.example.firstapp.database.service.RetrofitClient
import com.example.firstapp.databinding.ActivityVipBinding
import com.example.firstapp.entity.CardData
import com.example.firstapp.model.CardData
import com.example.firstapp.model.MemberBenefitItem
import com.example.firstapp.pay.PayAbility
import com.example.firstapp.ui.vip.MemberInfoCardFragment
import com.example.firstapp.utils.Log
import com.example.firstapp.utils.PreferencesManager
import com.google.gson.Gson
import kotlinx.coroutines.launch
import java.math.BigDecimal
class VipActivity : AppCompatActivity() {
    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityVipBinding
    private var isPaymentSelected = false // 全局变量,默认未选中
    private var orderName = ""
    private var orderType=""
    private var currentPrice = BigDecimal.ZERO
    private var originalPrice = BigDecimal.ZERO
    private var paymentMethod = ""
    private var currentUserInfo: UserInfo? = null // 确保使用你的实际数据类
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
@@ -41,52 +81,393 @@
            finish()
        }
//        会员基本信息初始化
        loadUserInfo()
//        会员卡片初始化
        vipCardInit()
//        会员权益初始化
        memberBenefitInit()
        // 支付宝点击事件
        handleAlipayClick()
        // 微信点击事件
        handleWechatClick()
        // 勾选协议点击事件
        handlePrototalClick()
        // VIP会员服务协议
        handleVipProtocolClick()
    }
    private fun handlePrototalClick(){
        val protocol: CheckBox = findViewById(R.id.protocol_checkbox);
        // 监听 RadioButton 选中状态
        protocol.setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
            isPaymentSelected = isChecked // 更新全局变量
//            if(isChecked){
//                protocol.background = ContextCompat.getDrawable(this, R.drawable.checkbox_round_selected)
//            }else{
//                protocol.background = ContextCompat.getDrawable(this, R.drawable.checkbox_round)
//            }
        })
    }
    // 这里添加alipay_layout的点击事件
    private fun handleAlipayClick() {
        // 绑定按钮
        val btnAlipay: LinearLayout = findViewById(R.id.alipay_layout);
        btnAlipay.setOnClickListener(View.OnClickListener {
            // 判断协议是否勾选
            if (!isPaymentSelected){
                // 这里需要弹出框
                showConfirmDialog()
            }else{
                doAlipay()
            }
        })
    }
    private fun doAlipay(){
        lifecycleScope.launch {
            try {
                var request = ProductOrdersRequest(
                    orderName=orderName,
                    orderType=orderType,
                    originalPrice=originalPrice,
                    currentPrice=currentPrice,
                    paymentMethod=""
                )
                Log.d("REQUEST", Gson().toJson(request))
                val response = RetrofitClient.apiService.getPayOrderInfo(request)
                Log.d("API_RESPONSE", response.toString())
                var orderInfo=response.data
                Log.d("AliPayHelper","获取订单信息时: ${response}")
                // 这里调用支付宝
                PayAbility.aliPay(this@VipActivity, orderInfo, Observer {
                    when (it.resultStatus) {
                        "9000" -> {
//                                Snackbar.make(binding.root, "支付成功", Snackbar.LENGTH_LONG).show()
                            runOnUiThread {
                                Toast.makeText(this@VipActivity, "支付成功", Toast.LENGTH_LONG).show()
                                loadUserInfo()
                            }
                        }
                        else -> {
//                                Snackbar.make(binding.root, "支付失败", Snackbar.LENGTH_LONG).show()
                            runOnUiThread {
                                Toast.makeText(this@VipActivity, "支付失败", Toast.LENGTH_LONG).show()
                            }
                        }
                    }
                })
            } catch (e: Exception) {
                Toast.makeText(this@VipActivity, e.message, Toast.LENGTH_LONG).show()
                Log.d("AliPayHelper","获取订单信息时发生错误: ${e.message}")
            }
        }
    }
    private fun showConfirmDialog() {
        val dialogView = LayoutInflater.from(this).inflate(R.layout.vip_protocol_dialog_custom, null)
        val dialog = AlertDialog.Builder(this)
            .setView(dialogView)
            .create()
        // 设置自定义背景
        dialog.window?.setBackgroundDrawableResource(R.drawable.dialog_background)
        // 获取按钮并设置点击事件
        val btnConfirm = dialogView.findViewById<Button>(R.id.btnConfirm)
        btnConfirm.setOnClickListener {
            // 将协议勾选上
            val protocol: CheckBox = findViewById(R.id.protocol_checkbox);
            protocol.isChecked = true
            doAlipay()
            dialog.dismiss()
        }
        // 显示对话框
        dialog.show()
    }
    private fun showConfirmDialog2() {
        // 创建标题 TextView 并居中
        val titleView = TextView(this).apply {
            text = "确认开通"
            textSize = 20f
            gravity = Gravity.CENTER
            setPadding(20, 20, 20, 20)
        }
        // 创建内容 TextView 并居中
        val messageView = TextView(this).apply {
            text = "请阅读并同意《会员协议》?"
            textSize = 16f
            gravity = Gravity.CENTER
            setPadding(40, 20, 40, 20)
        }
        AlertDialog.Builder(this)
            .setCustomTitle(titleView)  // 自定义标题
            .setView(messageView)  // 自定义内容
            .setPositiveButton("继续开通") { _, _ ->
                // 确认支付
                doAlipay()
            }
            .setNegativeButton("取消") { dialog, _ ->
                dialog.dismiss() // 关闭对话框
            }
            .show()
    }
    // 这里添加alipay_layout的点击事件
    private fun handleWechatClick() {
        // 绑定按钮
        val wechatAlipay: LinearLayout = findViewById(R.id.wechat_layout);
        wechatAlipay.setOnClickListener(View.OnClickListener {
            lifecycleScope.launch {
                try {
                    // 判断协议是否勾选
                    if (!isPaymentSelected){
                        Toast.makeText(this@VipActivity, "请勾选协议", Toast.LENGTH_LONG).show()
                        return@launch // 直接 return,不往下执行
                    }
//                    Toast.makeText(this@VipActivity, "功能暂未开放", Toast.LENGTH_LONG).show()
//                    return@launch
                    doWechatPay()
                } catch (e: Exception) {
                    Toast.makeText(this@VipActivity, e.message, Toast.LENGTH_LONG).show()
                    Log.d("AliPayHelper","获取订单信息时发生错误: ${e.message}")
                }
            }
        })
    }
    private fun doWechatPay() {
        val map=HashMap<String,String>()
        map["appId"]="wxb4ba3c02aa476ea1"
        map["partnerid"]="1900006771"
        map["package"]="Sign=WXPay"
        map["noncestr"]="2ac6c60ebfe7e549fbbc922ac54269bb"
        map["timeStamp"]="1606317975"
        map["prepayid"]="wx25232614962974fb29d1227819cc3e000g"
        map["sign"]="wx25232614962974fb29d1227819cc3e000g"
        PayAbility.wxPay(this, map, Observer { success ->
            if (success == 0) {
                Toast.makeText(this@VipActivity, "支付失败", Toast.LENGTH_LONG).show()
            } else {
                Toast.makeText(this@VipActivity, "支付成功", Toast.LENGTH_LONG).show()
            }
        })
    }
    private fun vipCardInit() {
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        recyclerView.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        val cardList = listOf(
            CardData.ContinueMonthly(
                "首月限时特惠",
                "连续包月",
                "首次开通",
                "¥9.9",
                BigDecimal("0.01"),
                "次月起12元/月",
                "自动续费可随时取消"
                "自动续费可随时取消",
            ),
            CardData.Yearly(
                "折合9元/月",
                "年卡",
                "¥108/年",
                "¥168"
                BigDecimal("0.01"),
                BigDecimal("168")
            ),
            CardData.SingleMonth(
                "1个月",
                "¥15"
                BigDecimal("0.01")
            )
        )
        val adapter = CardAdapter(cardList) {cardViewList, cardData, cardView ->
            handleCardClick(cardViewList,cardData,cardView)
        // 这里获取列表的第一项作为初始化的值
        // 获取第一项作为初始化的值
        try {
            val curentPriceTmp = cardList.firstOrNull()
            // 确保 currentPrice 不为空并获取价格
            currentPrice = when (curentPriceTmp) {
                is CardData.ContinueMonthly -> curentPriceTmp.price
                is CardData.Yearly -> curentPriceTmp.price
                is CardData.SingleMonth -> curentPriceTmp.price
                else -> BigDecimal.ZERO // 兜底处理,避免 null 时报错
            }
            val title = when (curentPriceTmp) {
                is CardData.ContinueMonthly -> curentPriceTmp.title
                is CardData.Yearly -> curentPriceTmp.title
                is CardData.SingleMonth -> curentPriceTmp.title
                else -> ""
            }
            originalPrice =  when (curentPriceTmp) {
                is CardData.ContinueMonthly -> BigDecimal.ZERO
                is CardData.Yearly -> curentPriceTmp.originalPrice
                is CardData.SingleMonth -> BigDecimal.ZERO
                else -> BigDecimal.ZERO
            }
            // 初始化支付
            orderName = title
            orderType = title
            val alipayAmount:TextView = findViewById(R.id.alipay_amount);
            val wechatAmount:TextView = findViewById(R.id.wechat_amount);
            val protocolDescLayout:TextView = findViewById(R.id.protocol_desc_layout);
            alipayAmount.text=currentPrice.toString()
            wechatAmount.text=currentPrice.toString()
            protocolDescLayout.text="自动续费可随时取消,开通后每月按${currentPrice.toString()}元自动续费,可随时取消自动续费"
        }catch (e: Exception) {
            Toast.makeText(this@VipActivity, e.message, Toast.LENGTH_LONG).show()
            Log.d("AliPayHelper","支付失败: ${e.message}")
        }
        val adapter = CardAdapter(cardList) { cardViewList, cardData, cardView ->
            handleVipCardClick(cardViewList, cardData, cardView)
        }
        recyclerView.adapter = adapter
    }
    private fun memberBenefitInit() {
        val container = findViewById<ConstraintLayout>(R.id.dynamicContainer)
        try {
            val firstMonthCardView = findViewById<CardView>(R.id.vip_first_month_card_view)
            firstMonthCardView.foreground = ContextCompat.getDrawable(this, R.drawable.gray_border_shape)  // 设置自定义背景
        // 传递过来的数据列表
        val itemList = listOf(
            MemberBenefitItem(R.mipmap.vip_life_insurance, "自定义更多分类", true),
            MemberBenefitItem(R.mipmap.vip_ad_no, "自动去广告", false),
            MemberBenefitItem(R.mipmap.vip_ling, "公众号提醒待办", true),
            MemberBenefitItem(R.mipmap.vip_circle_pie, "数据统计", false)
        )
        }catch (e: Exception){
            e.stackTrace
            Log.e("VipActivity",e.message+"")
        var previousViewId = R.id.dynamicContainer
        for ((index, item) in itemList.withIndex()) {
            val iconView = ImageView(this).apply {
                id = View.generateViewId()
                setImageResource(item.iconRes)
                layoutParams = ConstraintLayout.LayoutParams(
                    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, resources.displayMetrics).toInt(),
                    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, resources.displayMetrics).toInt()
                ).apply {
                    // 设置 margin
                    setMargins(
                        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20f, resources.displayMetrics).toInt(), // left
                        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, resources.displayMetrics).toInt(), // top
                        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20f, resources.displayMetrics).toInt(), // right
                        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, resources.displayMetrics).toInt()  // bottom
                    )
                }
            }
            val textView = TextView(this).apply {
                id = View.generateViewId()
                text = item.text
                textSize = 16f
                setTextColor(Color.parseColor("#DFC08E"))
            }
            val checkView = ImageView(this).apply {
                id = View.generateViewId()
//                setImageResource(R.drawable.ic_check)
                setImageResource(R.mipmap.vip_right)
                layoutParams = ConstraintLayout.LayoutParams(
                    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, resources.displayMetrics).toInt(),
                    TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, resources.displayMetrics).toInt()
                )
            }
            container.addView(iconView)
            container.addView(textView)
            container.addView(checkView)
            val constraintSet = ConstraintSet()
            constraintSet.clone(container)
            // Icon 位置
            constraintSet.connect(
                iconView.id,
                ConstraintSet.TOP,
                previousViewId,
                if (index == 0) ConstraintSet.TOP else ConstraintSet.BOTTOM,
                32
            )
            constraintSet.connect(
                iconView.id,
                ConstraintSet.START,
                ConstraintSet.PARENT_ID,
                ConstraintSet.START,
                32
            )
            // Text 位置
            constraintSet.connect(textView.id, ConstraintSet.TOP, iconView.id, ConstraintSet.TOP)
            constraintSet.connect(
                textView.id,
                ConstraintSet.START,
                iconView.id,
                ConstraintSet.END,
                32
            )
            // Check 位置
            constraintSet.connect(checkView.id, ConstraintSet.TOP, iconView.id, ConstraintSet.TOP)
            constraintSet.connect(
                checkView.id,
                ConstraintSet.END,
                ConstraintSet.PARENT_ID,
                ConstraintSet.END,
                32
            )
            constraintSet.applyTo(container)
            previousViewId = iconView.id
        }
    }
    /**
     * 会员卡样式点击事件
     */
    private fun handleCardClick(cardViewList: MutableList<CardView>,cardData: CardData, cardView: CardView) {
    private fun handleVipCardClick(cardViewList: MutableList<CardView>, cardData: CardData, cardView: CardView) {
        // 处理点击事件,修改样式
        // 修改所有的边框色都是灰色
        cardViewList.forEach { card ->
@@ -101,8 +482,81 @@
            is CardData.Yearly -> cardData.title
            is CardData.SingleMonth -> cardData.title
        }
        currentPrice = when (cardData) {
            is CardData.ContinueMonthly -> cardData.price
            is CardData.Yearly -> cardData.price
            is CardData.SingleMonth -> cardData.price
        }
        originalPrice =  when (cardData) {
            is CardData.ContinueMonthly -> BigDecimal.ZERO
            is CardData.Yearly -> cardData.originalPrice
            is CardData.SingleMonth -> BigDecimal.ZERO
        }
        val alipayAmount:TextView = findViewById(R.id.alipay_amount);
        val wechatAmount:TextView = findViewById(R.id.wechat_amount);
        alipayAmount.text=currentPrice.toString()
        wechatAmount.text=currentPrice.toString()
        // 处理点击事件,这里我们只是简单地展示一个 Toast
        Toast.makeText(this, "点击了: ${title}", Toast.LENGTH_SHORT).show()
//        Toast.makeText(this, "点击了: ${title}", Toast.LENGTH_SHORT).show()
        // 只有连续包月才展示
        val xieyiLayout:LinearLayout = findViewById(R.id.xieyi_layout);
        if (title == "连续包月") {
            xieyiLayout.visibility = View.VISIBLE
        }else{
            xieyiLayout.visibility = View.GONE
        }
        orderName = title
        orderType=title
    }
    private fun loadUserInfo() {
        // 获取Fragment实例
        val memberInfoCardFragment = supportFragmentManager.findFragmentById(R.id.memberInfoCardFragment) as MemberInfoCardFragment?
        lifecycleScope.launch {
            try {
                // 从本地获取保存的手机号
                val savedPhone = PreferencesManager.getPhone()
                if (savedPhone.isNullOrEmpty()) {
                    Toast.makeText(this@VipActivity, "用户未登录", Toast.LENGTH_SHORT).show()
                    return@launch
                }
                val response = RetrofitClient.apiService.getUserInfo(savedPhone)
                if (response.code == "0" && response.data != null) {
                    // 保存用户信息
                    currentUserInfo = response.data
                    val userInfo = response.data
                    // 调用ViewModel的刷新方法
                    memberInfoCardFragment?.viewModel?.updateMemberStatus(userInfo)
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Toast.makeText(this@VipActivity, "获取用户信息失败", Toast.LENGTH_SHORT).show()
            }
        }
    }
    // VIP会员服务协议
    private fun handleVipProtocolClick(){
        binding.protocolVip.setOnClickListener{
            startContentActivity("VIP会员服务协议", "VIP会员服务协议")
        }
    }
    private fun startContentActivity(type: String, title: String) {
        val intent = Intent(this, ContentDetailActivity::class.java).apply {
            putExtra(ContentDetailActivity.ID, type)
            putExtra(ContentDetailActivity.EXTRA_TITLE, title)
        }
        startActivity(intent)
    }
}