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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@
|
||||
android:id="@+id/tvTime"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingVertical="10dp"
|
||||
android:textColor="@color/black"
|
||||
android:textSize="24sp"
|
||||
android:visibility="visible"
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="600dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/bg_white_radius12"
|
||||
android:orientation="vertical"
|
||||
android:paddingHorizontal="40dp"
|
||||
android:paddingVertical="36dp">
|
||||
|
||||
<!-- 标题 -->
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center"
|
||||
android:text="环境切换"
|
||||
android:textColor="#ff141428"
|
||||
android:textSize="36sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<!-- 分割线 -->
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="2dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:background="#FFDCDCF0" />
|
||||
|
||||
<!-- 环境选项 -->
|
||||
<RadioGroup
|
||||
android:id="@+id/rgEnv"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="32dp"
|
||||
android:orientation="vertical">
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbTest"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="72dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingStart="16dp"
|
||||
android:text="测试环境"
|
||||
android:textColor="#ff141428"
|
||||
android:textSize="28sp" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbUat"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="72dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingStart="16dp"
|
||||
android:text="UAT 环境"
|
||||
android:textColor="#ff141428"
|
||||
android:textSize="28sp" />
|
||||
|
||||
<RadioButton
|
||||
android:id="@+id/rbProd"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="72dp"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingStart="16dp"
|
||||
android:text="生产环境"
|
||||
android:textColor="#ff141428"
|
||||
android:textSize="28sp" />
|
||||
|
||||
</RadioGroup>
|
||||
|
||||
<!-- 分割线 -->
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="2dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:background="#FFDCDCF0" />
|
||||
|
||||
<!-- 底部按钮 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="88dp"
|
||||
android:layout_marginTop="24dp"
|
||||
android:gravity="center"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnCancel"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginEnd="20dp"
|
||||
android:layout_weight="1"
|
||||
android:background="@drawable/bg_white_radius10_stroke2"
|
||||
android:text="取消"
|
||||
android:textColor="#ff141428"
|
||||
android:textSize="28sp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnConfirm"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginStart="20dp"
|
||||
android:layout_weight="1"
|
||||
android:background="@drawable/bg_blue_radius10"
|
||||
android:text="确认"
|
||||
android:textColor="#ffffff"
|
||||
android:textSize="28sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user