puzhibing
2026-08-21 5fa94b3718b4a78a4fcea4c97652b0ef20b7de3a
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package com.xw.yunwei.push
 
import android.app.Activity
import android.content.Intent
import android.view.View.inflate
import android.widget.ImageView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.blankj.utilcode.util.AppUtils
import com.xw.common.net.UpdateManager
import com.xw.common.net.interceptor.ApiException
import com.xw.common.utils.KeyPreferences
import com.xw.common.utils.LogUtils
import com.xw.common.utils.MMKVManager
import com.xw.common.utils.ToastUtilCommon
import com.xw.yunwei.MyApp
import com.xw.yunwei.R
import com.xw.yunwei.databinding.DialogProtocolBinding
import com.xw.yunwei.model.AgreeInfo
import com.xw.yunwei.model.SwitchTabMain
import com.xw.yunwei.model.SwitchTabTask
import com.xw.yunwei.net.TaskRepository
import com.xw.yunwei.net.UserRepository
import com.xw.yunwei.ui.MainActivity
import com.xw.yunwei.ui.task.TaskVM
import com.xw.yunwei.ui.task.accident.detail.AccidentDetailActivity
import com.xw.yunwei.ui.task.normal.NormalTaskDetailActivity
import com.xw.yunwei.ui.user.mine.protocol.ProtocolActivity
import com.xw.yunwei.ui.user.mine.scheduling.SchedulingActivity
import com.xw.yunwei.ui.work.car.detail.StandardDescriptionActivity
import com.xw.yunwei.utils.DialogFactory
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.io.IOException
import java.net.ConnectException
import java.net.SocketException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
 
object MessageHandler : KoinComponent {
    var TAG = "zmc-${this.javaClass.simpleName}"
    val userRepository: UserRepository by inject()
    val taskRepository: TaskRepository by inject()
 
    fun handlePushEvent(content: String?, isPush: Boolean = false) {
        try {
            handlePushEvent(JSONObject(content ?: ""), isPush)
        } catch (e: Exception) {
            ToastUtilCommon.showError("未获取到消息详情")
            LogUtils.e("zmc-MessageHandler", "handlePushEvent content = $content error = $e")
        }
    }
 
    fun handlePushEvent(map: Map<String?, String?>?, isPush: Boolean = false) {
        map?.let {
            handlePushEvent(map.toJSONObject(), isPush)
        }
    }
 
    @OptIn(DelicateCoroutinesApi::class)
    fun handlePushEvent(jsob: JSONObject, isPush: Boolean) {
        LogUtils.i("zmc-MessageHandler", "handlePushEvent $jsob isPush = $isPush")
        TaskVM.needRefresh.value = true
        /**
         * content:消息内容
         * strategy:推送策略 1=新增事故,2=事故即将超时提示,3=事故超时提示,4=排班信息变动,5=事故状态变更,6=新增任务,7=任务过期提示,8=版本更新提示,9=变更协议政策内容,10=变更协议内容并需用户授权,11=打卡成功通知, 12=强制升级
         * type:类型,事故需要,根据不同的id查询不同的事故详情 无-null 1=车辆检测 2=车辆充电 3=车辆整备 4=整备洗车 5=车辆维修 6=其他常规任务 7=事故任务
         * id:页面id(事故为事故id),无-null或0
         */
        val content = jsob.optString("content")
        val strategy = jsob.optInt("strategy")
        val type = jsob.optInt("type")
        val id = jsob.optString("id")
        handleMsg(strategy, id, isPush)
    }
 
