feat(global): 新增多环境切换弹窗及启动时自动读取已保存环境配置
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -41,10 +41,11 @@ object GlobalData {
|
||||
var unitTypeList: List<DictType> = arrayListOf()
|
||||
|
||||
/**
|
||||
* 具体业务baseurl
|
||||
* 具体业务 BaseUrl
|
||||
*/
|
||||
var testAppBaseUrl: String = "http://192.168.1.201:14801"
|
||||
var proAppBaseUrl: String = "https://dev.yixiong-tech.com:8081"
|
||||
const val TEST_BASE_URL = "http://192.168.1.201:14801"
|
||||
const val UAT_BASE_URL = "https://dev.yixiong-tech.com:8081"
|
||||
const val PROD_BASE_URL = "生产环境"
|
||||
var appBaseUrl: String = ""
|
||||
|
||||
/**
|
||||
@@ -67,4 +68,5 @@ object GlobalKey {
|
||||
const val KEY_TOKEN = "tokenKey"
|
||||
const val KEY_USER_INFO = "userInfoKey"
|
||||
const val KEY_USER_NAME = "userNameKey"
|
||||
const val KEY_BASE_URL = "baseUrlKey"
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import com.sw.inbound.databinding.ActivityHomeBinding
|
||||
import com.sw.inbound.dialog.EnvSwitchDialog
|
||||
import com.sw.inbound.model.response.User
|
||||
import com.sw.inbound.objbox.FoodModule
|
||||
import com.sw.inbound.sdk.SensorScaleUtils
|
||||
@@ -78,10 +79,10 @@ class HomeActivity : BaseActivity() {
|
||||
startActivity<FoodCollectionActivity>()
|
||||
}
|
||||
|
||||
// 时间点击:跳转趣味页面
|
||||
// binding.tvTime.setOnClickListener {
|
||||
// startActivity<FunActivity>()
|
||||
// }
|
||||
// 时间点击:打开环境切换弹窗
|
||||
binding.tvTime.setOnClickListener {
|
||||
EnvSwitchDialog(this).show()
|
||||
}
|
||||
|
||||
// 采购单入库入口
|
||||
binding.ivOrderPurchase.setOnClickListener {
|
||||
|
||||
@@ -9,9 +9,11 @@ import android.os.SystemClock
|
||||
import android.view.KeyEvent
|
||||
import android.view.animation.RotateAnimation
|
||||
import com.sw.inbound.GlobalData
|
||||
import com.sw.inbound.GlobalKey
|
||||
import com.sw.inbound.base.BaseActivity
|
||||
import com.sw.inbound.databinding.ActivityInitBinding
|
||||
import com.sw.inbound.utils.NetworkUtils
|
||||
import com.sw.inbound.utils.SPUtil
|
||||
|
||||
/**
|
||||
* 初始化页面 Activity
|
||||
@@ -138,13 +140,20 @@ class InitActivity : BaseActivity() {
|
||||
* 跳转到 HomeActivity
|
||||
*/
|
||||
private fun navigateToHome() {
|
||||
GlobalData.appBaseUrl = GlobalData.proAppBaseUrl
|
||||
val list = listOf(
|
||||
//"5a41a2cf-3cee-3731-911d-a28b10164f7a",
|
||||
"f0bcabbb-0d58-3b14-8bed-9863b7ec61ef"
|
||||
)
|
||||
if (list.contains(GlobalData.deviceId)) {
|
||||
GlobalData.appBaseUrl = GlobalData.testAppBaseUrl
|
||||
// 优先读取用户手动切换后持久化的 url
|
||||
val savedUrl = SPUtil.getInstance().get(GlobalKey.KEY_BASE_URL, "")
|
||||
if (!savedUrl.isNullOrEmpty()) {
|
||||
GlobalData.appBaseUrl = savedUrl
|
||||
} else {
|
||||
// 未保存过则使用默认逻辑:特定设备走测试环境,其余走 UAT
|
||||
GlobalData.appBaseUrl = GlobalData.UAT_BASE_URL
|
||||
val list = listOf(
|
||||
//"5a41a2cf-3cee-3731-911d-a28b10164f7a",
|
||||
"f0bcabbb-0d58-3b14-8bed-9863b7ec61ef"
|
||||
)
|
||||
if (list.contains(GlobalData.deviceId)) {
|
||||
GlobalData.appBaseUrl = GlobalData.TEST_BASE_URL
|
||||
}
|
||||
}
|
||||
val intent = Intent(this, HomeActivity::class.java)
|
||||
startActivity(intent)
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.sw.inbound.dialog
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import com.sw.inbound.GlobalData
|
||||
import com.sw.inbound.GlobalKey
|
||||
import com.sw.inbound.databinding.DialogEnvSwitchBinding
|
||||
import com.sw.inbound.utils.SPUtil
|
||||
import com.sw.inbound.utils.ext.dp
|
||||
import com.sw.inbound.utils.ext.toast
|
||||
|
||||
/**
|
||||
* 环境切换弹窗
|
||||
*
|
||||
* 提供 TEST / UAT / PROD 三套环境的切换功能,
|
||||
* 初始化时自动根据 [GlobalData.appBaseUrl] 选中当前环境,
|
||||
* 确认后更新 [GlobalData.appBaseUrl] 并通过 [onEnvChanged] 回调通知外部。
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param onEnvChanged 确认切换后的回调,参数为新的 baseUrl
|
||||
*/
|
||||
class EnvSwitchDialog(
|
||||
context: Context,
|
||||
private val onEnvChanged: (newBaseUrl: String) -> Unit = {}
|
||||
) : BaseDialog(
|
||||
context,
|
||||
defWidth = 600.dp,
|
||||
defHeight = WindowManager.LayoutParams.WRAP_CONTENT
|
||||
) {
|
||||
|
||||
private lateinit var binding: DialogEnvSwitchBinding
|
||||
|
||||
override fun getRootView(): View {
|
||||
binding = DialogEnvSwitchBinding.inflate(LayoutInflater.from(context))
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun initView() {
|
||||
// 根据当前 appBaseUrl 预选对应 RadioButton,不匹配则不选
|
||||
when (GlobalData.appBaseUrl) {
|
||||
GlobalData.TEST_BASE_URL -> binding.rbTest.isChecked = true
|
||||
GlobalData.UAT_BASE_URL -> binding.rbUat.isChecked = true
|
||||
GlobalData.PROD_BASE_URL -> binding.rbProd.isChecked = true
|
||||
}
|
||||
|
||||
// 取消按钮
|
||||
binding.btnCancel.setOnClickListener { dismiss() }
|
||||
|
||||
// 确认按钮
|
||||
binding.btnConfirm.setOnClickListener {
|
||||
// 未选中任何选项时提示用户
|
||||
if (binding.rgEnv.checkedRadioButtonId == -1) {
|
||||
context.toast("请选择环境")
|
||||
return@setOnClickListener
|
||||
}
|
||||
val newUrl = when (binding.rgEnv.checkedRadioButtonId) {
|
||||
binding.rbTest.id -> GlobalData.TEST_BASE_URL
|
||||
binding.rbUat.id -> GlobalData.UAT_BASE_URL
|
||||
binding.rbProd.id -> GlobalData.PROD_BASE_URL
|
||||
else -> return@setOnClickListener
|
||||
}
|
||||
GlobalData.appBaseUrl = newUrl
|
||||
SPUtil.getInstance().put(GlobalKey.KEY_BASE_URL, newUrl)
|
||||
onEnvChanged(newUrl)
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user