Browse Source

v1.0.3开发:完善日志上报接口

#Suyghur 3 năm trước cách đây
mục cha
commit
e1c5edb73e

+ 4 - 3
demo/src/main/java/com/eyuangame/demo/DemoActivity.kt

@@ -20,6 +20,7 @@ import androidx.compose.material.Surface
 import androidx.compose.material.Text
 import androidx.compose.runtime.*
 import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
 import androidx.compose.ui.tooling.preview.Preview
 import androidx.compose.ui.unit.dp
 import cn.yyxx.eyuangame.base.EYuanGame
@@ -27,6 +28,7 @@ import cn.yyxx.eyuangame.base.entity.SdkChargeInfo
 import cn.yyxx.eyuangame.base.entity.SdkEvent
 import cn.yyxx.eyuangame.base.entity.SdkRoleInfo
 import cn.yyxx.eyuangame.base.internal.ICallback
+import cn.yyxx.eyuangame.core.network.SdkRequest
 import cn.yyxx.support.hawkeye.LogUtils
 import cn.yyxx.support.hawkeye.ToastUtils
 import com.eyuangame.demo.ui.theme.EYuanGameSdkKTXTheme
@@ -76,7 +78,7 @@ class DemoActivity : AppCompatActivity() {
 
         setContent {
             EYuanGameSdkKTXTheme {
-                Surface(color = MaterialTheme.colors.background) {
+                Surface(color = Color.White) {
                     DemoPage()
                 }
             }
@@ -192,8 +194,7 @@ class DemoActivity : AppCompatActivity() {
 
             })
             12 -> {
-                val zapPath = getExternalFilesDir("dolin/zap")!!.absolutePath
-                ZipUtils.zipAll(zapPath, "$zapPath/test.zip")
+                SdkRequest.instance.uploadLogFile(this)
             }
         }
     }

+ 0 - 69
demo/src/main/java/com/eyuangame/demo/ZipUtils.kt

@@ -1,69 +0,0 @@
-package com.eyuangame.demo
-
-import cn.yyxx.eyuangame.base.utils.Logger
-import java.io.*
-import java.util.zip.ZipEntry
-import java.util.zip.ZipOutputStream
-
-/**
- * @author #Suyghur.
- * Created on 2022/01/11
- */
-object ZipUtils {
-
-    fun zipAll(directory: String, zipFile: String) {
-        val sourceFile = File(directory)
-
-        ZipOutputStream(BufferedOutputStream(FileOutputStream(zipFile))).use {
-            it.use { zos ->
-                zipFiles(zos, sourceFile, "")
-            }
-        }
-    }
-
-    private fun zipFiles(zipOut: ZipOutputStream, sourceFile: File, parentDirPath: String) {
-
-        val data = ByteArray(2048)
-
-        for (f in sourceFile.listFiles()) {
-
-            if (f.isDirectory) {
-                val entry = ZipEntry(f.name + File.separator)
-                entry.time = f.lastModified()
-                entry.isDirectory
-                entry.size = f.length()
-
-                Logger.d("Adding Directory: " + f.name)
-                zipOut.putNextEntry(entry)
-
-                //Call recursively to add files within this directory
-                zipFiles(zipOut, f, f.name)
-            } else {
-
-                if (!f.name.contains(".zip")) { //If folder contains a file with extension ".zip", skip it
-                    FileInputStream(f).use { fi ->
-                        BufferedInputStream(fi).use { origin ->
-                            val path = parentDirPath + File.separator + f.name
-                            Logger.d("Adding file: $path")
-                            val entry = ZipEntry(path)
-                            entry.time = f.lastModified()
-                            entry.isDirectory
-                            entry.size = f.length()
-                            zipOut.putNextEntry(entry)
-                            while (true) {
-                                val readBytes = origin.read(data)
-                                if (readBytes == -1) {
-                                    break
-                                }
-                                zipOut.write(data, 0, readBytes)
-                            }
-                        }
-                    }
-                } else {
-                    zipOut.closeEntry()
-                    zipOut.close()
-                }
-            }
-        }
-    }
-}

