package com.igo.user.ui.logistics import android.annotation.SuppressLint import android.util.Log import android.util.TypedValue import android.webkit.WebSettings import android.webkit.WebView import android.widget.CheckBox import android.widget.TextView import androidx.core.os.bundleOf import cn.sinata.rxnetty.LogHelper import cn.sinata.xldutils.toBrowser import cn.sinata.xldutils.utils.LanguageUtil import cn.sinata.xldutils.utils.myToast import cn.sinata.xldutils.utils.optDouble import cn.sinata.xldutils.utils.optInt import cn.sinata.xldutils.utils.optString import cn.sinata.xldutils.utils.showAllowingStateLoss import cn.sinata.xldutils.utils.toTime import com.igo.user.IGoApplication import com.igo.user.R import com.igo.user.dialog.ChooseCardDialog import com.igo.user.dialog.PayDialog import com.igo.user.dialog.TipDialog import com.igo.user.extension.safeLoadDataWithBaseURL import com.igo.user.extension.setSafeSetting import com.igo.user.interfaces.StringCallback import com.igo.user.network.HttpManager import com.igo.user.network.entity.Order import com.igo.user.network.request import com.igo.user.ui.H5Activity import com.igo.user.ui.TransparentStatusBarActivity import com.igo.user.ui.crosscity.PayCrossActivity import com.igo.user.ui.mine.OrderActivity import com.igo.user.ui.trip.TripActivity import com.igo.user.utils.Const import com.igo.user.utils.pay.PayListener import com.igo.user.utils.pay.PayUtil import org.jetbrains.anko.find import org.jetbrains.anko.startActivity class LogisticsGuideActivity : TransparentStatusBarActivity(), PayListener { override fun setContentView() = R.layout.activity_logistics_guide private val webView by lazy { find(R.id.webView) } private val tv_rule by lazy { find(R.id.tv_rule) } private val tv_action by lazy { find(R.id.tv_action) } private val cb_rule by lazy { find(R.id.cb_rule) } private val carTypeId by lazy { intent.getIntExtra("carTypeId", 0) } //车型id private val goodsNum by lazy { intent.getIntExtra("goodsNum", 1) } //货物数量 private val goodsType by lazy { intent.getIntExtra("goodsType", 1) } //货物类型(1=普通货物,2=贵重货物) private val startLat by lazy { intent.getDoubleExtra("startLat", 0.0) } private val startLon by lazy { intent.getDoubleExtra("startLon", 0.0) } private val endLat by lazy { intent.getDoubleExtra("endLat", 0.0) } private val endLon by lazy { intent.getDoubleExtra("endLon", 0.0) } private val receiveAddress by lazy { intent.getStringExtra("receiveAddress") } private val receiveName by lazy { intent.getStringExtra("receiveName") } private val receivePhone by lazy { intent.getStringExtra("receivePhone") } private val remark by lazy { intent.getStringExtra("remark") } private val startName by lazy { intent.getStringExtra("startName") } private val fastMoney by lazy { intent.getIntExtra("fastMoney", 0) } // 加急费用 private val urgent by lazy { intent.getIntExtra("urgent", 1) } private val type by lazy { intent.getIntExtra("type", Const.OrderType.TYPE_SAME_CITY) } // 默认为同城物流 //预估价格 private val estimatePrice by lazy { intent.getDoubleExtra("estimatePrice", 0.0) } private var isGotoWx = false //是否跳转到微信小程序支付 true:回来时需要查询是否支付成功 private var isCancel = false private var balance = -1.0 private var coupon = 0 private var redTotal = 0.0 private var deductionAmount = 0.0 private var order: Order? = null private var id: Int = 0 // 订单id override fun initClick() { tv_rule.setOnClickListener { // HttpManager.getH5(15).request(this, showDialog = true) { _, data -> // val url = data?.optString("content") ?: "" // startActivity("url" to url, "title" to getString(R.string.邮寄快件指南)) // } } tv_action.setOnClickListener { if (cb_rule.isChecked) { // setResult(Activity.RESULT_OK) // finish() // 犟种测试非要在这支付 createOrder() } else { myToast(getString(R.string.阅读并同意邮寄指南)) } } } override fun initView() { title = getString(R.string.邮寄快件指南) if (LanguageUtil.getLanguage() == Const.Language.FRENCH) { //解决法文标题过长问题 titleBar.titleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12f) } PayUtil.addPayListener(this) initWeb(webView) initRule() } //初始化规格 private fun initRule() { HttpManager.getH5(15, Const.Language.CHINESE).request(this, showDialog = true) { _, data -> data?.optString("content")?.also { setContent(webView, it) } } } @SuppressLint("SetJavaScriptEnabled") private fun initWeb(webView: WebView) { webView.isHorizontalScrollBarEnabled = false //声明WebSettings子类 val webSettings = webView.settings webSettings.domStorageEnabled = true //设置适应Html5 //如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript webSettings.javaScriptEnabled = true //允许js调用 webSettings.allowContentAccess = true //是否允许在WebView中访问内容URL(Content Url),默认允许 //缩放操作 webSettings.setSupportZoom(false) //支持缩放,默认为true。是下面那个的前提。 webSettings.builtInZoomControls = false //设置内置的缩放控件。若为false,则该WebView不可缩放 webSettings.displayZoomControls = false //不显示缩放按钮 //其他细节操作 webSettings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK //关闭webview中缓存 webSettings.javaScriptCanOpenWindowsAutomatically = true //支持通过JS打开新窗口 webSettings.loadsImagesAutomatically = true //支持自动加载图片 webSettings.defaultTextEncodingName = "utf-8" //设置编码格式 webView.setSafeSetting()//安全设置 } @SuppressLint("SetJavaScriptEnabled") private fun setContent(webView: WebView, content: String?) { content?.let { // 加载并显示HTML代码 webView.safeLoadDataWithBaseURL( null, content, "text/html", "utf-8", null ) } } private fun createOrder() { val currentLocation = IGoApplication.getCurrentLocation() HttpManager.smallLogistics( carTypeId, "$endLon,$endLat", goodsNum, goodsType, receiveAddress ?: "", currentLocation.latitude, currentLocation.longitude, receiveName ?: "", receivePhone ?: "", remark ?: "", startName ?: "", startLat, startLon, fastMoney, System.currentTimeMillis().toTime(), type, urgent ).request( this, showToast = { code, _ -> code != 10001 }, showDialog = true, success = { _, data -> data?.run { id = optInt("id") getOrderInfo() } }) { code, _ -> if (code == 10001) { showHadNotCompleteOrderDialog() } } } //显示有未完成订单弹窗 private fun showHadNotCompleteOrderDialog() { val tipDialog = TipDialog() tipDialog.arguments = bundleOf( "msg" to getString(R.string.有未完成订单是否前往查看), "ok" to getString(R.string.前往), "cancel" to getString(R.string.取消), "isNotifyRed" to true ) tipDialog.setCallback(object : TipDialog.OnClickCallback { override fun onOk() { startActivity() } override fun onCancel() { } }) tipDialog.showAllowingStateLoss(supportFragmentManager, "HadNotCompleteOrderDialog") } private fun getOrderInfo() { HttpManager.queryOrderInfo(id, type).request(this, showDialog = true) { _, data -> order = data queryBalance() } } /** * 查询余额和优惠券以供支付 */ private fun queryBalance() { HttpManager.queryBalance(id, type).request(this, showDialog = true) { _, data -> data?.let { isCancel = false balance = it.optDouble("balance") coupon = it.optInt("coupon") redTotal = it.optDouble("redTotal") deductionAmount = it.optDouble("deductionAmount") startPay() } } } private fun startPay() { val payDialog = PayDialog() payDialog.arguments = bundleOf( "isCancel" to false, "balance" to balance, "coupon" to coupon, "money" to order!!.getOrderMoneyValue(), "id" to id, "type" to type, "isFrozen" to (order?.isDispute == 1), "discount" to order?.discountMoney, "redTotal" to redTotal, "deductionAmount" to deductionAmount ) payDialog.setCallback(object : PayDialog.Callback { override fun onOk(way: Int, couponId: Int, useRed: Boolean) { if (way == 2) { val dialog = ChooseCardDialog() dialog.setCallback(object : StringCallback { override fun onRlt(rlt: String) { pay(way, couponId, rlt, useRed) } }) dialog.show(supportFragmentManager, "card") } else { pay(way, couponId, null, useRed) } payDialog.dismiss() } override fun onClose() { } }) payDialog.show(supportFragmentManager, "pay") } /** * @param way 1=微信,2=支付宝,3=余额,4=现金 */ private fun pay(way: Int, couponId: Int, cardId: String?, useRed: Boolean) { tv_action.isEnabled = false HttpManager.payTaxiOrder( id, way, type, if (couponId == 0) null else couponId, cardId, if (useRed) 1 else 0 ).request(this, showDialog = true, success = { _, data -> tv_action.isEnabled = true if (way in 3..4) { if (type == 4) { Log.e("当前订单详情: ", "小件物流支付成功进入下个流程 ") queryTripping() } onPaySuccess() } else { isGotoWx = true toBrowser(data.toString()) } }) { _, _ -> tv_action.isEnabled = true } } /** * 查询是否有进行中的订单 */ fun queryTripping() { showDialog() HttpManager.queryServingOrder().request(this, false, showDialog = true) { _, data -> data?.let { if (it.isNotEmpty()) { if (order?.state == 7 && order?.orderType == Const.OrderType.TYPE_CROSS_CITY) { startActivity("id" to order?.orderId) } finish() } } } } override fun onPaySuccess() { myToast(getString(R.string.支付成功)) // startActivity("id" to id, "type" to type) LogHelper.logRecord("快件支付成功orderId:${order?.orderId} orderType:${order?.orderType}") startActivity( "id" to order?.orderId, "type" to order?.orderType ) finish() } override fun onPayCancel() { } override fun onResume() { super.onResume() if (isGotoWx) { isGotoWx = false HttpManager.queryOrderInfo(id, type).request(this, showDialog = true) { _, data -> if (data?.state == order?.state) { myToast(getString(R.string.支付失败)) } else { onPaySuccess() } } } } override fun onDestroy() { super.onDestroy() PayUtil.removePayListener(this) } }