| | |
| | | package com.dayu.qiheonlinelibrary.net |
| | | package com.dayu.general.net |
| | | |
| | | import android.content.Context |
| | | import android.util.Log |
| | | import com.dayu.baselibrary.business.BusinessProvider |
| | | import com.dayu.baselibrary.net.subscribers.ProgressSubscriber |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import com.dayu.general.net.ApiService |
| | | import com.dayu.general.net.BaseResponse |
| | | import com.dayu.general.net.MyJsonParser |
| | | import com.dayu.general.net.NetConstans |
| | | import com.dayu.general.net.RetrofitClient |
| | | import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers |
| | | import io.reactivex.rxjava3.functions.Function |
| | | import io.reactivex.rxjava3.schedulers.Schedulers |
| | |
| | | @Volatile |
| | | private var apiManager: ApiManager? = null |
| | | |
| | | fun init() { |
| | | if (apiManager == null) { |
| | | synchronized(ApiManager::class) { |
| | | if (apiManager == null) { |
| | | apiManager = ApiManager() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | fun getInstance(): ApiManager { |
| | | return apiManager ?: throw IllegalStateException("ApiManager not initialized") |
| | | return apiManager ?: synchronized(this) { |
| | | apiManager ?: ApiManager().also { apiManager = it } |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | val mySubscriber = ProgressSubscriber<BaseResponse<T>>(context, hideLoading, listener); |
| | | |
| | | observable |
| | | ?.subscribeOn(Schedulers.io()) |
| | | ?.map(mapResponse(tClass)) |
| | | ?.unsubscribeOn(Schedulers.newThread()) |
| | | ?.observeOn(AndroidSchedulers.mainThread()) |
| | | ?.subscribe(mySubscriber) |
| | | .subscribeOn(Schedulers.io()) |
| | | .map(mapResponse(context, tClass)) |
| | | .unsubscribeOn(Schedulers.newThread()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe(mySubscriber) |
| | | } |
| | | |
| | | private fun <T> mapResponse(tClass: Class<T>): Function<Any, BaseResponse<T>> { |
| | | private fun <T> mapResponse(context: Context, tClass: Class<T>): Function<Any, BaseResponse<T>> { |
| | | return Function { rawResponse -> |
| | | when (rawResponse) { |
| | | is BaseResponse<*> -> { |
| | |
| | | val response = BaseResponse<T>().apply { |
| | | code = temp.code |
| | | msg = temp.msg ?: "" |
| | | success = temp.success |
| | | } |
| | | |
| | | // 处理token失效的情况 |
| | | if (temp.code == NetConstans.TOKEN_INVALID) { |
| | | // 可以在这里添加重定向到登录页面的逻辑 |
| | | // redirectToLogin() |
| | | BusinessProvider.getBusinessProvider().startLoginNavigotor.navigateToLogin(context) |
| | | return@Function response |
| | | } |
| | | |