init
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package com.sw.inbound.repository
|
||||
|
||||
import com.google.gson.JsonParseException
|
||||
import com.sw.inbound.model.response.ApiResponse
|
||||
import retrofit2.HttpException
|
||||
import timber.log.Timber
|
||||
import java.io.IOException
|
||||
import java.net.ConnectException
|
||||
import java.net.SocketTimeoutException
|
||||
import javax.net.ssl.SSLHandshakeException
|
||||
|
||||
abstract class BaseRepository {
|
||||
suspend fun <T> safeApiCall(apiCall: suspend () -> ApiResponse<T>): ApiResponse<T> {
|
||||
return try {
|
||||
apiCall()
|
||||
} catch (e: Exception) {
|
||||
Timber.e("safeApiCall Exception: ${e.stackTraceToString()}")
|
||||
|
||||
when (e) {
|
||||
is HttpException -> {
|
||||
// 对于HTTP异常,尝试从响应体中获取错误信息
|
||||
val errorBody = e.response()?.errorBody()?.string()
|
||||
val errorMsg = if (!errorBody.isNullOrEmpty()) {
|
||||
// 可以尝试解析errorBody为JSON获取更详细的错误信息
|
||||
errorBody
|
||||
} else {
|
||||
"HTTP Error: ${e.code()} - ${e.message()}"
|
||||
}
|
||||
ApiResponse(code = e.code(), msg = errorMsg)
|
||||
}
|
||||
|
||||
is SocketTimeoutException -> {
|
||||
ApiResponse(code = -2, msg = "请求超时: ${e.message}")
|
||||
}
|
||||
|
||||
is ConnectException -> {
|
||||
ApiResponse(code = -3, msg = "连接失败: ${e.message}")
|
||||
}
|
||||
|
||||
is SSLHandshakeException -> {
|
||||
ApiResponse(code = -4, msg = "SSL握手失败: ${e.message}")
|
||||
}
|
||||
|
||||
is JsonParseException -> {
|
||||
ApiResponse(code = -5, msg = "JSON解析错误: ${e.message}")
|
||||
}
|
||||
|
||||
is IOException -> {
|
||||
ApiResponse(code = -6, msg = "网络IO错误: ${e.message}")
|
||||
}
|
||||
|
||||
else -> {
|
||||
ApiResponse(code = -1, msg = "未知错误: ${e.message ?: "无错误信息"}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package com.sw.inbound.repository
|
||||
|
||||
import android.net.Uri
|
||||
import com.sw.inbound.model.request.GoodsAddParam
|
||||
import com.sw.inbound.model.request.LoginParam
|
||||
import com.sw.inbound.model.request.PurchaseWarehouseParam
|
||||
import com.sw.inbound.model.request.UploadInfo
|
||||
import com.sw.inbound.model.response.ApiResponse
|
||||
import com.sw.inbound.model.response.DictType
|
||||
import com.sw.inbound.model.response.GoodsType
|
||||
import com.sw.inbound.model.response.PurchaseInfo
|
||||
import com.sw.inbound.model.response.SearchGoodsInfo
|
||||
import com.sw.inbound.model.response.StorageType
|
||||
import com.sw.inbound.model.response.SupplierResponse
|
||||
import com.sw.inbound.model.response.User
|
||||
import com.sw.inbound.network.api.ApiService
|
||||
import com.sw.inbound.utils.ContextUtils
|
||||
import com.sw.inbound.utils.ImageUtils
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
class RemoteRepository @Inject constructor(
|
||||
private val apiService: ApiService
|
||||
) : BaseRepository() {
|
||||
|
||||
suspend fun login(loginParam: LoginParam): ApiResponse<User> {
|
||||
return safeApiCall { apiService.login(loginParam) }
|
||||
}
|
||||
|
||||
suspend fun getReceiveList(pageNum: Int, pageSize: Int): ApiResponse<SupplierResponse> {
|
||||
return safeApiCall {
|
||||
apiService.getReceiveList(pageNum, pageSize)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getReceiveDetail(id: Int): ApiResponse<PurchaseInfo> {
|
||||
return safeApiCall {
|
||||
apiService.getReceiveDetail(id)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getGoodsStorageType(): ApiResponse<List<StorageType>> {
|
||||
return safeApiCall {
|
||||
apiService.getGoodsStorageType()
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getGoodsType(): ApiResponse<GoodsType> {
|
||||
return safeApiCall {
|
||||
apiService.getGoodsType()
|
||||
}
|
||||
}
|
||||
|
||||
enum class TypeEnum(val string: String) {
|
||||
/**
|
||||
* 仓库
|
||||
*/
|
||||
WAREHOUSE("warehouse"),
|
||||
|
||||
/**
|
||||
* 供应商
|
||||
*/
|
||||
SUPPLIER("supplier"),
|
||||
|
||||
/**
|
||||
* 单位
|
||||
*/
|
||||
UNIT("unit")
|
||||
}
|
||||
|
||||
suspend fun getDictType(type: TypeEnum): ApiResponse<List<DictType>> {
|
||||
return safeApiCall {
|
||||
apiService.getDictType(type.string)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
suspend fun uploadImage(uri: Uri): ApiResponse<String> {
|
||||
Timber.d("uploadImage uri = $uri")
|
||||
return safeApiCall {
|
||||
val codeRequestBody = RequestBody.create(
|
||||
MultipartBody.FORM,
|
||||
"4"
|
||||
)
|
||||
|
||||
val part =
|
||||
ImageUtils.genRequestPart(context = ContextUtils.getAppContext(), imageUri = uri)
|
||||
if (part == null) {
|
||||
ApiResponse(code = -1, msg = "解析图片失败", data = "")
|
||||
} else {
|
||||
apiService.uploadImage(codeRequestBody, file = part)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 部分收货
|
||||
*/
|
||||
suspend fun partialReceipt(uploadInfo: UploadInfo): ApiResponse<Boolean> {
|
||||
return safeApiCall {
|
||||
apiService.partialReceipt(uploadInfo)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全部收货
|
||||
*/
|
||||
suspend fun confirmReceipt(uploadInfo: UploadInfo): ApiResponse<Boolean> {
|
||||
return safeApiCall {
|
||||
apiService.confirmReceipt(uploadInfo)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun searchGoodsInfoList(
|
||||
goodsName: String,
|
||||
pageNo: Int,
|
||||
pageSize: Int
|
||||
): ApiResponse<SearchGoodsInfo> {
|
||||
return safeApiCall {
|
||||
apiService.searchGoodsInfoList(goodsName, pageNo, pageSize)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自采购-添加商品
|
||||
*/
|
||||
suspend fun selfPurchaseGoodsAdd(goodsAddParam: GoodsAddParam): ApiResponse<SearchGoodsInfo> {
|
||||
return safeApiCall {
|
||||
apiService.selfPurchaseGoodsAdd(goodsAddParam)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自采购-入库
|
||||
*/
|
||||
suspend fun selfPurchaseWarehousing(list: List<PurchaseWarehouseParam>): ApiResponse<Boolean> {
|
||||
return safeApiCall {
|
||||
apiService.selfPurchaseWarehousing(list)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user