package com.kuanzhai.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.kuanzhai.user.R
|
import com.kuanzhai.user.network.HttpManager
|
import com.kuanzhai.user.network.entity.Order
|
import com.kuanzhai.user.network.request
|
import com.kuanzhai.user.ui.TransparentStatusBarActivity
|
import com.kuanzhai.user.dialog.PayDialog
|
import com.kuanzhai.user.dialog.TipDialog
|
import com.kuanzhai.user.ui.trip.OrderDetailActivity
|
import com.kuanzhai.user.utils.Const
|
import com.kuanzhai.user.utils.pay.PayListener
|
import com.kuanzhai.user.utils.pay.PayUtil
|
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 val type by lazy { //订单类型
|
intent.getIntExtra("type",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,couponType:Int) {
|
pay(way,couponId,couponType)
|
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")
|
showDialog()
|
HttpManager.cancelMoney(id,type).request(this@PayCrossActivity) { _, data ->
|
val money = data?.optDouble("amount")?:0.0
|
val tipDialog = TipDialog()
|
tipDialog.arguments =
|
bundleOf("msg" to if (money == 0.0)"您确定要取消该订单吗?" else "占座成功以后取消订单\n将收取您${money}元服务费", "ok" to "确定取消", "cancel" to "不取消")
|
tipDialog.setCallback(object : TipDialog.OnClickCallback {
|
override fun onOk() {
|
cancelOrder()
|
}
|
|
override fun onCancel() {
|
}
|
})
|
tipDialog.show(supportFragmentManager, "money")
|
}
|
|
}
|
}
|
|
private fun cancelOrder() {
|
HttpManager.cancelOrder(id,type).request(this){ _, _->
|
// myToast("取消成功")
|
setResult(Activity.RESULT_OK)
|
startActivity<OrderDetailActivity>("id" to id,"type" to type)
|
finish()
|
}
|
}
|
|
override fun initView() {
|
title = "支付费用"
|
showDialog()
|
getData()
|
PayUtil.addPayListener(this)
|
PayUtil.initWeChatPay(this, Const.WX_APP_ID)
|
}
|
|
private fun getData(){
|
HttpManager.queryOrderInfo(id, type).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,if (seatNumber.isEmpty()) "包车" else ("%s号座位".format(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,type).request(this,success = {_,data->
|
data?.let {
|
mPayDialog.arguments = bundleOf("balance" to it.optDouble("balance"),
|
"discount" to it.optDouble("discountAmount"), "coupon" to it.optString("objectId"),
|
"discountType" to it.optInt("type"), "money" to order!!.orderMoney,"id" to id,"type" to type)
|
}
|
}){_,_->
|
myToast("余额获取失败")
|
mPayDialog.arguments = bundleOf("money" to order!!.orderMoney,"id" to id,"type" to type)
|
}
|
}
|
|
/**
|
* @param way 1=微信,2=支付宝,3=余额
|
*/
|
private fun pay(way:Int,couponId:Int,couponType: Int){
|
// if (way == 1){ //微信支付,跳转微信小程序
|
// val api = WXAPIFactory.createWXAPI(this, Const.WX_APP_ID)
|
// val req = WXLaunchMiniProgram.Req()
|
// req.userName = "gh_a6c22560b6be" // 填小程序原始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,type,if (couponId == 0) null else couponId,if (couponType == 0) null else couponType).request(this){_,data->
|
if (way == 3)
|
onPaySuccess()
|
else if (way == 2)
|
PayUtil.aliPay(this,data!!.orderInfo)
|
else
|
PayUtil.weChatPay(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,type).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()
|
}
|
}
|