package com.dayu.general.net
|
|
import android.content.Context
|
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.functions.Function
|
import io.reactivex.rxjava3.schedulers.Schedulers
|
|
/**
|
* Copyright (C), 2023,
|
* Author: zuot
|
* Date: 2023-04-12 9:11
|
* Description:
|
*/
|
class ApiManager private constructor() {
|
|
companion object {
|
private const val TAG = "ApiManager"
|
|
@Volatile
|
private var apiManager: ApiManager? = null
|
|
fun getInstance(): ApiManager {
|
return apiManager ?: synchronized(this) {
|
apiManager ?: ApiManager().also { apiManager = it }
|
}
|
}
|
}
|
|
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 <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<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<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<BaseResponse<T>>
|
) {
|
request(context, false, path, false, tClass, params, listener)
|
}
|
|
/**
|
* 发送请求
|
*
|
* @param context
|
* @param hideLoading 是否显示加载框 false:显示 true:隐藏
|
* @param path 请求路径,在UrlConfig中定义
|
* @param isGet 是否是Get请求 true:get 请求
|
* @param tClass 对应的数据类型
|
* @param params Post请求时,对应的参数
|
* @param listener 回调请求
|
* @param <T>
|
*/
|
fun <T> request(
|
context: Context,
|
hideLoading: Boolean,
|
path: String,
|
isGet: Boolean,
|
tClass: Class<T>,
|
params: Map<String, Any>?,
|
listener: SubscriberListener<BaseResponse<T>>
|
) {
|
|
val observable = if (isGet) {
|
if (params == null) apiService.requestGet(path)
|
else apiService.requestGet(path, params)
|
} else {
|
if (params != null) {
|
apiService.requestPost(path, params)
|
} else {
|
apiService.requestPost(path)
|
}
|
}
|
|
// 使用BaseResponse<T>作为ProgressSubscriber的泛型类型
|
val mySubscriber = ProgressSubscriber<BaseResponse<T>>(context, hideLoading, listener);
|
|
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 ?: ""
|
success = temp.success
|
}
|
|
// 处理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")
|
}
|
}
|
}
|
|
}
|