| | |
| | | package com.dayu.general.net |
| | | |
| | | import android.content.Context |
| | | import android.text.TextUtils |
| | | import com.dayu.baselibrary.business.BusinessProvider |
| | | import com.dayu.baselibrary.net.subscribers.BaseProgressSubscriber |
| | | import android.util.Log |
| | | import com.dayu.baselibrary.net.subscribers.ProgressSubscriber |
| | | import com.dayu.baselibrary.net.subscribers.SubscriberListener |
| | | import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers |
| | | import io.reactivex.rxjava3.core.Observable |
| | | import io.reactivex.rxjava3.functions.Function |
| | | import io.reactivex.rxjava3.schedulers.Schedulers |
| | | |
| | | /** |
| | | * Copyright (C), 2023, |
| | | * Author: zuot |
| | | * Date: 2023-04-12 9:11 |
| | | * Description: |
| | | * Author: zuo |
| | | * Date: 2025-03-06 |
| | | */ |
| | | class ApiManager { |
| | | class ApiManager private constructor() { |
| | | |
| | | var apiService: ApiService? = null |
| | | companion object { |
| | | private const val TAG = "ApiManager" |
| | | |
| | | fun init() { |
| | | if (apiManager == null) { |
| | | apiManager = |
| | | com.dayu.qiheonlinelibrary.net.ApiManager() |
| | | @Volatile |
| | | private var apiManager: ApiManager? = null |
| | | |
| | | fun getInstance(): ApiManager { |
| | | return apiManager ?: synchronized(this) { |
| | | apiManager ?: ApiManager().also { apiManager = it } |
| | | } |
| | | } |
| | | } |
| | | |
| | | fun ApiManager() { |
| | | apiService = RetrofitClient.getInstance().getApiService() |
| | | private val apiService: ApiService = RetrofitClient.getInstance().getApiService() |
| | | |
| | | fun <T> requestGetLoading( |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, false, path, true, tClass, params, listener) |
| | | } |
| | | |
| | | fun getInstance(): com.dayu.qiheonlinelibrary.net.ApiManager { |
| | | return apiManager |
| | | fun <T> requestGetHideLoading( |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, true, path, true, tClass, params, listener) |
| | | } |
| | | |
| | | |
| | | fun <T> requestPostLoading( |
| | | context: Context?, |
| | | path: String?, |
| | | tClass: Class<T>?, |
| | | params: Map<String?, Any?>?, |
| | | listener: SubscriberListener<*>? |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, false, path, false, tClass, params, listener) |
| | | } |
| | | |
| | | fun <T> requestPostHideLoading( |
| | | context: Context?, |
| | | path: String?, |
| | | tClass: Class<T>?, |
| | | params: Map<String?, Any?>?, |
| | | listener: SubscriberListener<*>? |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, true, path, false, tClass, params, listener) |
| | | } |
| | | |
| | | fun <T> requestPost( |
| | | context: Context?, |
| | | path: String?, |
| | | tClass: Class<T>?, |
| | | params: Map<String?, Any?>?, |
| | | listener: SubscriberListener<*>? |
| | | context: Context, |
| | | path: String, |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | request(context, false, path, false, tClass, params, listener) |
| | | } |
| | |
| | | * @param params Post请求时,对应的参数 |
| | | * @param listener 回调请求 |
| | | * @param <T> |
| | | </T> */ |
| | | */ |
| | | fun <T> request( |
| | | context: Context?, |
| | | context: Context, |
| | | hideLoading: Boolean, |
| | | path: String?, |
| | | path: String, |
| | | isGet: Boolean, |
| | | tClass: Class<T>?, |
| | | params: Map<String?, Any?>?, |
| | | listener: SubscriberListener<*>? |
| | | tClass: Class<T>, |
| | | params: Map<String, Any>?, |
| | | listener: SubscriberListener<BaseResponse<T>> |
| | | ) { |
| | | val observable: Observable<*>=if (isGet) { |
| | | if (params == null) { |
| | | apiService.requestGet(path) |
| | | } else { |
| | | apiService.requestGet(path, params) |
| | | } |
| | | |
| | | val observable = if (isGet) { |
| | | if (params == null) apiService.requestGet(path) |
| | | else apiService.requestGet(path, params) |
| | | } else { |
| | | if (params != null) { |
| | | apiService.requestPost(path, params) |
| | |
| | | } |
| | | } |
| | | |
| | | val mySubscriber: BaseProgressSubscriber<*>=ProgressSubscriber<Any?>(context, hideLoading, listener) |
| | | observable.subscribeOn(Schedulers.io()).map |
| | | object : Function<Any?, com.dayu.qiheonlinelibrary.net.BaseResponse<T>?> { |
| | | override fun apply(o: Any): com.dayu.qiheonlinelibrary.net.BaseResponse<T> { |
| | | if (o is com.dayu.qiheonlinelibrary.net.BaseResponse) { |
| | | val tem: com.dayu.qiheonlinelibrary.net.BaseResponse = |
| | | o as com.dayu.qiheonlinelibrary.net.BaseResponse |
| | | val response: com.dayu.qiheonlinelibrary.net.BaseResponse<T> = |
| | | com.dayu.qiheonlinelibrary.net.BaseResponse<T>() |
| | | //未登录或登录超时,请重新登录 |
| | | if (tem.getCode() == 100401) { |
| | | if (BusinessProvider.getBusinessProvider() != null) { |
| | | BusinessProvider.getBusinessProvider().startLoginNavigotor.navigateToLogin( |
| | | context |
| | | ) |
| | | } |
| | | } |
| | | response.setCode(tem.getCode()) |
| | | response.setMsg(tem.getMsg()) |
| | | if (tClass != null) { |
| | | if (TextUtils.isEmpty( |
| | | tem.getData().toString() |
| | | ) && BaseResult::class.java.isAssignableFrom(tClass) |
| | | ) { |
| | | response.setData(null) |
| | | return response |
| | | } |
| | | if (tem.getData() is Map<*, *>) { |
| | | try { |
| | | // response.setData(MyJsonParser.getBeanFromMap((Map<String, Object>) tem.getData(), tClass)); |
| | | val jsonData: String = |
| | | MyJsonParser.getJsontoMap(tem.getData() as Map<*, *>) |
| | | response.setData(MyJsonParser.getBeanFromJson<T>(jsonData, tClass)) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | } else if (tem.getData() is List<*>) { |
| | | try { |
| | | response.setData( |
| | | MyJsonParser.getListByJson<T>( |
| | | MyJsonParser.getJsonbyList<Any>( |
| | | tem.getData() as List<*> |
| | | ), tClass |
| | | ) as T |
| | | ) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | } else if (tem.getData() is Int) { |
| | | response.setData(tem.getData() as T) |
| | | } else if (tem.getData() is Boolean) { |
| | | response.setData(tem.getData() as T) |
| | | } |
| | | if (tClass.name is String && tem.getData() is String) { |
| | | try { |
| | | response.setData(tem.getData() as T) |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | } |
| | | } |
| | | return response |
| | | } |
| | | } |
| | | // 使用BaseResponse<T>作为ProgressSubscriber的泛型类型 |
| | | val mySubscriber = ProgressSubscriber<BaseResponse<T>>(context, hideLoading, listener); |
| | | |
| | | return null |
| | | } |
| | | } |
| | | observable |
| | | .subscribeOn(Schedulers.io()) |
| | | .map(mapResponse(tClass)) |
| | | .unsubscribeOn(Schedulers.newThread()) |
| | | .observeOn(AndroidSchedulers.mainThread()) |
| | | .subscribe(mySubscriber) |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | private fun <T> mapResponse(tClass: Class<T>): Function<Any, BaseResponse<T>> { |
| | | return Function { rawResponse -> |
| | | when (rawResponse) { |
| | | is BaseResponse<*> -> { |
| | | val temp = rawResponse |
| | | |
| | | // 创建新的响应对象并设置基本属性 |
| | | val response = BaseResponse<T>().apply { |
| | | code = temp.code |
| | | msg = temp.msg ?: "" |
| | | } |
| | | |
| | | // 处理token失效的情况 |
| | | if (temp.code == NetConstans.TOKEN_INVALID) { |
| | | // 可以在这里添加重定向到登录页面的逻辑 |
| | | // redirectToLogin() |
| | | return@Function response |
| | | } |
| | | |
| | | // 根据内容类型进行处理 |
| | | when (val content = temp.content) { |
| | | is Map<*, *> -> { |
| | | try { |
| | | val jsonData = MyJsonParser.getJsontoMap(content as Map<String, Any>) |
| | | response.content = MyJsonParser.getBeanFromJson(jsonData, tClass) |
| | | } catch (e: Exception) { |
| | | Log.e(TAG, "Error parsing map content", e) |
| | | } |
| | | } |
| | | is List<*> -> { |
| | | try { |
| | | @Suppress("UNCHECKED_CAST") |
| | | response.content = MyJsonParser.getListByJson( |
| | | MyJsonParser.getJsonbyList(content), |
| | | tClass |
| | | ) as T |
| | | } catch (e: Exception) { |
| | | Log.e(TAG, "Error parsing list content", e) |
| | | } |
| | | } |
| | | is Int, is String, is Boolean -> { |
| | | @Suppress("UNCHECKED_CAST") |
| | | response.content = content as T |
| | | } |
| | | } |
| | | |
| | | response |
| | | } |
| | | else -> throw IllegalArgumentException("Unexpected response type") |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |