feat(app): 添加环境切换功能并优化基础URL管理

- 在主界面标题时间区域添加点击事件,支持弹出环境切换对话框
- 新增EnvSwitchDialog类实现TEST/UAT/PROD三套环境切换功能
- 新增dialog_env_switch.xml布局文件定义环境切换弹窗UI
- 重构GlobalData类,统一管理基础URL常量并添加持久化支持
- 修改MyApp初始化逻辑,优先读取用户手动设置的环境配置
- 添加SpTool.baseUrl属性用于环境配置的本地存储
- 修复MainActivity布局文件中的TextView垂直内边距问题
- 调整食物识别防抖时间从500ms到3000ms,优化计算营养逻辑执行时机
This commit is contained in:
2026-03-31 09:48:32 +08:00
parent f40c2046f1
commit 91800a6bb1
8 changed files with 216 additions and 13 deletions
@@ -17,11 +17,14 @@ object GlobalData {
*/
var appVersion: String = "1"
var appBaseUrl: String = ""
/**
* 具体业务baseurl
* 具体业务 BaseUrl
*/
var appBaseUrl2: String = "http://192.168.1.201:14801"
var appBaseUrl: 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 = "生产环境"
/**
* 菜品识别模型版本号,后边以接口返回为准
@@ -69,4 +72,5 @@ object GlobalKey {
const val KEY_PICKUP_MODE = "pickupMode"
const val KEY_CHARGE_MODE = "chargeMode"
const val KEY_SETTLEMENT_MODE = "settlementMode"
const val KEY_BASE_URL = "baseUrlKey"
}
+14 -2
View File
@@ -6,6 +6,8 @@ import com.sw.dualscreen.objbox.ObjectBox
import com.sw.dualscreen.sdk.SensorScaleUtils
import com.sw.dualscreen.utils.ActivityManager
import com.sw.dualscreen.utils.CrashHandler
import com.sw.dualscreen.utils.SPUtil
import com.sw.dualscreen.utils.SpTool
import com.sw.plate.App
import com.sw.plate.utils.AppUtil
import timber.log.Timber
@@ -13,7 +15,7 @@ import timber.log.Timber
class MyApp : App() {
companion object {
const val DEBUG: Boolean = true
var instance: MyApp?=null
var instance: MyApp? = null
}
override fun onCreate() {
@@ -24,13 +26,23 @@ class MyApp : App() {
Timber.d("deviceId = $deviceId")
// deviceId = "3ea47dc0-3cf0-3c2f-909c-265a9a65572e"
GlobalData.deviceId = deviceId
GlobalData.appBaseUrl = if ("2987f0c5-5754-33e9-b00a-251db5e2e55f" == deviceId) GlobalData.appBaseUrl2 else GlobalData.appBaseUrl
ObjectBox.init(this)
// 初始化崩溃处理器
CrashHandler.init(this)
SensorScaleUtils.startScale()
addActivityLifecycleListener()
// 优先读取用户手动切换后持久化的 url
val savedUrl = SpTool.baseUrl
if (!savedUrl.isNullOrEmpty()) {
GlobalData.appBaseUrl = savedUrl
} else {
// 未保存过则使用默认逻辑:特定设备走测试环境,其余走 UAT
GlobalData.appBaseUrl =
if ("2987f0c5-5754-33e9-b00a-251db5e2e55f" == deviceId) GlobalData.TEST_BASE_URL else GlobalData.PROD_BASE_URL
}
}
private fun addActivityLifecycleListener() {
@@ -31,6 +31,7 @@ import com.sw.dualscreen.GlobalKey
import com.sw.dualscreen.adapter.FoodOrderAdapter
import com.sw.dualscreen.adapter.MainFoodListAdapter
import com.sw.dualscreen.databinding.ActivityMainBinding
import com.sw.dualscreen.dialog.EnvSwitchDialog
import com.sw.dualscreen.dialog.RemindDialog
import com.sw.dualscreen.ext.dp
import com.sw.dualscreen.ext.gone
@@ -203,6 +204,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
}
}
updateDateTime()
binding.tvTitleTime.setOnClickListener {
EnvSwitchDialog(this).show()
}
binding.ivSetting.setOnClickListener {
isRefreshPage = false
startActivity(Intent(this, SettingActivity::class.java))
@@ -0,0 +1,69 @@
package com.sw.dualscreen.dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.sw.dualscreen.GlobalData
import com.sw.dualscreen.databinding.DialogEnvSwitchBinding
import com.sw.dualscreen.ext.dp
import com.sw.dualscreen.utils.SpTool
import com.sw.plate.utils.ToastUtils
/**
* 环境切换弹窗
*
* 提供 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) {
ToastUtils.showToast("请选择环境")
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
SpTool.baseUrl = newUrl
onEnvChanged(newUrl)
dismiss()
}
}
}
@@ -102,7 +102,7 @@ class MainScreenPresentation(
// private var userNutritionData: UserNutritionData? = null
private var userNutrition: UserNutrition? = null
private var dinnerTypeInfo: DinnerType? = null
private val debouncer = Debouncer(500)
private val debouncer = Debouncer(3000)
private var recognitionTime: Long = 0L // 人脸识别时的时间
var recognitionWeight: Int = 0 // 人脸识别时的重量
@@ -771,14 +771,14 @@ class MainScreenPresentation(
if (currentFood == null || userNutrition == null) {
return
}
var weight = value
if (weight < 0) weight = 0
if (activity.settlementMode == 0) {
activity.runOnUiThread {
addFoodToOrder(weight, currentFood!!)
}
}
debouncer.debounce {
var weight = value
if (weight < 0) weight = 0
if (activity.settlementMode == 0) {
activity.runOnUiThread {
addFoodToOrder(weight, currentFood!!)
}
}
Timber.tag(TAG)
.d("calculateNutrition weight = $weight, recognitionWeight = $recognitionWeight")
val dinnerType = dinnerTypeInfo?.dinnerType
@@ -1,5 +1,6 @@
package com.sw.dualscreen.utils
import com.sw.dualscreen.GlobalKey
import com.sw.dualscreen.MyApp
import com.sw.dualscreen.utils.SPUtil.Companion.getInstance
@@ -27,4 +28,10 @@ object SpTool {
spUtil?.boolean(IS_FIRST_GET_FACE, value)
}
var baseUrl: String?
get() = spUtil?.get(GlobalKey.KEY_BASE_URL, "")
set(value) {
spUtil?.put(GlobalKey.KEY_BASE_URL, value)
}
}
@@ -22,6 +22,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="28dp"
android:paddingVertical="10dp"
android:textColor="#ff0a1428"
android:textSize="26sp"
android:textStyle="bold"
@@ -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_border_red"
android:text="取消"
android:textColor="#FFFF3232"
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_btn_save2"
android:text="确认"
android:textColor="#FFFFFF"
android:textSize="28sp" />
</LinearLayout>
</LinearLayout>