Pu Zhibing
2025-12-11 ab0c19e49fabdb8cc6eea454ea455bce2efbb718
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.igo.user.ui.mine
 
import android.app.Activity
import android.text.Editable
import android.text.TextWatcher
import android.widget.CheckedTextView
import android.widget.EditText
import android.widget.TextView
import androidx.core.os.bundleOf
import cn.sinata.xldutils.toBrowser
import cn.sinata.xldutils.utils.SpanBuilder
import cn.sinata.xldutils.utils.myToast
import com.igo.user.R
import com.igo.user.dialog.ChooseCardDialog
import com.igo.user.dialog.SingleWheelDialog
import com.igo.user.interfaces.StringCallback
import com.igo.user.network.HttpManager
import com.igo.user.network.request
import com.igo.user.ui.TransparentStatusBarActivity
import com.igo.user.utils.pay.PayListener
import org.jetbrains.anko.find
 
 
class RechargeActivity : TransparentStatusBarActivity(), PayListener {
    override fun setContentView() = R.layout.activity_recharge
 
    private val items = arrayListOf<CheckedTextView>()
    private var money = 10.0 //充值金额
    private var balance = 0.0
    private var isGotoPay = false //是否跳转到微信小程序支付 true:回来时需要查询是否充值成功
 
    private val tv_action by lazy { find<TextView>(R.id.tv_action) }
    private val tv_money by lazy { find<TextView>(R.id.tv_money) }
    private val tv_10 by lazy { find<CheckedTextView>(R.id.tv_10) }
    private val tv_20 by lazy { find<CheckedTextView>(R.id.tv_20) }
    private val tv_50 by lazy { find<CheckedTextView>(R.id.tv_50) }
    private val tv_100 by lazy { find<CheckedTextView>(R.id.tv_100) }
    private val tv_200 by lazy { find<CheckedTextView>(R.id.tv_200) }
    private val et_money by lazy { find<EditText>(R.id.et_money) }
 
    override fun initClick() {
        tv_action.setOnClickListener {
            if (money == 0.0)
                myToast(getString(R.string.请选择充值金额))
            else{
                val singleWheelDialog = SingleWheelDialog()
                singleWheelDialog.arguments = bundleOf("data" to arrayListOf(getString(R.string.银行卡支付),getString(R.string.手机支付)))
                singleWheelDialog.setCallback(object :StringCallback{
                    override fun onRlt(rlt: String) {
                        if (rlt == getString(R.string.银行卡支付)){
                            val dialog = ChooseCardDialog()
                            dialog.setCallback(object :StringCallback{
                                override fun onRlt(rlt: String) {
                                    showDialog()
                                    HttpManager.depositBalance(2,money,rlt).request(this@RechargeActivity){ _, data->
                                        isGotoPay =  true
                                        toBrowser(data?:"")
                                    }
                                }
                            })
                            dialog.show(supportFragmentManager,"card")
                        } else{
                            showDialog()
                            HttpManager.depositBalance(1,money).request(this@RechargeActivity){ _, data->
                                isGotoPay =  true
                                toBrowser(data?:"")
                            }
                        }
                    }
                })
                singleWheelDialog.show(supportFragmentManager,"way")
            }
        }
    }
 
 
    override fun initView() {
        title = getString(R.string.充值)
        balance = intent.getDoubleExtra("balance",0.0)
        tv_money.text = SpanBuilder(String.format("FCFA%.2f",balance)).size(0,3,21).build()
        items.addAll(arrayOf(tv_10,tv_20,tv_50,tv_100,tv_200))
        items.forEachIndexed { index, checkedTextView ->
            checkedTextView.setOnClickListener {
                items.forEach {
                    it.isChecked = false
                }
                checkedTextView.isChecked = true
                et_money.setText("")
                money = checkedTextView.text.toString().substring(3,checkedTextView.text.length).toDouble()
            }
        }
        et_money.addTextChangedListener(object :TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                if (s.isNullOrEmpty())
                    money = 0.0
                else{
                    items.forEach {
                        it.isChecked = false
                    }
                    money = try {
                        s.toString().toDouble()
                    }catch (e:Exception){
                        0.0
                    }
                }
 
            }
 
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
 
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
        })
    }
 
    override fun onPaySuccess() {
        myToast(getString(R.string.充值成功))
        setResult(Activity.RESULT_OK)
        finish()
    }
 
    override fun onPayCancel() {
    }
 
    override fun onResume() {
        super.onResume()
        if (isGotoPay){
            isGotoPay = false
            HttpManager.queryUserInfo().request(this){_,data->
                data?.run {
                    if (balance == this@RechargeActivity.balance+money){
                        onPaySuccess()
                    }else{
                        myToast(getString(R.string.充值失败))
                    }
                }
            }
        }
    }
}