Browse Source

@fun: tool

bai 3 years ago
parent
commit
0af3aaa38f

+ 22 - 0
app/src/main/java/com/ys/bdtp/adm/app/tools/Files.kt

@@ -0,0 +1,22 @@
+package com.ys.bdtp.adm.app.tools
+
+import okhttp3.MediaType.Companion.toMediaType
+import okhttp3.RequestBody
+import okhttp3.RequestBody.Companion.toRequestBody
+import java.io.File
+import java.io.FileInputStream
+
+/**
+ * 读取文件,返回字节数组
+ */
+fun File.toBytes(): ByteArray {
+    val fis = FileInputStream(this)
+    return fis.readBytes()
+}
+
+fun File.toRequestBody(
+    mediaType: String = "application/json;charset=utf-8"
+): RequestBody {
+    val bytes = this.toBytes()
+    return bytes.toRequestBody(mediaType.toMediaType())
+}

+ 43 - 0
app/src/main/java/com/ys/bdtp/adm/app/tools/Md5.kt

@@ -0,0 +1,43 @@
+package com.ys.bdtp.adm.app.tools
+
+import java.io.File
+import java.io.InputStream
+import java.security.MessageDigest
+
+/**
+ * MD5
+ */
+fun md5(byteArray: ByteArray): String {
+    val digest = MessageDigest.getInstance("md5")
+    val bytes = digest.digest(byteArray)
+    var md5 = ""
+    for (b in bytes) {
+        val hex = Integer.toHexString(0xFF and b.toInt())
+        if (hex.length == 1) {
+            md5 += "0"
+        }
+        md5 += hex
+    }
+    return md5
+}
+
+/**
+ * 文件 md5
+ */
+fun fileMD5(file: File): String {
+    return md5(file.toBytes())
+}
+
+/**
+ * 字符串 md5
+ */
+fun String.md5(): String {
+    return md5(this.toByteArray())
+}
+
+fun streamMD5(stream: InputStream): String {
+    return md5(stream.readBytes())
+}
+
+
+

+ 1 - 1
app/src/main/java/com/ys/bdtp/adm/app/Tools.kt

@@ -1,4 +1,4 @@
-package com.ys.bdtp.adm.app
+package com.ys.bdtp.adm.app.tools
 
 import android.net.ConnectivityManager
 import android.net.NetworkCapabilities

+ 6 - 3
app/src/main/java/com/ys/bdtp/adm/mvvm/model/db/dao/task/CadDao.kt

@@ -15,12 +15,15 @@ import com.ys.bdtp.adm.mvvm.model.db.entity.task.CadEntity
 interface CadDao {
 
     @Query("select * from cad")
-    fun getCads(): List<CadEntity>
+    suspend fun getCads(): List<CadEntity>
+
+    @Query("select * from cad where id = :id")
+    suspend fun getCadById(id: String): CadEntity
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    fun insCad(vararg cad: CadEntity)
+    suspend fun insCad(vararg cad: CadEntity)
 
     @Insert(onConflict = OnConflictStrategy.REPLACE)
-    fun insCads(cads: List<CadEntity>)
+    suspend fun insCads(cads: List<CadEntity>)
 
 }

+ 21 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/model/nt/Api.kt

@@ -5,6 +5,7 @@ import com.ys.bdtp.adm.mvvm.model.vo.*
 import com.ys.bdtp.adm.mvvm.model.vo.request.Account
 import com.ys.bdtp.adm.mvvm.model.vo.request.Login
 import retrofit2.http.Body
+import retrofit2.http.Headers
 import retrofit2.http.POST
 
 /**
@@ -47,12 +48,19 @@ interface Api {
     suspend fun getBuildingsAndFloors(@Body map: Map<String, String>): Model<BuildingsAndFloors>
 
     /**
-     * 获取建筑数据
+     * 下载任务数据
      */
     @POST("adm/download")
     suspend fun getMission(@Body map: Map<String, String>): Model<Mission>
 
     /**
+     * 上传任务数据
+     */
+    @Headers("Content-Encoding: gzip")
+    @POST("adm/upload")
+    suspend fun uploadMission(@Body mission: Mission)
+
+    /**
      * 获取关系数据
      */
     @POST("adm/downloadObjRel")
@@ -64,5 +72,17 @@ interface Api {
     @POST("adm/hasUpdate")
     suspend fun hasUpdate(): Model<Boolean>
 
+    /**
+     * 文件上传
+     */
+    @POST("adm/uploadFile")
+    suspend fun uploadFile()
+
+    /**
+     * 下载地图
+     */
+    @POST("adm/downloadMap")
+    suspend fun downloadFile()
+
 
 }

+ 1 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/flash/Flash.kt

@@ -17,7 +17,7 @@ import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.sp
 import com.ys.bdtp.adm.R
 import com.ys.bdtp.adm.app.Token
-import com.ys.bdtp.adm.app.isNet
+import com.ys.bdtp.adm.app.tools.isNet
 import com.ys.bdtp.adm.app.theme.textBlackColor
 import com.ys.bdtp.adm.mvvm.ui.screen.home.LocalNavHostController
 import com.ys.bdtp.adm.mvvm.ui.screen.home.Route

+ 1 - 1
app/src/main/java/com/ys/bdtp/adm/mvvm/ui/screen/project/Project.kt

@@ -8,7 +8,7 @@ import androidx.compose.runtime.*
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
-import com.ys.bdtp.adm.app.isNet
+import com.ys.bdtp.adm.app.tools.isNet
 import com.ys.bdtp.adm.app.theme.underColor
 import com.ys.bdtp.adm.mvvm.model.FailureResponse
 import com.ys.bdtp.adm.mvvm.model.SuccessResponse