|
@@ -1,19 +1,23 @@
|
|
|
package com.framework.mvvm.mv
|
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
+import android.util.Log
|
|
|
import androidx.core.content.edit
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
import cn.sagacloud.cadengine.OkhttpUtil
|
|
|
import com.framework.app.DEVICE_ID
|
|
|
import com.framework.app.IP
|
|
|
+import com.framework.app.http.FileInfo
|
|
|
+import com.framework.app.http.FileUploadInfo
|
|
|
import com.framework.app.pathDownloadMap
|
|
|
import com.framework.app.timestamp
|
|
|
+import com.framework.app.tools.md5
|
|
|
+import com.framework.app.tools.toBytes
|
|
|
+import com.framework.mvvm.model.data.*
|
|
|
import com.framework.mvvm.model.db.entity.task.ObjectEntity
|
|
|
import com.framework.mvvm.model.repository.AdmRepository
|
|
|
import com.framework.mvvm.model.vo.AdmRequest
|
|
|
-import com.framework.mvvm.model.vo.FloorInfo
|
|
|
-import com.google.gson.Gson
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed
|
|
|
import kotlinx.coroutines.flow.StateFlow
|
|
@@ -21,6 +25,9 @@ import kotlinx.coroutines.flow.flow
|
|
|
import kotlinx.coroutines.flow.stateIn
|
|
|
import kotlinx.coroutines.launch
|
|
|
import kotlinx.coroutines.withContext
|
|
|
+import okhttp3.RequestBody
|
|
|
+import okhttp3.RequestBody.Companion.toRequestBody
|
|
|
+import java.io.File
|
|
|
|
|
|
|
|
|
class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
|
|
@@ -128,4 +135,49 @@ class AdmViewModel(private val repository: AdmRepository) : ViewModel() {
|
|
|
suspend fun getBuildings(): List<ObjectEntity> {
|
|
|
return repository.getBuildings()
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取文件上传信息
|
|
|
+ */
|
|
|
+ private suspend fun getFileUploadInfo(basic: FileInfo): Response<FileUploadInfo> {
|
|
|
+ return request { repository.getFileUploadInfo(basic) }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ */
|
|
|
+ private suspend fun uploadFile(url: String, file: RequestBody): String {
|
|
|
+ return repository.uploadFile(url, file)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传文件
|
|
|
+ */
|
|
|
+ fun fileUpload(file: File) {
|
|
|
+ val bytes = file.toBytes()
|
|
|
+
|
|
|
+ val md5 = md5(bytes)
|
|
|
+
|
|
|
+ val info = FileInfo(
|
|
|
+ fileMd5 = md5,
|
|
|
+ fileName = file.name,
|
|
|
+ fileSize = bytes.size
|
|
|
+ )
|
|
|
+
|
|
|
+ viewModelScope.launch(Dispatchers.IO) {
|
|
|
+ val response = getFileUploadInfo(info)
|
|
|
+ if (response is SuccessResponse) {
|
|
|
+ val data = response.data
|
|
|
+ val url = data.content?.get(0)?.uploadUrl ?: ""
|
|
|
+ if (url.isNotEmpty()) {
|
|
|
+ val body = bytes.toRequestBody()
|
|
|
+ val result = uploadFile(url, body)
|
|
|
+
|
|
|
+ Log.d("fileUpload", "success: $result")
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Log.d("fileUpload", "upload file has something error:$response")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|