lmw
2024-11-20 ec282d9bf134fbda578ff901e4a620222a851d3f
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
package com.sinata.xqmuse.ui.mine
 
import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import cn.sinata.xldutils.utils.myToast
import com.sinata.xqmuse.R
import com.sinata.xqmuse.dialog.PayDialog
import com.sinata.xqmuse.network.HttpManager
import com.sinata.xqmuse.network.request
import com.sinata.xqmuse.ui.TransparentStatusBarActivity
import com.sinata.xqmuse.utils.Const
import com.sinata.xqmuse.utils.NumberInputFilter
import com.sinata.xqmuse.utils.event.EmptyEvent
import com.sinata.xqmuse.utils.interfaces.StringCallback
import com.sinata.xqmuse.utils.pay.PayListener
import com.sinata.xqmuse.utils.pay.PayUtil
import kotlinx.android.synthetic.main.activity_recharge.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.lang.Exception
 
class RechargeActivity : TransparentStatusBarActivity(),PayListener {
    override fun setContentView() = R.layout.activity_recharge
 
    private var way = 1
    private var money = 0.0
 
    override fun initClick() {
        tv_way.setOnClickListener {
            PayDialog.show(supportFragmentManager,object : StringCallback {
                override fun onResult(rst: String) {
                    way = rst.toInt()
                    tv_way.text = if (rst == "1") "微信" else "支付宝"
                }
            },0.0,false)
        }
 
        tv_action.onClick {
            val moneyS = et_money.text.toString()
            if (moneyS.isEmpty())
                myToast("请填写充值金额")
            else{
                try {
                    money = moneyS.toDouble()
                    if (money == 0.0)
                        myToast("充值金额不能为0")
                    else{
                        pay(way,money)
                    }
                }catch (e:Exception){
                    myToast("充值金额有误")
                }
            }
        }
    }
 
    override fun initView() {
        PayUtil.addPayListener(this)
        et_money.filters = arrayOf(NumberInputFilter(Int.MAX_VALUE.toDouble()))
        tv_action.alpha = 0.5f
        et_money.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }
 
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
 
            override fun afterTextChanged(s: Editable?) {
                tv_action.alpha = if (s.isNullOrEmpty()) 0.5f else 1f
            }
        })
    }
 
    private fun pay(payType:Int,money:Double){
        toast("%s充值%.2f".format(if (payType == 1) "微信" else "支付宝",money))
        HttpManager.placeOrder(4,payType,2,money,null,null,null )
            .request(this){_,data->
                if (payType == 1){
                    PayUtil.initWeChatPay(this,Const.WX_APP_ID)
                    PayUtil.weChatPay(data!!)
                }else{
                    PayUtil.aliPay(this,data?.orderInfo?:"")
                }
                onPaySuccess() //todo test
 
            }
    }
 
    override fun onPaySuccess() {
        EventBus.getDefault().post(EmptyEvent(Const.EventCode.REFRESH_WALLET)) //充值成功,刷新钱包
        startActivity<RechargeSucActivity>("money" to money,"way" to way)
        setResult(Activity.RESULT_OK)
        finish()
    }
 
    override fun onPayCancel() {
    }
 
    override fun onPayError(msg: String) {
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK){
            tv_action.postDelayed({
                tv_action.callOnClick()
            },500)
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        PayUtil.removePayListener(this)
        PayUtil.unregisterApp()
    }
 
}