    private fun handleMsg(
        strategy: Int,
        id: String,
        isPush: Boolean
    ) {
        runOnMainThread {
            val currentShowActivity = MyApp.myApp.getCurrentShowActivity()
            LogUtils.i(
                "zmc-MessageHandler", "handlePushEvent ${currentShowActivity.javaClass.name}"
            )
            when (strategy) {
                1, 2, 3, 5 -> { // 1=新增事故,2=事故即将超时提示,3=事故超时提示,5=事故状态变更 点击都跳事故详情
                    if (strategy == 5 && isPush) return@runOnMainThread
                    checkTaskCouldInDetail(id) {
                        if (!it) {
                            ToastUtilCommon.showSuc("事故处理完成,已归档")
                            return@checkTaskCouldInDetail
                        }
                        currentShowActivity.startActivity(
                            Intent(currentShowActivity, AccidentDetailActivity::class.java).apply {
                                putExtra("taskId", id.toLong())
                            })
                    }
                }
 
                4 -> { // 排班信息变动
                    currentShowActivity.startActivity(
                        Intent(currentShowActivity, SchedulingActivity::class.java)
                    )
                }
 
                6, 7 -> { // 6=新增任务,7=任务过期提示  点击都跳任务详情
                    if (isPush) return@runOnMainThread
                    if (strategy == 6 && id == "0") { // 多条任务 跳转任务中心待处理页面
                        EventBus.getDefault().post(SwitchTabMain(1))
                        EventBus.getDefault().post(SwitchTabTask(2))
                        currentShowActivity.startActivity(
                            Intent(
                                currentShowActivity,
                                MainActivity::class.java
                            )
                        )
                    } else {
                        currentShowActivity.startActivity(
                            Intent(
                                currentShowActivity,
                                NormalTaskDetailActivity::class.java
                            ).apply {
                                putExtra("taskId", id.toLong())
                            })
                    }
                }
 
                8, 12 -> { // 升级
                    if (strategy == 8 && isPush) return@runOnMainThread
                    showUpdateDialog(currentShowActivity)
                }
 
                9, 10 -> { // 变更协议内容
                    if (strategy == 9 && isPush) return@runOnMainThread
                    if (strategy == 9) {
                        currentShowActivity.startActivity(
                            Intent(currentShowActivity, ProtocolActivity::class.java)
                        )
                    } else {
                        MMKVManager.getInstance().saveBoolean(KeyPreferences.NEED_READ, true)
                        showProtocolDialog(currentShowActivity, true)
                    }
                }
            }
        }
    }
 
    //检测任务是否能够进入详情页
    private fun checkTaskCouldInDetail(taskId: String, block: (could: Boolean) -> Unit) {
        launchRequest {
            taskRepository.vehicleAccidentDetail(taskId)?.also {
                runOnMainThread {
                    block.invoke(it.status != 2)
                }
            }
        }
    }
 
    fun Map<String?, String?>.toJSONObject(): JSONObject {
        return JSONObject().apply {
            this@toJSONObject.forEach { (key, value) ->
                if (key != null) {
                    put(key, value ?: JSONObject.NULL)
                }
            }
        }
    }
 
    fun showUpdateDialog(currentShowActivity: Activity) {
        launchRequest {
            userRepository.getVersion()?.let {
                runOnMainThread {
                    UpdateManager.instance!!
                        .setContext(currentShowActivity)
                        .apkUrl(it.getDownloadUrl() ?: "")
                        .updateContent(it.updateDescription ?: "")
                        .updateVersionCode(it.versionCode ?: 1)
                        .updateVersionName(it.version ?: "1.0")
                        .updateForceUpdateFlag(it.forceUpdateFlag ?: 0)
                        .show()
                }
            }
        }
    }
 
    fun showProtocolDialog(
        currentShowActivity: Activity,
        forced: Boolean,
        dismissBlock: (() -> Unit)? = null
    ) {
        launchRequest {
            userRepository.getAgreeConfig()?.also { list ->
                runOnMainThread {
                    showProtocolDialog(currentShowActivity, forced, list, dismissBlock)
                }
            }
        }
    }
 
