package com.liuzhou.callcar.driver.pad.base import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.Handler import android.os.Looper import android.util.Log import cn.sinata.rxnetty.NettyClient import cn.sinata.xldutils.utils.toast import com.google.gson.Gson import com.liuzhou.callcar.driver.pad.base.MyApplication.Companion.getInstance import com.liuzhou.callcar.driver.pad.bean.LoginBean import com.liuzhou.callcar.driver.pad.netUtls.Api import com.liuzhou.callcar.driver.pad.netUtls.callNet import com.liuzhou.callcar.driver.pad.netUtls.getMapByAny import com.liuzhou.callcar.driver.pad.netUtls.getToken import com.liuzhou.callcar.driver.pad.slab.SlabMainActivity import com.liuzhou.callcar.driver.pad.utils.Cache.CacheKey import com.liuzhou.callcar.driver.pad.utils.Cache.CacheUtil import org.greenrobot.eventbus.EventBus class MainBroadCastReceiver : BroadcastReceiver() { override fun onReceive(contexts: Context?, intent: Intent?) { MyApplication.upLog("收到广播,Action:${intent?.action}") Log.e("MoneyBroadCastReceiver", "收到广播,Action:${intent?.action}") if (intent == null) { toast("内容获取为空,广播接收消息错误") return } try { Handler(Looper.getMainLooper()).post { val b = intent.getBooleanExtra("isLogin", true) if (!b) { loginOut(contexts) CacheUtil.get().clear() NettyClient.getInstance().stopService() // getInstance()?.stopUpPoint() EventBus.getDefault().post(BaseEvent(BaseEvent.OUT_APP)) } else { if (getToken().isNullOrEmpty()) { Handler(Looper.getMainLooper()).post { // toast("开始登录") } var driverCertificate = intent.getStringExtra("driverCertificate") if (driverCertificate == null) { // toast("身份证获取失败") } else { MyApplication.upLog("广播参数:driverCertificate:${driverCertificate},开始登录") if (contexts == null) { // toast("上下文为空,使用全局跳转") toLoginActivity(MyApplication.getInstance(), driverCertificate) } else { toLoginActivity(MyApplication.getInstance(), driverCertificate) } } } } } } catch (e: Exception) { } } private fun loginOut(context: Context?) { var map = getMapByAny() callNet(context!!, Api.loginOut, map) { toast("柳州便民退签") } } private fun toLoginActivity(context: Context?, driverCertificate: String?) { val map = getMapByAny() if (driverCertificate.isNullOrEmpty()) { toast("资格证号:空") return } map["taxiAptitudeCard"] = driverCertificate callNet(context!!, Api.loginByTaxiAptitudeCard, map) { toast("登录成功") val bean = Gson().fromJson(it, LoginBean::class.java) CacheKey.putKeyStr("identification", driverCertificate) CacheKey.putKeyStr("appid", bean.data.appid) CacheKey.putKeyStr("token", bean.data.token) CacheKey.putKeyStr("userId", bean.data.id.toString()) CacheKey.putKeyStr("serverId", bean.data.serverId.toString()) CacheKey.putKeyStr("terminalName", bean.data.terminalId.toString()) Handler(Looper.getMainLooper()).post { val intent = Intent() intent.setClass(context, SlabMainActivity::class.java) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK context.startActivity(intent) } } } }