lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
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.fuban.user.ui.mine
 
import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.widget.CheckedTextView
import androidx.core.os.bundleOf
import cn.sinata.xldutils.utils.SPUtils
import cn.sinata.xldutils.utils.SpanBuilder
import cn.sinata.xldutils.utils.myToast
import com.fuban.user.R
import com.fuban.user.network.HttpManager
import com.fuban.user.network.request
import com.fuban.user.ui.TransparentStatusBarActivity
import com.fuban.user.dialog.SingleWheelDialog
import com.fuban.user.interfaces.StringCallback
import com.fuban.user.utils.Const
import com.fuban.user.utils.pay.PayListener
import com.fuban.user.utils.pay.PayUtil
import com.fuban.user.wxapi.WXPayEntryActivity
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import kotlinx.android.synthetic.main.activity_recharge.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.support.v4.startActivity
 
 
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 isGotoWx = false //是否跳转到微信小程序支付 true:回来时需要查询是否充值成功
    override fun initClick() {
        tv_action.setOnClickListener {
            if (money == 0.0)
                myToast("请选择充值金额")
            else{
                val singleWheelDialog = SingleWheelDialog()
                singleWheelDialog.arguments = bundleOf("data" to arrayListOf(
//                    "支付宝支付",
                    "微信支付"))
                singleWheelDialog.setCallback(object :StringCallback{
                    override fun onRlt(rlt: String) {
                        if (rlt == "微信支付")
                            HttpManager.depositBalance(if (rlt == "支付宝支付") 2 else 1,money).request(this@RechargeActivity){ _, data->
                                this@RechargeActivity.startActivity<WXPayEntryActivity>("type" to 1,"data" to data)
                            }
                        else{
                            showDialog()
                            HttpManager.depositBalance(if (rlt == "支付宝支付") 2 else 1,money).request(this@RechargeActivity){ _, data->
                                PayUtil.aliPay(this@RechargeActivity, data!!.orderString)
                            }
                        }
                    }
                })
                singleWheelDialog.show(supportFragmentManager,"way")
            }
        }
    }
 
    /**
     * 微信小程序支付
     */
    private fun wxPay() {
    }
 
    override fun initView() {
        title = "充值"
        balance = intent.getDoubleExtra("balance",0.0)
        tv_money.text = SpanBuilder(String.format("¥%.2f",balance)).size(0,1,24).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(0,checkedTextView.text.length-1).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) {
            }
        })
        PayUtil.addPayListener(this)
    }
 
    override fun onPaySuccess() {
        myToast("充值成功")
        setResult(Activity.RESULT_OK)
        finish()
    }
 
    override fun onPayCancel() {
    }
 
    override fun onResume() {
        super.onResume()
        if (isGotoWx){
            isGotoWx = false
            HttpManager.queryUserInfo().request(this){_,data->
                data?.run {
                    if (balance == this@RechargeActivity.balance+money){
                        onPaySuccess()
                    }else{
                        myToast("充值失败")
                    }
                }
            }
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        PayUtil.removePayListener(this)
    }
 
}