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()
|
}
|
|
}
|