feat(config): 实现应用基础URL配置管理与环境切换功能
- 引入GlobalData对象统一管理应用基础URL配置 - 添加TEST/UAT/PROD三套环境切换弹窗功能 - 在BaseActivity中集成双击右上角状态栏显示环境切换对话框 - 将网络请求配置从App.configUrl迁移至GlobalData.appBaseUrl - 扩展SpTool工具类支持baseUrl本地存储 - 增加HTTP连接和写入超时时间至30秒 - 更新构建配置启用BuildConfig支持 - 添加环境切换相关UI资源文件和样式定义
This commit is contained in:
@@ -44,6 +44,7 @@ android {
|
||||
}
|
||||
buildFeatures {
|
||||
viewBinding = true
|
||||
buildConfig = true
|
||||
}
|
||||
|
||||
ndkVersion = "29.0.13846066"
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.shuwei.intelligent.shelves
|
||||
|
||||
object GlobalData {
|
||||
|
||||
// /**
|
||||
// * 设备id
|
||||
// */
|
||||
// var deviceId: String = ""
|
||||
|
||||
// /**
|
||||
// * app版本号
|
||||
// */
|
||||
// var appVersion: String = "1"
|
||||
|
||||
var appBaseUrl: String = ""
|
||||
|
||||
/**
|
||||
* 具体业务 BaseUrl
|
||||
*/
|
||||
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 = "生产环境"
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 全局常量
|
||||
*/
|
||||
object Constants {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* key
|
||||
*/
|
||||
object GlobalKey {
|
||||
const val KEY_BASE_URL = "baseUrlKey"
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import com.shuwei.intelligent.shelves.App
|
||||
import com.shuwei.intelligent.shelves.GlobalData
|
||||
import com.shuwei.intelligent.shelves.base.BaseActivity
|
||||
import com.shuwei.intelligent.shelves.databinding.ActivityInitBinding
|
||||
import com.shuwei.intelligent.shelves.model.DeviceConfigInfo
|
||||
@@ -63,7 +64,12 @@ class InitActivity : BaseActivity() {
|
||||
// App.deviceId = "4787e213-90ab-3e32-88e0-ac271a937751"
|
||||
Log.d(TAG, "onCreate: deviceId = ${App.deviceId}")
|
||||
SpTool.put(SpTool.DEVICE_ID, App.deviceId)
|
||||
App.configUrl = UrlConfig.BASE_URL
|
||||
val appBaseUrl = SpTool.baseUrl
|
||||
if (!appBaseUrl.isBlank()) {
|
||||
GlobalData.appBaseUrl = appBaseUrl
|
||||
} else {
|
||||
GlobalData.appBaseUrl = GlobalData.PROD_BASE_URL
|
||||
}
|
||||
App.canteenId = "0"
|
||||
|
||||
// var deviceId = AppUtil.getUDID(this)
|
||||
@@ -144,7 +150,7 @@ class InitActivity : BaseActivity() {
|
||||
state.data.data?.let {
|
||||
if (it is DeviceConfigInfo) {
|
||||
SpTool.put(SpTool.DEVICE_CONFIG_CACHE, it.toJsonString())
|
||||
App.configUrl = it.appPackageUrl?:""
|
||||
GlobalData.appBaseUrl = it.appPackageUrl?:""
|
||||
App.canteenId = it.canteenId?:""
|
||||
|
||||
startActivity<HomeActivity>()
|
||||
@@ -169,7 +175,7 @@ class InitActivity : BaseActivity() {
|
||||
if (config == null) {
|
||||
return false
|
||||
}
|
||||
App.configUrl = config.appPackageUrl?:""
|
||||
GlobalData.appBaseUrl = config.appPackageUrl?:""
|
||||
App.canteenId = config.canteenId?:""
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -71,6 +71,9 @@ open class BaseActivity : AppCompatActivity() {
|
||||
binding.tvLeftStatus.setOnDoubleClickListener {
|
||||
finish()
|
||||
}
|
||||
binding.tvRightStatus.setOnDoubleClickListener {
|
||||
EnvSwitchDialog(this).show()
|
||||
}
|
||||
}
|
||||
|
||||
fun hideStatusBar() {
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.shuwei.intelligent.shelves.base
|
||||
|
||||
import android.content.Context
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import com.shuwei.intelligent.shelves.GlobalData
|
||||
import com.shuwei.intelligent.shelves.databinding.DialogEnvSwitchBinding
|
||||
import com.shuwei.intelligent.shelves.utils.SpTool
|
||||
import com.shuwei.intelligent.shelves.utils.ToastUtil
|
||||
import com.shuwei.intelligent.shelves.utils.ext.dp
|
||||
|
||||
/**
|
||||
* 环境切换弹窗
|
||||
*
|
||||
* 提供 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) {
|
||||
ToastUtil.show(context,"请选择环境")
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,8 @@ package com.shuwei.intelligent.shelves.net
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.util.Log
|
||||
import com.shuwei.intelligent.shelves.App
|
||||
import com.shuwei.intelligent.shelves.BuildConfig
|
||||
import com.shuwei.intelligent.shelves.GlobalData
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Response
|
||||
@@ -18,7 +19,7 @@ import kotlin.apply
|
||||
|
||||
val apiService: ApiService = Retrofit.Builder()
|
||||
// .baseUrl(UrlConfig.BASE_URL)
|
||||
.baseUrl(UrlConfig.DEVICE_BASE_URL)
|
||||
.baseUrl(GlobalData.appBaseUrl)
|
||||
.client(HttpManager.instance.client)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build()
|
||||
@@ -28,9 +29,9 @@ class HttpManager private constructor() {
|
||||
val client: OkHttpClient by lazy {
|
||||
OkHttpClient.Builder()
|
||||
.apply {
|
||||
connectTimeout(15, TimeUnit.SECONDS)
|
||||
connectTimeout(30, TimeUnit.SECONDS)
|
||||
readTimeout(30, TimeUnit.SECONDS)
|
||||
writeTimeout(15, TimeUnit.SECONDS)
|
||||
writeTimeout(30, TimeUnit.SECONDS)
|
||||
sslSocketFactory(createSSLSocketFactory(), TrustAllCerts())
|
||||
hostnameVerifier { _, _ -> true }
|
||||
// addInterceptor(LoggingInterceptor())
|
||||
@@ -38,11 +39,11 @@ class HttpManager private constructor() {
|
||||
Log.d("HttpManager","okhttp logger ==>${it}")
|
||||
}).apply {
|
||||
level =
|
||||
// if (MyApp.DEBUG) {
|
||||
if (BuildConfig.DEBUG) {
|
||||
HttpLoggingInterceptor.Level.BODY
|
||||
// } else {
|
||||
// HttpLoggingInterceptor.Level.NONE
|
||||
// }
|
||||
} else {
|
||||
HttpLoggingInterceptor.Level.NONE
|
||||
}
|
||||
})
|
||||
// addInterceptor { chain ->
|
||||
// val request = chain.request().newBuilder()
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.util.Log
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.shuwei.intelligent.shelves.App
|
||||
import com.shuwei.intelligent.shelves.GlobalData
|
||||
import com.shuwei.intelligent.shelves.model.ShelfBody
|
||||
import com.shuwei.intelligent.shelves.model.ShelfModel
|
||||
import com.shuwei.intelligent.shelves.net.HttpManager.Companion.instance
|
||||
@@ -148,9 +149,6 @@ class NetViewModel : ViewModel() {
|
||||
viewModelScope.launch {
|
||||
_getGoodsListUiState.value = UiState.Loading
|
||||
runCatching {
|
||||
if (App.configUrl.isNullOrBlank()) {
|
||||
App.configUrl = UrlConfig.BASE_URL
|
||||
}
|
||||
val response = apiService.getGoodsList(
|
||||
canteenId = canteenId,
|
||||
goodsName = goodsName,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.shuwei.intelligent.shelves.net
|
||||
|
||||
import com.shuwei.intelligent.shelves.App
|
||||
import com.shuwei.intelligent.shelves.GlobalData
|
||||
|
||||
object UrlConfig {
|
||||
// const val DEVICE_BASE_URL = "http://device.shuziweidao.com:8889"
|
||||
@@ -13,18 +14,18 @@ object UrlConfig {
|
||||
// */
|
||||
// const val DEVICE_CONFIG = "${DEVICE_BASE_URL}/equipment/stEquipment/queryByEquipmentCode"
|
||||
|
||||
const val BASE_URL = "https://dev.yixiong-tech.com:8081"
|
||||
// const val BASE_URL = "https://yyjk.shuziweidao.com/gateway"
|
||||
// const val BASE_URL = "http://192.168.1.201:14801"
|
||||
const val DEVICE_BASE_URL = BASE_URL
|
||||
// const val BASE_URL = "https://dev.yixiong-tech.com:8081"
|
||||
//// const val BASE_URL = "https://yyjk.shuziweidao.com/gateway"
|
||||
//// const val BASE_URL = "http://192.168.1.201:14801"
|
||||
// const val DEVICE_BASE_URL = BASE_URL
|
||||
|
||||
var GET_ACCESS_TOKEN = "${App.configUrl}/restaurant/equipment/stEquipment/getEquipmentToken"
|
||||
var GET_ACCESS_TOKEN = "${GlobalData.appBaseUrl}/restaurant/equipment/stEquipment/getEquipmentToken"
|
||||
var GET_SHELF_LIST =
|
||||
"${App.configUrl}/terminal/neglect/smartShelves/app/smartShelves/app/getShelvesListByDeviceId"
|
||||
"${GlobalData.appBaseUrl}/terminal/neglect/smartShelves/app/smartShelves/app/getShelvesListByDeviceId"
|
||||
var GET_GOODS_LIST =
|
||||
"${App.configUrl}/terminal/neglect/smartShelves/app/smartShelves/app/getGoodsPageList"
|
||||
"${GlobalData.appBaseUrl}/terminal/neglect/smartShelves/app/smartShelves/app/getGoodsPageList"
|
||||
var SAVE_SHELF_GOODS_LIST =
|
||||
"${App.configUrl}/terminal/neglect/smartShelves/app/smartShelves/app/saveShelvesGoodsList"
|
||||
"${GlobalData.appBaseUrl}/terminal/neglect/smartShelves/app/smartShelves/app/saveShelvesGoodsList"
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.shuwei.intelligent.shelves.utils
|
||||
|
||||
import com.shuwei.intelligent.shelves.App
|
||||
import com.shuwei.intelligent.shelves.GlobalKey
|
||||
import com.shuwei.intelligent.shelves.utils.ext.put
|
||||
import kotlin.to
|
||||
|
||||
@@ -28,4 +29,10 @@ object SpTool {
|
||||
fun getBoolean(key: String, defValue: Boolean = false): Boolean {
|
||||
return pref.getBoolean(key, defValue)
|
||||
}
|
||||
|
||||
var baseUrl: String
|
||||
get() = getString(GlobalKey.KEY_BASE_URL, "")
|
||||
set(value) {
|
||||
put(GlobalKey.KEY_BASE_URL, value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:color="#30000000">
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
|
||||
<solid android:color="@color/white" />
|
||||
<corners android:radius="8dp" />
|
||||
<stroke
|
||||
android:width="1dp"
|
||||
android:color="@color/bg_card_blue" />
|
||||
</shape>
|
||||
</item>
|
||||
</ripple>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:color="#30000000">
|
||||
<item>
|
||||
<shape android:shape="rectangle">
|
||||
|
||||
<solid android:color="@color/bg_card_blue" />
|
||||
<corners android:radius="8dp" />
|
||||
</shape>
|
||||
</item>
|
||||
</ripple>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="@color/white"/>
|
||||
<corners android:radius="12dp"/>
|
||||
</shape>
|
||||
@@ -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">
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
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_cancel"
|
||||
android:text="取消"
|
||||
android:textColor="@color/bg_card_blue"
|
||||
android:textSize="28sp" />
|
||||
|
||||
<androidx.appcompat.widget.AppCompatButton
|
||||
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_confirm"
|
||||
android:text="确认"
|
||||
android:textColor="#FFFFFF"
|
||||
android:textSize="28sp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user