feat(network): 优化网络异常处理和环境切换

- 新增网络异常统一处理逻辑
- 实现环境切换功能
- 更新 API接口
- 重构部分代码以提高可维护性
This commit is contained in:
songzhiling
2025-05-21 15:20:52 +08:00
parent 502e1cd604
commit f29cac2d73
39 changed files with 2450 additions and 1026 deletions

View File

@ -6,6 +6,8 @@ import com.blankj.utilcode.util.ThreadUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.JsonParseException
import com.za.base.Const
import com.za.base.view.NetWarnBean
import com.za.base.view.warnBean
import com.za.bean.BaseResponse
import com.za.common.GlobalData
import com.za.common.log.LogUtil
@ -32,6 +34,11 @@ abstract class BaseObserver<T> : Observer<BaseResponse<T>> {
override fun onNext(tBaseResponse : BaseResponse<T>) {
if (tBaseResponse.isOk) {
doSuccess(tBaseResponse.result)
ThreadUtils.runOnUiThread {
if (warnBean.value != null && warnBean.value is NetWarnBean) {
warnBean.value = null
}
}
} else {
when (tBaseResponse.code) {
3, 401 -> handlerTokenExpired()
@ -67,15 +74,18 @@ abstract class BaseObserver<T> : Observer<BaseResponse<T>> {
is ConnectException -> {
doFailure(Const.NetWorkException, "与服务器断开连接")
handlerNetError()
}
is UnknownHostException -> {
doFailure(Const.NetWorkException, "与服务器断开连接")
handlerNetError()
}
is SSLHandshakeException -> {
doFailure(1, "证书验证失败")
LogUtil.print("SSLHandshakeException", e)
handlerNetError()
}
is TimeoutException -> {
@ -86,6 +96,7 @@ abstract class BaseObserver<T> : Observer<BaseResponse<T>> {
is SocketTimeoutException -> {
doFailure(Const.NetWorkException, "网络连接超时2")
LogUtil.print("SocketTimeoutException2", e)
handlerNetError()
}
else -> {
@ -102,6 +113,15 @@ abstract class BaseObserver<T> : Observer<BaseResponse<T>> {
abstract fun doFailure(code : Int, msg : String?)
private fun handlerNetError() {
ThreadUtils.runOnUiThread {
if (warnBean.value == null) {
warnBean.value = NetWarnBean("当前网络异常")
}
}
}
private fun handlerTokenExpired() {
ThreadUtils.runOnUiThread {
try {