SplashVm.kt 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package com.swago.app
  2. import android.annotation.SuppressLint
  3. import android.app.Application
  4. import android.content.Intent
  5. import android.os.Handler
  6. import android.os.Message
  7. import androidx.lifecycle.MutableLiveData
  8. import com.alibaba.android.arouter.launcher.ARouter
  9. import com.google.gson.Gson
  10. import com.swago.baseswago.constant.ARouteConstant
  11. import com.swago.baseswago.constant.UrlConstant
  12. import com.swago.baseswago.http.RetrofitManager
  13. import com.swago.baseswago.inter.ApiManager
  14. import com.swago.baseswago.model.DomainConfig
  15. import com.swago.baseswago.util.*
  16. import org.jsoup.Jsoup
  17. import java.lang.Exception
  18. /**
  19. *@date 2022/2/26 11:33
  20. *description:
  21. */
  22. class SplashVm(application: Application) : BaseViewModel(application) {
  23. private val gson by lazy {
  24. Gson()
  25. }
  26. val jumpToLoginLiveData by lazy {
  27. MutableLiveData<Boolean>()
  28. }
  29. var retry = 0
  30. private val executorService by lazy {
  31. ThreadPoolUtils.getInstance().executorService
  32. }
  33. private val mHandler = @SuppressLint("HandlerLeak")
  34. object : Handler() {
  35. override fun handleMessage(msg: Message) {
  36. if (UserInfo.getLoginModel() == null) {
  37. jumpToLoginLiveData.value = false
  38. } else {
  39. loginByAuto()
  40. }
  41. }
  42. }
  43. fun getApiDomain() {
  44. requestData2(false) {
  45. requestData {
  46. val domainUrl = ApiManager.configApi.getApiDomain()
  47. setBaseUrl(domainUrl.host)
  48. RetrofitManager.resetUrl()
  49. val data = ApiManager.configApi.getAppConfig()
  50. SpUtil.putString("configModel", gson.toJson(data))
  51. UrlConstant.setAgoraId(data.agoral_app_id)
  52. UrlConstant.setImId(data.im_app_id)
  53. UrlConstant.lottieDownloadUrl = data.special_zip_url
  54. if (UserInfo.getLoginModel() == null) {
  55. jumpToLoginLiveData.value = false
  56. } else {
  57. loginByAuto()
  58. }
  59. }
  60. requestError {
  61. retry++
  62. if (BuildConfig.DEBUG) {
  63. getHostUrl("https://github.com/whyNotBanMagna/MvpProject/blob/master/README.md")
  64. } else {
  65. when (retry) {
  66. 1 -> {
  67. getApiDomain()
  68. }
  69. 2 -> {
  70. getHostUrl("https://github.com/whyNotBanMagna/MvpProject/blob/master/README.md")
  71. }
  72. 3 -> {
  73. getHostUrl("https://github.com/mark-wby/pro/blob/main/README.md")
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. private fun getHostUrl(url: String) {
  81. executorService.execute {
  82. try {
  83. val document =
  84. Jsoup.connect(url)
  85. .timeout(2000).get()
  86. val list = document.text().split(" ")
  87. list.forEach {
  88. if (it.length == 32) {
  89. setBaseUrl(MD5Util.decodeBase64(it))
  90. RetrofitManager.resetUrl()
  91. mHandler.sendEmptyMessage(0)
  92. return@execute
  93. }
  94. }
  95. } catch (e: Exception) {
  96. e.printStackTrace()
  97. }
  98. }
  99. }
  100. private fun setBaseUrl(baseUrl: String) {
  101. if (BuildConfig.DEBUG) {
  102. // UrlConstant.BASE_URL = "http:test-api.swago.cn"
  103. UrlConstant.BASE_URL = "http:huawei.swago.cn"
  104. // UrlConstant.BASE_URL = "https://pro-api.swago.cn"
  105. } else {
  106. UrlConstant.BASE_URL = baseUrl
  107. }
  108. }
  109. fun loginByAuto() {
  110. requestData {
  111. val data = ApiManager.loginApi.loginByAuto()
  112. UserInfo.setLoginInfo(data)
  113. SpUtil.putString("login_info", Gson().toJson(data))
  114. SwagoLoading.cancelLoadingDialog()
  115. ARouter.getInstance().build(ARouteConstant.Home.home)
  116. .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)
  117. .navigation()
  118. }
  119. }
  120. /**
  121. * 获取配置
  122. */
  123. private fun getAppConfig(){
  124. requestData {
  125. }
  126. }
  127. }