package com.kuanzhai.user.ui.mine
|
|
import android.app.Activity
|
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.kuanzhai.user.R
|
import com.kuanzhai.user.network.HttpManager
|
import com.kuanzhai.user.network.request
|
import com.kuanzhai.user.ui.TransparentStatusBarActivity
|
import com.kuanzhai.user.dialog.SingleWheelDialog
|
import com.kuanzhai.user.interfaces.StringCallback
|
import com.kuanzhai.user.utils.Const
|
import com.kuanzhai.user.utils.pay.PayListener
|
import com.kuanzhai.user.utils.pay.PayUtil
|
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram
|
import com.tencent.mm.opensdk.openapi.WXAPIFactory
|
import kotlinx.android.synthetic.main.activity_recharge.*
|
|
|
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 == "微信支付")
|
// wxPay()
|
// else{
|
showDialog()
|
HttpManager.depositBalance(if (rlt == "支付宝支付") 2 else 1,money).request(this@RechargeActivity){ _, data->
|
if (rlt == "微信支付")
|
PayUtil.weChatPay(data!!)
|
else
|
PayUtil.aliPay(this@RechargeActivity, data!!.orderInfo)
|
}
|
// }
|
}
|
})
|
singleWheelDialog.show(supportFragmentManager,"way")
|
}
|
}
|
}
|
|
/**
|
* 微信小程序支付
|
*/
|
private fun wxPay() {
|
val api = WXAPIFactory.createWXAPI(this, Const.WX_APP_ID)
|
val req = WXLaunchMiniProgram.Req()
|
req.userName = "gh_a6c22560b6be" // 填小程序原始id
|
val content = "{\"money\":${money}}"
|
req.path = "/pages/appPay/appPay?orderId=&orderType=&type=4" +
|
"&userType=1&uid=${SPUtils.instance().getInt(Const.User.USER_ID)}&content=${content}"
|
req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE// 可选打开 开发版,体验版和正式版
|
isGotoWx = true
|
api.sendReq(req)
|
}
|
|
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)
|
PayUtil.initWeChatPay(this, Const.WX_APP_ID)
|
}
|
|
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)
|
PayUtil.unregisterApp()
|
}
|
|
}
|