lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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()
    }
}