package com.fuban.user.ui.crosscity
|
|
import android.app.Activity
|
import android.os.CountDownTimer
|
import android.view.View
|
import androidx.core.os.bundleOf
|
import cn.sinata.xldutils.utils.*
|
import com.fuban.user.R
|
import com.fuban.user.dialog.PayDialog
|
import com.fuban.user.dialog.TipDialog
|
import com.fuban.user.network.HttpManager
|
import com.fuban.user.network.entity.Order
|
import com.fuban.user.network.request
|
import com.fuban.user.ui.TransparentStatusBarActivity
|
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 kotlinx.android.synthetic.main.activity_pay_cross.*
|
import org.jetbrains.anko.sdk27.coroutines.onClick
|
import org.jetbrains.anko.startActivity
|
|
class PayCrossActivity : TransparentStatusBarActivity() , PayListener {
|
override fun setContentView() = R.layout.activity_pay_cross
|
|
private val id by lazy { //订单id
|
intent.getIntExtra("id", 0)
|
}
|
private var order:Order? = null
|
private var isGotoWx = false //是否跳转到微信小程序支付 true:回来时需要查询是否支付成功
|
private val mPayDialog by lazy {
|
val payDialog = PayDialog()
|
payDialog.setCallback(object : PayDialog.Callback {
|
override fun onOk(way: Int, couponId: Int) {
|
pay(way, couponId)
|
payDialog.dismiss()
|
}
|
|
override fun onClose() {
|
}
|
})
|
payDialog
|
}
|
private var timer :CountDownTimer?= null
|
|
override fun initClick() {
|
tv_action.onClick {
|
if (order!=null)
|
mPayDialog.show(supportFragmentManager, "pay")
|
}
|
tv_cancel.onClick {
|
val tipDialog = TipDialog()
|
tipDialog.arguments = bundleOf(
|
"msg" to "您确定要取消该订单吗?",
|
"ok" to "确定取消",
|
"cancel" to "不取消"
|
)
|
tipDialog.setCallback(object : TipDialog.OnClickCallback {
|
override fun onOk() {
|
showDialog()
|
cancelOrder()
|
}
|
|
override fun onCancel() {
|
}
|
})
|
tipDialog.show(supportFragmentManager, "cancel")
|
}
|
}
|
|
private fun cancelOrder() {
|
HttpManager.cancelOrder(id, Const.OrderType.TYPE_CROSS_CITY).request(this){ _, _->
|
myToast("取消成功")
|
setResult(Activity.RESULT_OK)
|
finish()
|
}
|
}
|
|
override fun initView() {
|
title = "支付费用"
|
showDialog()
|
getData()
|
PayUtil.addPayListener(this)
|
PayUtil.initWeChatPay(this, Const.WX_APP_ID)
|
}
|
|
private fun getData(){
|
HttpManager.queryOrderInfo(id, Const.OrderType.TYPE_CROSS_CITY).request(this){ _, data->
|
data?.apply {
|
this@PayCrossActivity.order = this
|
showDialog()
|
queryBalance()
|
tv_start.text = startAddress
|
tv_end.text = endAddress
|
tv_time.text = travelTime.substring(0, 6)+" "+lineShiftTime+"出发"
|
tv_count.text = String.format("%d人乘车:%s号座位", peopleNumber, seatNumber).replace(
|
",",
|
"号,"
|
)
|
tv_driver.text = "${driverName}-$licensePlate"
|
tv_remark.visibility = if (remark.isEmpty()) View.GONE else {
|
tv_remark.text = remark
|
View.VISIBLE
|
}
|
startTimer()
|
}
|
}
|
}
|
|
private fun startTimer(){
|
val deadline = (order!!.insertTime.parserTime()) + 30L * 60 * 1000 //付款结束时间
|
val offset = deadline - System.currentTimeMillis()
|
if (offset<=0){
|
myToast("订单已失效,请重新下单")
|
finish()
|
}else{
|
timer = object :CountDownTimer(offset, 1000){
|
override fun onFinish() {
|
myToast("订单已失效,请重新下单")
|
finish()
|
}
|
|
override fun onTick(millisUntilFinished: Long) {
|
tv_timer.text = SpanBuilder(
|
String.format(
|
"支付剩余:%d分%02d秒",
|
millisUntilFinished / 1000 / 60,
|
millisUntilFinished / 1000 % 60
|
)
|
)
|
.color(this@PayCrossActivity, 0, 5, R.color.textColor).build()
|
}
|
}.start()
|
}
|
}
|
|
/**
|
* 查询余额和优惠券以供支付
|
*/
|
private fun queryBalance(){
|
HttpManager.queryBalance(id, Const.OrderType.TYPE_CROSS_CITY).request(
|
this,
|
success = { _, data ->
|
data?.let {
|
mPayDialog.arguments = bundleOf(
|
"balance" to it.optDouble("balance"),
|
"coupon" to it.optInt("coupon"),
|
"money" to order!!.orderMoney,
|
"id" to id,
|
"type" to Const.OrderType.TYPE_CROSS_CITY
|
)
|
}
|
}){ _, _->
|
myToast("余额获取失败")
|
mPayDialog.arguments = bundleOf(
|
"money" to order!!.orderMoney,
|
"id" to id,
|
"type" to Const.OrderType.TYPE_CROSS_CITY
|
)
|
}
|
}
|
|
/**
|
* @param way 1=微信,2=支付宝,3=余额
|
*/
|
private fun pay(way: Int, couponId: Int){
|
// if (way == 1){ //微信支付,跳转微信小程序
|
// val api = WXAPIFactory.createWXAPI(this, Const.WX_APP_ID)
|
//
|
//
|
// val req = WXLaunchMiniProgram.Req()
|
// req.userName = "gh_bc8f199f11ae" // 填小程序原始id
|
// val content = if (order!!.state == 12)"{\"cancleId\":${order!!.cancelId}}"
|
// else if (couponId!=0) "{\"couponId\":${couponId}}"
|
// else ""
|
// req.path = "/pages/appPay/appPay?orderId=${id}&orderType=${Const.OrderType.TYPE_CROSS_CITY}&type=${if (order!!.state == 12) 2 else 1}" +
|
// "&userType=1&uid=${SPUtils.instance().getInt(Const.User.USER_ID)}&content=${content}"
|
// req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE// 可选打开 开发版,体验版和正式版
|
// isGotoWx = true
|
// api.sendReq(req)
|
//
|
// }else{
|
HttpManager.payTaxiOrder(
|
id,
|
way,
|
Const.OrderType.TYPE_CROSS_CITY,
|
if (couponId == 0) null else couponId
|
).request(this){ _, data->
|
if (way == 3)
|
onPaySuccess()
|
else if (way == 2)
|
PayUtil.aliPay(this, data!!.orderString)
|
else if (way == 1){
|
startActivity<WXPayEntryActivity>("type" to 1,"data" to data)
|
}
|
}
|
// }
|
}
|
|
|
|
override fun onPaySuccess() {
|
myToast("支付成功")
|
startActivity<PaySuccessActivity>("order" to order)
|
setResult(Activity.RESULT_OK)
|
finish()
|
}
|
|
override fun onResume() {
|
super.onResume()
|
if (isGotoWx){
|
isGotoWx = false
|
HttpManager.queryOrderInfo(id, Const.OrderType.TYPE_CROSS_CITY).request(this){ _, data->
|
if (data?.state == order?.state){
|
myToast("支付失败")
|
}else{
|
onPaySuccess()
|
}
|
}
|
}
|
}
|
|
override fun onPayCancel() {
|
}
|
|
override fun onDestroy() {
|
super.onDestroy()
|
PayUtil.removePayListener(this)
|
PayUtil.unregisterApp()
|
timer?.cancel()
|
}
|
}
|