package com.fuban.user.ui.logistics
|
|
import android.animation.ObjectAnimator
|
import android.app.Activity
|
import android.content.Intent
|
import android.os.Bundle
|
import android.util.Log
|
import android.view.View
|
import androidx.core.os.bundleOf
|
import cn.sinata.xldutils.fragment.BaseFragment
|
import cn.sinata.xldutils.gone
|
import cn.sinata.xldutils.ioScheduler
|
import cn.sinata.xldutils.rxutils.ResultException
|
import cn.sinata.xldutils.utils.*
|
import cn.sinata.xldutils.visible
|
import com.amap.api.maps.AMap
|
import com.amap.api.maps.CameraUpdateFactory
|
import com.amap.api.maps.TextureMapView
|
import com.amap.api.maps.model.BitmapDescriptorFactory
|
import com.amap.api.maps.model.CameraPosition
|
import com.amap.api.maps.model.LatLng
|
import com.amap.api.maps.model.MyLocationStyle
|
import com.amap.api.services.core.LatLonPoint
|
import com.amap.api.services.poisearch.PoiResult
|
import com.amap.api.services.poisearch.PoiSearch
|
import com.autonavi.amap.mapcore.Inner_3dMap_location
|
import com.fuban.user.FBApplication
|
import com.fuban.user.R
|
import com.fuban.user.dialog.ChooseReserveTimeDialog
|
import com.fuban.user.dialog.PayDialog
|
import com.fuban.user.dialog.SingleWheelDialog
|
import com.fuban.user.dialog.TipDialog
|
import com.fuban.user.interfaces.StringCallback
|
import com.fuban.user.network.HttpManager
|
import com.fuban.user.network.requestByF
|
import com.fuban.user.ui.H5Activity
|
import com.fuban.user.ui.MainActivity
|
import com.fuban.user.ui.mine.OrderActivity
|
import com.fuban.user.ui.mine.TripOrderFragment
|
import com.fuban.user.ui.trip.ChoosePositionActivity
|
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 io.reactivex.Flowable
|
import io.reactivex.subscribers.DisposableSubscriber
|
import kotlinx.android.synthetic.main.fragment_logistics.*
|
import org.jetbrains.anko.sdk27.coroutines.onClick
|
import org.jetbrains.anko.support.v4.find
|
import org.jetbrains.anko.support.v4.runOnUiThread
|
import org.jetbrains.anko.support.v4.startActivity
|
import org.jetbrains.anko.support.v4.startActivityForResult
|
|
class LogisticsFragment:BaseFragment(), PayListener {
|
override fun contentViewId() = R.layout.fragment_logistics
|
|
var type = Const.OrderType.TYPE_SAME_CITY //默认为同城物流
|
|
private val DEFAULT_SCALE_LEVEL = 20f
|
private var startByChoose = false //true 通过选择出发地移动地图,false 通过手势拖动地图
|
private var startByLocal = false //true 通过复位按钮移动地图,false 通过手势拖动地图
|
|
//取件经纬度
|
private var startLon = 0.0
|
private var startLat = 0.0
|
private var startName = ""
|
//收件信息
|
private var receiveName = ""
|
private var receivePhone = ""
|
private var receiveRegion = ""
|
private var provinceIndex = 0
|
private var cityIndex = 0
|
private var regionIndex = 0
|
private var receiveAddress = ""
|
private var timeFormat = "" //取件时间:例 2020-09-17 21:00:00
|
|
private var isFirstOrder = false
|
//货物信息
|
private var goodsType = 0 //货物类型(1=普通货物,2=贵重货物)
|
private var goodsNum = 0 //货物数量
|
private var remark = ""
|
|
private var orderMoney = 0.0 //物流费用
|
private var fastMoney = 0 //加急费用
|
|
private val mMapView by lazy {
|
find<TextureMapView>(R.id.mMapView)
|
}
|
|
private val aMap by lazy {
|
mMapView.map
|
}
|
private val mainActivity by lazy {
|
activity as MainActivity
|
}
|
private val tipsDialog by lazy {
|
val dialog = SingleWheelDialog()
|
dialog.arguments = bundleOf("title" to "修改加急金额","subTitle" to "加急费用归司机所有,添加将提高应答率","data" to (1..20).map { "${it}元" })
|
dialog.setCallback(object :StringCallback{
|
override fun onRlt(rlt: String) {
|
tv_tips.text = rlt
|
fastMoney = rlt.substring(0,rlt.length-1).toInt()
|
}
|
})
|
dialog
|
}
|
private val successDialog by lazy {
|
val dialog = TipDialog()
|
dialog.arguments = bundleOf("msg" to "您已下单成功,我们将根据预约时间安排人员领取物品,请保持电话通畅!","isAlert" to true)
|
dialog.setDismissCallback(object :TipDialog.OnDismiss{
|
override fun onDismiss() {
|
startActivity<OrderActivity>("type" to TripOrderFragment.TYPE_LOGISTICS)
|
}
|
})
|
dialog
|
}
|
|
|
override fun onFirstVisibleToUser() {
|
initMap()
|
initClick()
|
PayUtil.addPayListener(this)
|
}
|
|
private fun initClick() {
|
tv_order_count.onClick {
|
if (mainActivity.checkLogin())
|
startActivity<OrderActivity>("type" to TripOrderFragment.TYPE_LOGISTICS)
|
}
|
//点击归位
|
iv_location.setOnClickListener {
|
startByLocal = true
|
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(FBApplication.lat, FBApplication.lon),DEFAULT_SCALE_LEVEL))
|
}
|
tv_start.onClick {
|
startActivityForResult<ChoosePositionActivity>(Const.RequestCode.LOGISTICS_START,"isLogistics" to true)
|
}
|
tv_receiver.onClick {
|
startActivityForResult<ReceiverActivity>(Const.RequestCode.LOGISTICS_RECEIVER,"name" to receiveName,
|
"phone" to receivePhone,"region" to receiveRegion,"address" to receiveAddress,"provinceIndex" to provinceIndex,
|
"cityIndex" to cityIndex,"regionIndex" to regionIndex)
|
}
|
tv_type.onClick {
|
if (startName.isEmpty()){
|
myToast("请选择取件地点")
|
return@onClick
|
}
|
if (receiveAddress.isEmpty()){
|
myToast("请完善收货人信息")
|
return@onClick
|
}
|
if (mainActivity.checkLogin())
|
startActivityForResult<GoodsTypeActivity>(Const.RequestCode.LOGISTICS_TYPE,"address" to receiveRegion+receiveAddress,
|
"lat" to startLat,"lon" to startLon,"type" to type)
|
}
|
tv_time.onClick {
|
val chooseReserveTimeDialog = ChooseReserveTimeDialog()
|
chooseReserveTimeDialog.arguments = bundleOf("title" to "设置取货时间")
|
chooseReserveTimeDialog.setCallback(object :ChooseReserveTimeDialog.Callback{
|
override fun onOk(s: String, formatS: String) {
|
tv_time.text = s
|
timeFormat = formatS
|
}
|
})
|
chooseReserveTimeDialog.show(fragmentManager!!,"timeLogistics")
|
}
|
tv_tips.onClick {
|
if (cb_fast.isChecked&&!tipsDialog.isAdded){
|
tipsDialog.show(fragmentManager!!,"tips")
|
}
|
}
|
tv_action.onClick {
|
if (timeFormat.isEmpty()){
|
myToast("请选择司机取货时间")
|
return@onClick
|
}
|
if (type == Const.OrderType.TYPE_SAME_CITY&&cb_fast.isChecked&&fastMoney == 0){
|
myToast("请选择加急费用")
|
return@onClick
|
}
|
if (isFirstOrder){
|
HttpManager.getH5(15).requestByF(this@LogisticsFragment){ _, data->
|
val url = data?.optString("content")?:""
|
startActivityForResult<H5Activity>(Const.RequestCode.LOGISTICS_RULE,"url" to url,"title" to "小件物流服务协议"
|
,"actionEnable" to true)
|
}
|
}else
|
createOrder()
|
}
|
cb_fast.setOnCheckedChangeListener { _, isChecked ->
|
if (!isChecked){
|
fastMoney = 0
|
tv_tips.text = ""
|
}
|
}
|
}
|
|
var savedInstanceState: Bundle? = null
|
override fun onCreate(savedInstanceState: Bundle?) {
|
super.onCreate(savedInstanceState)
|
this.savedInstanceState = savedInstanceState
|
}
|
|
private var locCount = 0L //定位次数
|
|
private fun initMap() {
|
mMapView.onCreate(savedInstanceState)
|
val myLocationStyle = MyLocationStyle()
|
myLocationStyle.strokeColor(resources.getColor(R.color.colorBrown))
|
myLocationStyle.radiusFillColor(resources.getColor(R.color.colorTransBrown))
|
myLocationStyle.interval(5000)
|
myLocationStyle.anchor(0.5f, 0.5f)
|
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER)//
|
myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_coordinate))
|
aMap.myLocationStyle = myLocationStyle//设置定位蓝点的Style
|
aMap.uiSettings.isMyLocationButtonEnabled = false//设置默认定位按钮是否显示,非必需设置。
|
aMap.uiSettings.setZoomInByScreenCenter(true)
|
aMap.uiSettings.isZoomControlsEnabled = false
|
aMap.uiSettings.isRotateGesturesEnabled = false
|
aMap.uiSettings.isTiltGesturesEnabled = false
|
aMap.isMyLocationEnabled = true
|
aMap.setOnMyLocationChangeListener {
|
it as Inner_3dMap_location
|
Log.e(Const.Tag,"物流:lat:"+it.latitude+",lon:"+it.longitude)
|
FBApplication.lat = it.latitude
|
FBApplication.lon = it.longitude
|
FBApplication.address = it.address
|
FBApplication.cityCode = it.extras.getString("adcode")?:""
|
if (locCount<1){
|
if (mainActivity.lat!=0.0)
|
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(mainActivity.lat,mainActivity.lon),DEFAULT_SCALE_LEVEL))
|
else
|
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLng(it.latitude,it.longitude),DEFAULT_SCALE_LEVEL))
|
}
|
locCount++
|
}
|
aMap.setOnCameraChangeListener(object : AMap.OnCameraChangeListener {
|
override fun onCameraChangeFinish(p0: CameraPosition?) {
|
if (!startByChoose&&iv_center_logistics.visibility == View.VISIBLE) {
|
startLat = p0?.target?.latitude ?: 0.0
|
startLon = p0?.target?.longitude ?: 0.0
|
getPoi(startLat, startLon)
|
}
|
startByChoose = false
|
startByLocal = false
|
val objectAnimator =
|
ObjectAnimator.ofFloat(iv_center_logistics, "translationY", 0f, -30f, 0f).setDuration(600)
|
val objectAnimator1 =
|
ObjectAnimator.ofFloat(tv_center, "translationY", 0f, -30f, 0f).setDuration(600)
|
objectAnimator.start()
|
objectAnimator1.start()
|
tv_center.text = "在这里领取"
|
}
|
|
override fun onCameraChange(p0: CameraPosition?) {
|
if (startByLocal)
|
tv_center.text = "正在获取定位"
|
}
|
})
|
}
|
|
private fun getOrderCount(){
|
if (mainActivity.checkLogin(false)){
|
HttpManager.queryLogisticsNumber().requestByF(this){_,data->
|
data?.apply {
|
tv_order_count.text = "我的订单(%d)".format(optInt("number"))
|
}
|
}
|
}else{
|
tv_order_count.text = "我的订单(%d)".format(0)
|
}
|
}
|
|
private fun createOrder(){
|
showDialog()
|
HttpManager.smallLogistics(goodsNum,goodsType,"$receiveRegion${receiveAddress}",FBApplication.lat,
|
FBApplication.lon,receiveName,receivePhone,remark,startName,startLat,startLon,fastMoney,timeFormat,type,
|
if (cb_fast.isChecked) 2 else 1).requestByF(this){_,data->
|
showDialog()
|
HttpManager.queryUserInfo().requestByF(this,true,{_,userInfo->
|
userInfo?.let {
|
val payDialog = PayDialog()
|
val payMoney =
|
if (type == Const.OrderType.TYPE_SAME_CITY && cb_fast.isChecked) orderMoney + fastMoney else orderMoney
|
payDialog.arguments = bundleOf("money" to payMoney,"isLogistics" to true,
|
"balance" to it.balance)
|
payDialog.setCallback(object : PayDialog.Callback{
|
override fun onOk(way: Int, couponId: Int) {
|
pay(way,data?.optInt("id")?:0)
|
payDialog.dismiss()
|
}
|
|
override fun onClose() {
|
onPayCancel()
|
}
|
})
|
payDialog.show(fragmentManager!!,"pay")
|
getOrderCount()
|
back()
|
}
|
}){_,_->
|
getOrderCount()
|
back()
|
}
|
}
|
}
|
|
/**
|
* @param way 1=微信,2=支付宝,3=余额
|
*/
|
private fun pay(way:Int,id:Int){
|
// if (way == 1){//微信支付,跳转微信小程序
|
// val api = WXAPIFactory.createWXAPI(activity, Const.WX_APP_ID)
|
// val req = WXLaunchMiniProgram.Req()
|
// req.userName = "gh_bc8f199f11ae" // 填小程序原始id
|
// req.path = "/pages/appPay/appPay?orderId=${id}&orderType=${type}&type=1" +
|
// "&userType=1&uid=${SPUtils.instance().getInt(Const.User.USER_ID)}&content="
|
// req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE// 可选打开 开发版,体验版和正式版
|
// api.sendReq(req)
|
// }else{
|
HttpManager.payTaxiOrder(id,way,4).requestByF(this){_,data->
|
if (way == 3)
|
onPaySuccess()
|
else if (way == 2)
|
PayUtil.aliPay(activity!!,data!!.orderString)
|
else if (way == 1){
|
startActivity<WXPayEntryActivity>("type" to 1,"data" to data)
|
}
|
}
|
// }
|
}
|
|
/**
|
* 返回
|
* @param isBackHome true:直接返回到首页
|
*/
|
fun back(isBackHome:Boolean = true) {
|
group_order.gone()
|
group_fast.gone()
|
iv_center_logistics.visible()
|
tv_center.visible()
|
tv_order_count.visible()
|
iv_count.visible()
|
iv_location.visible()
|
mainActivity.updateTitleVisibility(false,false)
|
mainActivity.updateLogisticsTitle(false)
|
mainActivity.updateTabVisibility(true)
|
aMap.uiSettings.isScrollGesturesEnabled = true
|
if (isBackHome){
|
tv_receiver.text = ""
|
receiveAddress = ""
|
receiveRegion = ""
|
receiveName = ""
|
receivePhone = ""
|
provinceIndex = 0
|
cityIndex = 0
|
regionIndex = 0
|
}
|
tv_type.text = ""
|
tv_start.isEnabled = true
|
tv_receiver.isEnabled = true
|
tv_type.isEnabled = true
|
tv_time.text = ""
|
timeFormat = ""
|
cb_fast.isChecked = false
|
tv_tips.text = ""
|
fastMoney = 0
|
}
|
|
/**
|
* 获取支付金额
|
*/
|
private fun getPrice(){
|
showDialog()
|
HttpManager.queryPayMoney(receiveRegion+receiveAddress,"$startLon,$startLat",
|
goodsNum,type).requestByF(this,false,{_,data->
|
data?.apply {
|
group_order.visible()
|
iv_center_logistics.gone()
|
tv_center.gone()
|
tv_order_count.gone()
|
iv_count.gone()
|
iv_location.gone()
|
tv_start.isEnabled = false
|
tv_receiver.isEnabled = false
|
tv_type.isEnabled = false
|
if (type == Const.OrderType.TYPE_SAME_CITY)
|
group_fast.visible()
|
mainActivity.updateTitleVisibility(false,true)
|
mainActivity.updateLogisticsTitle(true)
|
mainActivity.updateTabVisibility(false)
|
aMap.uiSettings.isScrollGesturesEnabled = false
|
orderMoney = optDouble(if (goodsType == 1) "ordinary" else "precious")
|
val format = "物流费用¥%.2f".format(orderMoney)
|
tv_price.text = SpanBuilder(format).color(activity!!,0,4,R.color.textColor)
|
.size(0,4,14).build()
|
}
|
}){_,_->
|
myToast("获取支付金额失败")
|
}
|
}
|
|
fun changeType(newType:Int){
|
if (newType != type){
|
type = newType
|
judgingTheCity()
|
}
|
}
|
|
/**
|
* 获取poi信息
|
*/
|
private var poiSearchDisposable: DisposableSubscriber<PoiResult>? = null
|
|
fun getPoi(lat: Double?, lng: Double?) {
|
if (lat == null || lng == null) {
|
return
|
}
|
//如果上一次还没处理完,取消订阅
|
if (poiSearchDisposable != null && !poiSearchDisposable!!.isDisposed) {
|
poiSearchDisposable?.dispose()
|
}
|
poiSearchDisposable = object : DisposableSubscriber<PoiResult>() {
|
override fun onComplete() {
|
|
}
|
|
override fun onNext(t: PoiResult?) {
|
val address = if (t != null) {
|
t.pois.sortBy { it.distance }
|
if (t.pois.isNotEmpty()) {
|
t.pois[0].title
|
} else {
|
null
|
}
|
} else {
|
null
|
}
|
runOnUiThread {
|
startName = address?:""
|
tv_start?.text = address
|
}
|
}
|
|
override fun onError(t: Throwable?) {
|
runOnUiThread {
|
startName = ""
|
tv_start.text = "点击选择起点"
|
}
|
}
|
}
|
val query = PoiSearch.Query("", "190000", "")
|
val poiSearch = PoiSearch(context, query)
|
poiSearch.bound = PoiSearch.SearchBound(LatLonPoint(lat, lng), 1000)
|
Flowable.just(poiSearch).ioScheduler().flatMap {
|
try {
|
val result = poiSearch.searchPOI()
|
if (result == null) {
|
Flowable.error(ResultException(""))
|
} else
|
Flowable.just(result)
|
} catch (e: Exception) {
|
e.printStackTrace()
|
Flowable.error<PoiResult>(e)
|
}
|
}.subscribe(poiSearchDisposable)
|
}
|
|
/**
|
* 判断同城小件物流是否是同城
|
*/
|
private fun judgingTheCity(){
|
if (type == Const.OrderType.TYPE_SAME_CITY&&startLat!=0.0&&receiveRegion.isNotEmpty()){
|
HttpManager.judgingTheCity(receiveRegion+receiveAddress,"$startLon,$startLat").requestByF(this,false,{_,_->
|
}){_,msg->
|
back()
|
val tipDialog = TipDialog()
|
tipDialog.arguments = bundleOf("msg" to msg,"isAlert" to true)
|
tipDialog.show(fragmentManager!!,"disable")
|
}
|
}
|
}
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
super.onActivityResult(requestCode, resultCode, data)
|
if (resultCode == Activity.RESULT_OK){
|
if (requestCode == Const.RequestCode.LOGISTICS_RULE){
|
createOrder()
|
}
|
if (data!=null)
|
when(requestCode) {
|
Const.RequestCode.LOGISTICS_START -> {
|
startLat = data.getDoubleExtra("lat", 0.0)
|
startLon = data.getDoubleExtra("lon", 0.0)
|
startByChoose = true
|
aMap.animateCamera(CameraUpdateFactory.newLatLng(LatLng(startLat, startLon)))
|
startName = data.getStringExtra("name") ?: ""
|
tv_start.text = startName
|
judgingTheCity()
|
}
|
Const.RequestCode.LOGISTICS_RECEIVER -> {
|
receiveName = data.getStringExtra("name") ?: ""
|
receivePhone = data.getStringExtra("phone") ?: ""
|
receiveRegion = data.getStringExtra("region") ?: ""
|
receiveAddress = data.getStringExtra("address") ?: ""
|
provinceIndex = data.getIntExtra("provinceIndex",0)
|
cityIndex = data.getIntExtra("cityIndex",0)
|
regionIndex = data.getIntExtra("regionIndex",0)
|
val receiver = "%s %s\n%s%s".format(
|
receiveName,
|
receivePhone,
|
receiveRegion,
|
receiveAddress
|
)
|
tv_receiver.text = SpanBuilder(receiver).color(
|
activity!!, receiver.length - receiveAddress.length - receiveRegion.length,
|
receiver.length, R.color.textColor99
|
).size(
|
receiver.length - receiveAddress.length - receiveRegion.length,
|
receiver.length, 12
|
).build()
|
judgingTheCity()
|
}
|
Const.RequestCode.LOGISTICS_TYPE->{
|
isFirstOrder = data.getBooleanExtra("isFirstOrder",false)
|
goodsType = data.getIntExtra("goodsType",0)
|
goodsNum = data.getIntExtra("goodsNum",0)
|
remark = data.getStringExtra("remark")?:""
|
tv_type.text = "%sX%d".format(if (goodsType == 1) "普通小件" else "普通大件",goodsNum)
|
getPrice()
|
}
|
}
|
}
|
}
|
|
override fun onResume() {
|
super.onResume()
|
mMapView.onResume()
|
getOrderCount()
|
}
|
|
override fun onPause() {
|
super.onPause()
|
mMapView.onPause()
|
}
|
|
override fun onDestroy() {
|
super.onDestroy()
|
mMapView.onDestroy()
|
PayUtil.removePayListener(this)
|
}
|
|
override fun onSaveInstanceState(outState: Bundle) {
|
super.onSaveInstanceState(outState)
|
mMapView.onSaveInstanceState(outState)
|
}
|
|
override fun onLowMemory() {
|
super.onLowMemory()
|
mMapView.onLowMemory()
|
}
|
|
override fun onPaySuccess() {
|
if (!successDialog.isAdded)
|
successDialog.show(fragmentManager!!,"suc")
|
}
|
|
override fun onPayCancel() {
|
startActivity<OrderActivity>("type" to TripOrderFragment.TYPE_LOGISTICS)
|
}
|
}
|