+ 2 - 2
library_base/src/main/java/cn/yyxx/eyuangame/Version.kt

@@ -5,7 +5,7 @@ package cn.yyxx.eyuangame
  * Created on 2021/06/09
  */
 object Version {
-    const val VERSION_NAME = "1.0.2"
-    const val VERSION_CODE = 102
+    const val VERSION_NAME = "1.0.3"
+    const val VERSION_CODE = 103
     const val SERVER_VERSION = "1.0.0"
 }

+ 1 - 1
library_core/src/main/java/cn/yyxx/eyuangame/core/impl/login/LoginActivity.kt

@@ -115,7 +115,7 @@ class LoginActivity : FragmentActivity(), View.OnClickListener {
             autoLoginDialog = null
         }
 
-        session = SessionUtils.instance.getLocalLastSession(this)
+        session = SessionUtils.instance.getLocalLastSession()
 
         if (session == null) {
             showChooseDialog = true

+ 10 - 2
library_core/src/main/java/cn/yyxx/eyuangame/core/impl/login/UserSignInImpl.kt

@@ -175,13 +175,17 @@ class UserSignInImpl constructor(val activity: LoginActivity, private val callba
                             SdkBackLoginInfo.instance.phoneNum = jsonObject.getString("phone_num")
                             SdkBackLoginInfo.instance.loginType = LoginType.TYPE_ACCOUNT_LOGIN
 
+                            if (jsonObject.getInt("upload_log_auto") == 1) {
+                                SdkRequest.instance.uploadLogFile(context)
+                            }
+
                             with(Session()) {
                                 userId = SdkBackLoginInfo.instance.userId
                                 loginType = SdkBackLoginInfo.instance.loginType
                                 this.userName = userName
                                 this.pwd = pwd
                                 GlobalScope.launch(Dispatchers.IO) {
-                                    SessionUtils.instance.saveSession(context, this@with)
+                                    SessionUtils.instance.saveSession(this@with)
                                 }
                             }
                             callback.onSuccess(SdkBackLoginInfo.instance.toJsonString())
@@ -226,6 +230,10 @@ class UserSignInImpl constructor(val activity: LoginActivity, private val callba
                         SdkBackLoginInfo.instance.phoneNum = jsonObject.getString("phone_num")
                         SdkBackLoginInfo.instance.loginType = loginParams.getInt("login_type")
 
+                        if (jsonObject.getInt("upload_log_auto") == 1) {
+                            SdkRequest.instance.uploadLogFile(context)
+                        }
+
                         with(Session()) {
                             userId = SdkBackLoginInfo.instance.userId
                             loginType = SdkBackLoginInfo.instance.loginType
@@ -234,7 +242,7 @@ class UserSignInImpl constructor(val activity: LoginActivity, private val callba
                                 pwd = loginParams.getString("pwd")
                             }
                             GlobalScope.launch(Dispatchers.IO) {
-                                SessionUtils.instance.saveSession(context, this@with)
+                                SessionUtils.instance.saveSession(this@with)
                             }
                         }
                         callback.onSuccess(SdkBackLoginInfo.instance.toJsonString())

+ 2 - 2
library_core/src/main/java/cn/yyxx/eyuangame/core/impl/login/fragment/LauncherFragment.kt

@@ -170,7 +170,7 @@ class LauncherFragment : Fragment(), View.OnClickListener {
     }
 
     private fun autoFillUserInfo() {
-        val temp = SessionUtils.instance.getLocalSessionLimit3(requireActivity())
+        val temp = SessionUtils.instance.getLocalSessionLimit3()
         if (temp.size == 0) {
             //本地没有缓存账号,直接返回
             eetAccount.rightImageView.visibility = View.GONE
@@ -433,7 +433,7 @@ class LauncherFragment : Fragment(), View.OnClickListener {
 
     private inner class AccountDeleteClick(val session: Session) : View.OnClickListener {
         override fun onClick(v: View?) {
-            SessionUtils.instance.deleteUserInfo(requireActivity(), session.userId)
+            SessionUtils.instance.deleteUserInfo(session.userId)
             userLists.clear()
             autoFillUserInfo()
         }

+ 0 - 1
library_core/src/main/java/cn/yyxx/eyuangame/core/impl/share/ShareImpl.kt

@@ -60,7 +60,6 @@ class ShareImpl private constructor() {
         shareDialog?.show(shareLinkContent)
     }
 
-
     fun onShareResult(requestCode: Int, resultCode: Int, intent: Intent?) {
         fbCallback?.onActivityResult(requestCode, resultCode, intent)
     }

+ 29 - 8
library_core/src/main/java/cn/yyxx/eyuangame/core/network/SdkRequest.kt

@@ -4,14 +4,21 @@ import android.content.Context
 import cn.yyxx.eyuangame.base.entity.SdkChargeInfo
 import cn.yyxx.eyuangame.base.entity.SdkRoleInfo
 import cn.yyxx.eyuangame.base.utils.Logger
+import cn.yyxx.eyuangame.base.utils.ParamsUtils
 import cn.yyxx.eyuangame.core.entity.LoginType
+import cn.yyxx.eyuangame.core.entity.ResultInfo
 import cn.yyxx.eyuangame.core.entity.SdkBackLoginInfo
-import cn.yyxx.eyuangame.core.impl.SdkDrive
 import cn.yyxx.eyuangame.core.internal.IFileRequestCallback
 import cn.yyxx.eyuangame.core.internal.IRequestCallback
+import cn.yyxx.eyuangame.core.utils.zipAll
+import cn.yyxx.support.encryption.Md5Utils
 import cn.yyxx.support.volley.source.VolleyError
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
 import org.json.JSONException
 import org.json.JSONObject
+import java.io.File
 
 /**
  * @author #Suyghur.
@@ -171,13 +178,27 @@ class SdkRequest {
     }
 
 
-//    fun uploadLogFile(context: Context) {
-//        val params = HashMap<String, Any>()
-//        params["common"] = SdkDrive.instance.getComm(context)
-//        params["upload_type"] = "log"
-//        params[""]
-//        VolleyRequest.uploadFile(context)
-//    }
+    fun uploadLogFile(context: Context) {
+        val zapFolderPath = context.getExternalFilesDir("dolin/zap")!!.absolutePath
+        val dolinFolderPath = context.getExternalFilesDir("dolin")!!.absolutePath
+        GlobalScope.launch(Dispatchers.IO) {
+            File(zapFolderPath).zipAll("$dolinFolderPath/upload_file.zip")
+            val file = File("$dolinFolderPath/upload_file.zip")
+            val md5 = Md5Utils.getFileMD5(file)
+            val jsonObject = JSONObject()
+            jsonObject.put("gcp_code", ParamsUtils.getGcpCode(context))
+            jsonObject.put("game_code", ParamsUtils.getGameCode(context))
+            jsonObject.put("upload_type", "log")
+            jsonObject.put("upload_file_sign", md5)
+            jsonObject.put("uid", SdkBackLoginInfo.instance.userId)
+            val url = "${Host.HOST}/${Host.BASIC_ROUTE_UPLOAD_SDK_LOG}"
+            VolleyRequest.uploadFile(context, url, file, jsonObject, object : IRequestCallback {
+                override fun onResponse(resultInfo: ResultInfo) {
+                    Logger.d(resultInfo.toString())
+                }
+            })
+        }
+    }
 
     companion object {
         val instance: SdkRequest by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {

+ 40 - 6
library_core/src/main/java/cn/yyxx/eyuangame/core/network/VolleyRequest.kt

@@ -162,23 +162,57 @@ object VolleyRequest {
         VolleySingleton.getInstance(context.applicationContext).addToRequestQueue(context.applicationContext, request)
     }
 
-    fun uploadFile(context: Context, url: String, file: File, params: HashMap<String, Any>, callback: IRequestCallback) {
+    fun uploadFile(context: Context, url: String, file: File, jsonObject: JSONObject, callback: IRequestCallback) {
         if (!file.exists()) {
             Logger.e("${file.name}不存在请检查")
             return
         }
+        val resultInfo = ResultInfo()
+        resultInfo.code = -1
+        resultInfo.msg = "接口请求异常"
+
+        val time = System.currentTimeMillis().toString()
+        val randomKey = time + StrUtils.getRandomString(16)
+        val rawKey = Md5Utils.encodeByMD5(randomKey)
+        val aesKey = Md5Utils.encodeByMD5(rawKey + StrUtils.reverseString(rawKey))
+        val hexP = SdkDrive.instance.invokeJob(context, aesKey.substring(8, 24), jsonObject.toString())
+        val tmp = HexUtils.hexString2Bytes(hexP)
+        val p = Base64Utils.encode(tmp)
+
+        Logger.d("请求地址 : $url")
+        Logger.d("请求参数 : $jsonObject")
+        Logger.logHandler("请求地址 : $url\n")
+        Logger.logHandler("请求参数 : $jsonObject\n")
+        val params = HashMap<String, Any>()
+        params["p"]=p
+        params["ts"]=rawKey
+
         Logger.d("上传文件: ${file.name} ...")
         val fileEntity = FileEntity()
         fileEntity.mName = "upload_file"
         fileEntity.mFileName = file.name
         fileEntity.mFile = file
 
-        val request = MultipartRequest(url, params, fileEntity, {
-            Logger.d("upload file result: $it")
-        }) {
-            Logger.d("upload file onError: ${it.message}")
+        kotlin.runCatching {
+            val request = MultipartRequest(url, params, fileEntity, {
+                with(JSONObject(it)) {
+                    resultInfo.code = getInt("code")
+                    resultInfo.msg = getString("msg")
+                }
+                callback.onResponse(resultInfo)
+            }) {
+                if (TextUtils.isEmpty(it.localizedMessage)) {
+                    resultInfo.msg = it.localizedMessage!!
+                }
+                callback.onResponse(resultInfo)
+            }
+            VolleySingleton.getInstance(context.applicationContext).addToRequestQueue(context.applicationContext, request)
+        }.onFailure {
+            if (TextUtils.isEmpty(it.localizedMessage)) {
+                resultInfo.msg = it.localizedMessage!!
+            }
+            callback.onResponse(resultInfo)
         }
-        VolleySingleton.getInstance(context.applicationContext).addToRequestQueue(context.applicationContext, request)
     }
 
     private fun parseResponse(context: Context, data: JSONObject): String {

+ 9 - 17
library_core/src/main/java/cn/yyxx/eyuangame/core/utils/SessionUtils.kt

@@ -1,6 +1,5 @@
 package cn.yyxx.eyuangame.core.utils
 
-import android.content.Context
 import android.text.TextUtils
 import cn.yyxx.eyuangame.base.utils.Logger
 import cn.yyxx.eyuangame.core.entity.LoginType
@@ -19,8 +18,8 @@ import org.json.JSONObject
 class SessionUtils private constructor() {
 
     @Synchronized
-    fun saveSession(context: Context, currentSession: Session) {
-        val userList = getLocalSession(context)
+    fun saveSession(currentSession: Session) {
+        val userList = getLocalSession()
         userList.apply {
             if (size > 0) {
                 var isSessionExist = false
@@ -46,7 +45,6 @@ class SessionUtils private constructor() {
             for (item in userList) {
                 jsonArray.put(item.toJSONObject())
             }
-//            val filePath = FileUtils.getUserInfoFilePath(context)
             try {
                 jsonObject.put("info", jsonArray)
             } catch (e: JSONException) {
@@ -58,12 +56,11 @@ class SessionUtils private constructor() {
                 else -> "online"
             }
             MMKVUtils.instance.userKV.encode(keyName, jsonObject.toString())
-//            FileUtils.writeFile(jsonObject.toString(), filePath)
         }
     }
 
-    fun getLocalLastSession(context: Context): Session? {
-        val list = getLocalSession(context)
+    fun getLocalLastSession(): Session? {
+        val list = getLocalSession()
         if (list.size == 0) {
             return null
         }
@@ -72,8 +69,8 @@ class SessionUtils private constructor() {
         return session
     }
 
-    fun getLocalSessionLimit3(context: Context): MutableList<Session> {
-        val list = getLocalSession(context)
+    fun getLocalSessionLimit3(): MutableList<Session> {
+        val list = getLocalSession()
         list.let {
             if (it.size == 0) {
                 return mutableListOf()
@@ -96,14 +93,13 @@ class SessionUtils private constructor() {
         }
     }
 
-    private fun getLocalSession(context: Context): MutableList<Session> {
+    private fun getLocalSession(): MutableList<Session> {
         val keyName = when (Host.IP_MODEL) {
             HostModelUtils.ENV_ONLINE -> "online"
             HostModelUtils.ENV_TEST -> "test"
             else -> "online"
         }
         val json = MMKVUtils.instance.userKV.decodeString(keyName)
-//        val json = FileUtils.readFile(FileUtils.getUserInfoFilePath(context))
         return if (TextUtils.isEmpty(json)) {
             mutableListOf()
         } else {
@@ -112,8 +108,8 @@ class SessionUtils private constructor() {
     }
 
     @Synchronized
-    fun deleteUserInfo(context: Context, userId: String) {
-        val userList = getLocalSession(context)
+    fun deleteUserInfo(userId: String) {
+        val userList = getLocalSession()
         if (userList.size == 0) {
             return
         }
@@ -137,8 +133,6 @@ class SessionUtils private constructor() {
             jsonArray.put(item.toJSONObject())
         }
 
-//        val filePath = FileUtils.getUserInfoFilePath(context)
-
         try {
             jsonObject.put("info", jsonArray)
         } catch (e: JSONException) {
@@ -152,7 +146,6 @@ class SessionUtils private constructor() {
             else -> "online"
         }
         MMKVUtils.instance.userKV.encode(keyName, jsonObject.toString())
-//        FileUtils.writeFile(jsonObject.toString(), filePath)
     }
 
     private fun toList(json: String): MutableList<Session> {
@@ -192,7 +185,6 @@ class SessionUtils private constructor() {
     }
 
     companion object {
-
         val instance: SessionUtils by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
             SessionUtils()
         }

+ 56 - 0
library_core/src/main/java/cn/yyxx/eyuangame/core/utils/ZipUtils.kt

@@ -0,0 +1,56 @@
+package cn.yyxx.eyuangame.core.utils
+
+import java.io.*
+import java.util.zip.ZipEntry
+import java.util.zip.ZipOutputStream
+
+/**
+ * @author #Suyghur.
+ * Created on 2022/01/11
+ */
+
+fun File.zipAll(zipFile: String) {
+    ZipOutputStream(BufferedOutputStream(FileOutputStream(zipFile))).use {
+        it.use { zos ->
+            zos.zipFiles(this, "")
+        }
+    }
+}
+
+private fun ZipOutputStream.zipFiles(sourceFile: File, parentDirPath: String) {
+    val data = ByteArray(2048)
+    for (f in sourceFile.listFiles()) {
+        if (f.isDirectory) {
+            val entry = ZipEntry(f.name + File.separator)
+            entry.time = f.lastModified()
+            entry.isDirectory
+            entry.size = f.length()
+            putNextEntry(entry)
+            zipFiles(f, f.name)
+        } else {
+            // 过滤zip和cache
+            if (!f.name.contains(".zip")) {
+                FileInputStream(f).use { fi ->
+                    BufferedInputStream(fi).use { origin ->
+                        val path = parentDirPath + File.separator + f.name
+                        val entry = ZipEntry(path)
+                        entry.time = f.lastModified()
+                        entry.isDirectory
+                        entry.size = f.length()
+                        putNextEntry(entry)
+                        while (true) {
+                            val readBytes = origin.read(data)
+                            if (readBytes == -1) {
+                                break
+                            }
+                            write(data, 0, readBytes)
+                        }
+                    }
+                }
+            } else {
+                closeEntry()
+                close()
+            }
+        }
+    }
+}

BIN
libs/yyxx_support_1.0.2.jar