tanghao
2023-01-17 66d9ccf38814e8199b4ed2335c8c2d183b676380
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
package com.okgoincar.base
 
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.os.Looper
import cn.sinata.rxnetty.netStatus.NetUtils
import cn.sinata.xldutils.utils.toast
import com.google.gson.Gson
import com.okgoincar.bean.LoginBean
import com.okgoincar.netUtls.Api
import com.okgoincar.netUtls.callNet
import com.okgoincar.netUtls.getMapByAny
import com.okgoincar.netUtls.getToken
import com.okgoincar.slab.SlabLoginActivity
import com.okgoincar.slab.SlabMainActivity
import com.okgoincar.utils.Cache.CacheKey
import com.okgoincar.utils.Cache.CacheUtil
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivity
import java.lang.Exception
 
class MainBroadCastReceiver : BroadcastReceiver() {
    override fun onReceive(contexts: Context?, intent: Intent?) {
        if (intent == null) {
            toast("内容获取为空,广播接收消息错误")
            return
        }
        try {
            Handler(Looper.getMainLooper()).post {
                val b = intent.getBooleanExtra("isLogin", true)
                if (!b) {
                    loginOut(contexts)
                    CacheUtil.get().clear()
                    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 {
                            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("Ok退签")
        }
    }
 
    private fun toLoginActivity(context: Context?, driverCertificate: String?) {
        var map = getMapByAny()
        if (driverCertificate.isNullOrEmpty()){
            toast("资格证号:空")
            return
        }
        map["taxiAptitudeCard"] = driverCertificate
        callNet(context!!, Api.loginByTaxiAptitudeCard, map) {
            toast("登录成功")
            var bean = Gson().fromJson<LoginBean>(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 {
                var intent = Intent()
                intent.setClass(context, SlabMainActivity::class.java)
                intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                context.startActivity(intent)
            }
        }
    }
}