初始代码提交
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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
|
||||
// } else {
|
||||
// "HTTP Error: ${e.code()} - ${e.message()}"
|
||||
// }
|
||||
ApiResponse(code = "${e.code()}", msg = e.message())
|
||||
}
|
||||
|
||||
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,57 @@
|
||||
//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 BaseRepositoryBak {
|
||||
// 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
|
||||
//// } else {
|
||||
//// "HTTP Error: ${e.code()} - ${e.message()}"
|
||||
//// }
|
||||
// ApiResponse(code = e.code(), message = e.message())
|
||||
// }
|
||||
//
|
||||
// is SocketTimeoutException -> {
|
||||
// ApiResponse(code = -2, message = "请求超时: ${e.message}")
|
||||
// }
|
||||
//
|
||||
// is ConnectException -> {
|
||||
// ApiResponse(code = -3, message = "连接失败: ${e.message}")
|
||||
// }
|
||||
//
|
||||
// is SSLHandshakeException -> {
|
||||
// ApiResponse(code = -4, message = "SSL握手失败: ${e.message}")
|
||||
// }
|
||||
//
|
||||
// is JsonParseException -> {
|
||||
// ApiResponse(code = -5, message = "JSON解析错误: ${e.message}")
|
||||
// }
|
||||
//
|
||||
// is IOException -> {
|
||||
// ApiResponse(code = -6, message = "网络IO错误: ${e.message}")
|
||||
// }
|
||||
//
|
||||
// else -> {
|
||||
// ApiResponse(code = -1, message = "未知错误: ${e.message ?: "无错误信息"}")
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,238 @@
|
||||
package com.sw.inbound.repository
|
||||
|
||||
import android.net.Uri
|
||||
import com.sw.inbound.model.request.GoodsAddParam
|
||||
import com.sw.inbound.model.request.GoodsPurchaseUnitParam
|
||||
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.EquipmentInfo
|
||||
import com.sw.inbound.model.response.GoodsType
|
||||
import com.sw.inbound.model.response.PurchaseInfo
|
||||
import com.sw.inbound.model.response.RecGoodsName
|
||||
import com.sw.inbound.model.response.SearchGoodsInfo
|
||||
import com.sw.inbound.model.response.StorageType
|
||||
import com.sw.inbound.model.response.SupplierInfo
|
||||
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.FileUtils
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.MultipartBody
|
||||
import okhttp3.RequestBody
|
||||
import okhttp3.RequestBody.Companion.asRequestBody
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* 远程数据处理
|
||||
*/
|
||||
class RemoteRepository @Inject constructor(
|
||||
private val apiService: ApiService
|
||||
) : BaseRepository() {
|
||||
/**
|
||||
* 生成token
|
||||
*/
|
||||
suspend fun getDeviceToken(qrcodeId: String): ApiResponse<String> {
|
||||
return safeApiCall {
|
||||
apiService.getDeviceToken(
|
||||
qrcodeId
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备信息
|
||||
*/
|
||||
suspend fun getDeviceInfo(equipmentCode: String, token: String): ApiResponse<EquipmentInfo> {
|
||||
return safeApiCall {
|
||||
apiService.getDeviceInfo(
|
||||
equipmentCode = equipmentCode,
|
||||
token = token
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun login(loginParam: LoginParam): ApiResponse<User> {
|
||||
return safeApiCall { apiService.login(body = loginParam) }
|
||||
}
|
||||
|
||||
suspend fun getReceiveList(pageNum: Int, pageSize: Int): ApiResponse<List<SupplierInfo>?> {
|
||||
return safeApiCall {
|
||||
apiService.getReceiveList(
|
||||
param = mutableMapOf(
|
||||
"pageNum" to pageNum.toString(),
|
||||
"pageSize" to pageSize.toString(),
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getReceiveDetail(id: String): ApiResponse<PurchaseInfo> {
|
||||
return safeApiCall {
|
||||
apiService.getReceiveDetail(id = 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 = 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 =
|
||||
FileUtils.genRequestPart(context = ContextUtils.getAppContext(), imageUri = uri)
|
||||
if (part == null) {
|
||||
ApiResponse(code = "-1", msg = "解析图片失败")
|
||||
} else {
|
||||
//apiService.uploadImage(code = codeRequestBody, file = part)
|
||||
apiService.uploadImage(file = part)
|
||||
}
|
||||
}
|
||||
}
|
||||
suspend fun uploadImage(file:File): ApiResponse<String> {
|
||||
return safeApiCall {
|
||||
//val codeRequestBody = RequestBody.create(
|
||||
// MultipartBody.FORM,
|
||||
// "4"
|
||||
//)
|
||||
|
||||
val part = FileUtils.genRequestPart(file)
|
||||
if (part == null) {
|
||||
ApiResponse(code = "-1", msg = "解析图片失败")
|
||||
} else {
|
||||
//apiService.uploadImage(code = codeRequestBody, file = part)
|
||||
apiService.uploadImage(file = part)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 部分收货
|
||||
*/
|
||||
suspend fun partialReceipt(uploadInfo: UploadInfo): ApiResponse<Boolean> {
|
||||
return safeApiCall {
|
||||
apiService.partialReceipt(uploadInfo = uploadInfo)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 全部收货
|
||||
*/
|
||||
suspend fun confirmReceipt(uploadInfo: UploadInfo): ApiResponse<Boolean> {
|
||||
return safeApiCall {
|
||||
apiService.confirmReceipt(uploadInfo = uploadInfo)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun searchGoodsInfoList(
|
||||
goodsName: String,
|
||||
pageNo: Int = 1,
|
||||
pageSize: Int = 10
|
||||
): ApiResponse<SearchGoodsInfo> {
|
||||
return safeApiCall {
|
||||
val map = mutableMapOf(
|
||||
"goodsName" to goodsName,
|
||||
"pageNum" to pageNo.toString(),
|
||||
"pageSize" to pageSize.toString()
|
||||
)
|
||||
apiService.searchGoodsInfoList(param = map)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun recognizeGoodsList(nameList: List<String>): ApiResponse<List<SearchGoodsInfo.Record>> {
|
||||
return safeApiCall {
|
||||
apiService.recognizeGoodsList(recGoodsName = RecGoodsName(nameList))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自采购-添加商品
|
||||
*/
|
||||
suspend fun selfPurchaseGoodsAdd(goodsAddParam: GoodsAddParam): ApiResponse<String> {
|
||||
return safeApiCall {
|
||||
apiService.selfPurchaseGoodsAdd(goodsAddParam = goodsAddParam)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun selfPurchaseGoodsAddUnit(addParam: GoodsPurchaseUnitParam): ApiResponse<Boolean> {
|
||||
return safeApiCall {
|
||||
apiService.goodsAddUnit(purchaseUnitParam = addParam)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自采购-入库
|
||||
*/
|
||||
suspend fun selfPurchaseWarehousing(list: List<PurchaseWarehouseParam>): ApiResponse<Boolean> {
|
||||
return safeApiCall {
|
||||
apiService.selfPurchaseWarehousing(list = list)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun uploadMultipleImages(fileList: List<File>, params: HashMap<String, RequestBody>): ApiResponse<String> {
|
||||
// val paramsMap = hashMapOf<String, RequestBody>()
|
||||
// param.forEach { (key, value) ->
|
||||
// paramsMap[key] = value.toRequestBody("text/plain".toMediaTypeOrNull())
|
||||
// }
|
||||
|
||||
// 准备文件参数
|
||||
val fileParts = mutableListOf<MultipartBody.Part>()
|
||||
fileList.forEachIndexed { index, file ->
|
||||
val requestFile = file
|
||||
.asRequestBody("multipart/form-data".toMediaTypeOrNull())
|
||||
val filePart = MultipartBody.Part.createFormData(
|
||||
"goodsPics",
|
||||
file.name,
|
||||
requestFile
|
||||
)
|
||||
fileParts.add(filePart)
|
||||
}
|
||||
return safeApiCall {
|
||||
apiService.uploadMultipleImages(params = params, goodsPics = fileParts)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user