    //显示隐私弹窗
    private fun showProtocolDialog(
        currentShowActivity: Activity,
        forced: Boolean,
        list: List<AgreeInfo>,
        dismissBlock: (() -> Unit)? = null
    ) {
        DialogFactory.produce(currentShowActivity).cornerRadius(15f).cancelable(!forced).show {
            customView(
                com.xw.yunwei.R.layout.dialog_protocol,
                noVerticalPadding = true,
                dialogWrapContent = true
            )
            cornerRadius(8f)
            DataBindingUtil.bind<DialogProtocolBinding>(getCustomView())?.let { bind ->
                val checked = mutableSetOf<AgreeInfo>()
 
                fun refreshCheck(imageView: ImageView, checked: Boolean) {
                    imageView.setImageResource(if (checked) R.mipmap.icon_tick_circle_blue else R.mipmap.icon_circle_gray2)
                }
 
                fun checkAllChecked() {
                    (0..<bind.llAgree.childCount).forEachIndexed { index, i ->
                        val checked = checked.contains(list[index])
                        refreshCheck(
                            bind.llAgree.getChildAt(index).findViewById<ImageView>(R.id.iv_check),
                            checked
                        )
                    }
                    refreshCheck(bind.ivCheckAll, checked.size == list.size)
                }
 
                list.forEach { info ->
                    val itemView = inflate(
                        currentShowActivity,
                        R.layout.item_protocol,
                        null
                    ).apply {
                        findViewById<ImageView>(R.id.iv_check).apply {
                            setOnClickListener {
                                if (checked.contains(info)) {
                                    checked.remove(info)
                                } else {
                                    checked.add(info)
                                }
                                checkAllChecked()
                            }
                        }
                        findViewById<TextView>(R.id.tv_name).apply {
                            text = info.name
                            setOnClickListener {
//                                if (!checked.contains(info)) {
//                                    checked.add(info)
//                                    checkAllChecked()
//                                }
                                currentShowActivity.startActivity(
                                    Intent(
                                        currentShowActivity,
                                        StandardDescriptionActivity::class.java
                                    ).apply {
                                        putExtra("type", info.id?.toInt())
                                        putExtra("webType", 1)
                                        putExtra("title", info.name)
                                    }
                                )
                            }
                        }
                    }
                    bind.llAgree.addView(itemView)
                }
                bind.llCheckAll.setOnClickListener {
                    if (checked.size != list.size) {
                        checked.addAll(list)
                    } else {
                        checked.clear()
                    }
                    checkAllChecked()
                }
                bind.btnLeft.setOnClickListener {
                    dismiss()
                    if (forced) {
                        currentShowActivity.finishAffinity()
                        android.os.Process.killProcess(android.os.Process.myPid())
                    }
                }
                bind.btnRight.setOnClickListener {
                    if (checked.size != list.size) {
                        ToastUtilCommon.showError("请勾选全部协议后再确认同意")
                        return@setOnClickListener
                    }
                    MMKVManager.getInstance().saveBoolean(KeyPreferences.NEED_READ, false)
                    dismiss()
                }
 
            }
            setOnDismissListener { dismissBlock?.invoke() }
        }
    }
 
    private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
        LogUtils.e(TAG, "Uncaught coroutine exception: ${throwable.message}")
        runOnMainThread {
            ToastUtilCommon.showError(getNetworkErrorMessage(throwable))
        }
    }
 
    private suspend fun errorHandling(
        e: Throwable,
        error: suspend (code: Int, msg: String) -> Unit
    ) {
        if (e is ApiException) {
            when {
                e.isTokenExpired() -> {
                    MMKVManager.getInstance().clear()
                    ToastUtilCommon.showWarn("登录已过期,请重新登录")
                    CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
                        delay(1000)
                        AppUtils.relaunchApp(true)
                    }
                }
 
                else -> {
                    error.invoke(e.code, e.message ?: "")
                }
            }
        } else {
            val errorMsg = getNetworkErrorMessage(e)
            error.invoke(-1, errorMsg)
        }
    }
 
    /**
     * 获取网络异常的友好提示信息
     */
    private fun getNetworkErrorMessage(e: Throwable): String {
        return when (e) {
            is UnknownHostException -> "网络连接失败,请检查网络设置"
            is ConnectException -> "无法连接到服务器,请检查网络"
            is SocketTimeoutException -> "连接超时,请稍后重试"
            is SocketException -> "网络异常,请检查网络连接"
            is IOException -> "网络请求失败,请检查网络"
            else -> e.message ?: "未知错误"
        }
    }
 
    //运行在主线程
    private fun runOnMainThread(block: suspend () -> Unit) {
        CoroutineScope(Dispatchers.Main + exceptionHandler).launch {
            block()
        }
    }
 
    private fun launchRequest(
        error: suspend (code: Int, msg: String) -> Unit = { code, msg ->
            ToastUtilCommon.showError(msg)
            LogUtils.e(TAG, "launchRequest: code = $code msg = $msg")
        }, request: suspend () -> Unit
    ) {
        CoroutineScope(Dispatchers.IO + exceptionHandler).launch {
            try {
                request()
            } catch (e: Throwable) {
                try {
                    errorHandling(e, error)
                } catch (handlingError: Throwable) {
                    LogUtils.e(TAG, "Error handling failed: ${handlingError.message}")
                }
            }
        }
    }
}