From 641d6300869c8d53e8b038e47eb0fe469891a95a Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期二, 01 四月 2025 10:29:07 +0800
Subject: [PATCH] 1.vip续费页面-会员信息

---
 app/src/main/java/com/example/firstapp/activity/VipActivity.kt |  139 ++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 128 insertions(+), 11 deletions(-)

diff --git a/app/src/main/java/com/example/firstapp/activity/VipActivity.kt b/app/src/main/java/com/example/firstapp/activity/VipActivity.kt
index 56df585..1199c1b 100644
--- a/app/src/main/java/com/example/firstapp/activity/VipActivity.kt
+++ b/app/src/main/java/com/example/firstapp/activity/VipActivity.kt
@@ -10,7 +10,6 @@
 import android.widget.Button
 import android.widget.CheckBox
 import android.widget.CompoundButton
-import android.widget.EditText
 import android.widget.ImageView
 import android.widget.LinearLayout
 import android.widget.TextView
@@ -25,16 +24,22 @@
 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.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.google.android.material.dialog.MaterialAlertDialogBuilder
+import com.example.firstapp.utils.PreferencesManager
+import com.google.gson.Gson
 import kotlinx.coroutines.launch
+import java.math.BigDecimal
 
 
 class VipActivity : AppCompatActivity() {
@@ -42,6 +47,16 @@
     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)
@@ -63,6 +78,9 @@
         binding.ivBack.setOnClickListener{
             finish()
         }
+
+//        会员基本信息初始化
+        loadUserInfo()
 
 //        会员卡片初始化
         vipCardInit()
@@ -115,7 +133,19 @@
     private fun doAlipay(){
         lifecycleScope.launch {
             try {
-                val response = RetrofitClient.apiService.getPayOrderInfo()
+
+                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}")
                 // 这里调用支付宝
@@ -125,6 +155,7 @@
 //                                Snackbar.make(binding.root, "支付成功", Snackbar.LENGTH_LONG).show()
                             runOnUiThread {
                                 Toast.makeText(this@VipActivity, "支付成功", Toast.LENGTH_LONG).show()
+                                loadUserInfo()
                             }
                         }
                         else -> {
@@ -240,21 +271,66 @@
                 "首月限时特惠",
                 "连续包月",
                 "首次开通",
-                "9.9",
+                BigDecimal("0.01"),
                 "次月起12元/月",
-                "自动续费可随时取消"
+                "自动续费可随时取消",
             ),
             CardData.Yearly(
                 "折合9元/月",
                 "年卡",
-                "108",
-                "168"
+                BigDecimal("0.01"),
+                BigDecimal("168")
             ),
             CardData.SingleMonth(
                 "1个月",
-                "15"
+                BigDecimal("0.01")
             )
         )
+
+        // 这里获取列表的第一项作为初始化的值
+        // 获取第一项作为初始化的值
+        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);
+
+            alipayAmount.text=currentPrice.toString()
+            wechatAmount.text=currentPrice.toString()
+
+
+        }catch (e: Exception) {
+            Toast.makeText(this@VipActivity, e.message, Toast.LENGTH_LONG).show()
+            Log.d("AliPayHelper","xuan: ${e.message}")
+        }
 
         val adapter = CardAdapter(cardList) { cardViewList, cardData, cardView ->
             handleVipCardClick(cardViewList, cardData, cardView)
@@ -369,19 +445,60 @@
             is CardData.Yearly -> cardData.title
             is CardData.SingleMonth -> cardData.title
         }
-        val price = when (cardData) {
+        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=price
-        wechatAmount.text=price
+        alipayAmount.text=currentPrice.toString()
+        wechatAmount.text=currentPrice.toString()
         // 处理点击事件,这里我们只是简单地展示一个 Toast
         Toast.makeText(this, "点击了: ${title}", Toast.LENGTH_SHORT).show()
+
+        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()
+            }
+        }
+
     }
 
 }
\ No newline at end of file

--
Gitblit v1.9.3