From 14853a281f5bd590eaad03a4018ab8a858c15a96 Mon Sep 17 00:00:00 2001 From: shenzuqiang Date: Fri, 27 Mar 2026 12:04:45 +0800 Subject: [PATCH] =?UTF-8?q?Dev=EF=BC=9A=201=E3=80=81=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=E4=B8=8A=E6=8A=A5=E6=95=B0=E6=8D=AE=202=E3=80=81=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=A6=96=E9=A1=B5=E5=90=AF=E5=8A=A8=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E9=80=BB=E8=BE=91=203=E3=80=81=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E8=B7=B3=E8=BD=AC=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/img/rabbit/MainActivity.kt | 98 +++++++++++++++---- .../img/rabbit/bean/request/ReportRequest.kt | 1 + .../java/com/img/rabbit/pages/MainPage.kt | 27 +++-- .../pages/screen/SplashScreenContent.kt | 5 + .../java/com/img/rabbit/utils/AppEventBus.kt | 1 + .../java/com/img/rabbit/utils/UniAppUtils.kt | 19 +++- 6 files changed, 120 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/img/rabbit/MainActivity.kt b/app/src/main/java/com/img/rabbit/MainActivity.kt index d600cb8..7f1020b 100644 --- a/app/src/main/java/com/img/rabbit/MainActivity.kt +++ b/app/src/main/java/com/img/rabbit/MainActivity.kt @@ -3,6 +3,7 @@ package com.img.rabbit import android.annotation.SuppressLint import android.app.Activity import android.os.Bundle +import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge @@ -16,6 +17,7 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableFloatStateOf +import androidx.compose.runtime.mutableLongStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope @@ -30,6 +32,7 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.viewmodel.compose.viewModel import com.google.accompanist.permissions.ExperimentalPermissionsApi import com.img.rabbit.bean.response.UniVersionEntity +import com.img.rabbit.components.CenterToast import com.img.rabbit.components.GlobalToast import com.img.rabbit.pages.LoadingCallback import com.img.rabbit.pages.LoginScreen @@ -81,6 +84,8 @@ class MainActivity : ComponentActivity(), LoadingCallback { val reportViewModel:ReportViewModel = viewModel() generalViewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(application))[GeneralViewModel::class.java] + var lastClickTime by remember { mutableLongStateOf(0L) } + // UniMp小程序资源下载更新实体 var downloadUniEntity by remember { mutableStateOf(null) } var updateUniEntity by remember { mutableStateOf(null) } @@ -107,11 +112,17 @@ class MainActivity : ComponentActivity(), LoadingCallback { // 是否加载splashScreen完成 val isSplashDone = remember { mutableStateOf(false) } + // 是否启动小程序完成 + val isUniMpDone = remember { mutableStateOf(false) } // 加载UniMp小程序 var isLoadingUniMp by remember { mutableStateOf(false) } // 是否退出登录 var isLogout by remember { mutableStateOf(false) } + // 加载显示MainScreen + var showMainScreen by remember { mutableStateOf(false) } + // 延迟加载MainScreen通知 + var delayLoadingMainScreen by remember { mutableStateOf(false) } // 设置启动页显示条件 splashScreen.setKeepOnScreenCondition { @@ -149,6 +160,7 @@ class MainActivity : ComponentActivity(), LoadingCallback { showLoginDialog = true } // ... 可以处理其他事件 + else -> {} } } } @@ -175,9 +187,11 @@ class MainActivity : ComponentActivity(), LoadingCallback { } } + // UniMp事件监听 LaunchedEffect(Unit) { DCUniMPSDK.getInstance().setOnUniMPEventCallBack { appid, event, data, callback ->//appid, event, data, callback -> - if(event == "start_combo_pay"){ + Log.i("MainScreen", "onUniMPEventCallBack: $event") + if("start_combo_pay" == event){ //拉起微信小程序来支付 val weixinMpOriId = JSONObject(data.toString()).optString("weixinMpOriId") val outTradeNo = JSONObject(data.toString()).optString("outTradeNo") @@ -186,6 +200,22 @@ class MainActivity : ComponentActivity(), LoadingCallback { weixinMpOriId = weixinMpOriId, outTradeNo = outTradeNo ) + }else if("unimp_BackPress_goMe" == event){ + //这里处理切换到我的页面 + coroutineScope.launch { + GlobalEventBus.emit( + GlobalEvent.JumpMineNotify) + } + UniAppUtils.getCurrentUniMp()?.closeUniMP() + }else if("unimp_BackPress_backbutton" == event){ + //这里需要处理手势、虚拟返回 + val currentTime = System.currentTimeMillis() + if (currentTime - lastClickTime > 2000) { + CenterToast.show("再按一次退出应用") + lastClickTime = currentTime + } else { + (context as? Activity)?.finish() + } } } } @@ -237,6 +267,7 @@ class MainActivity : ComponentActivity(), LoadingCallback { when (result) { -1 -> { //下载失败 + isUniMpDone.value = false isSplashDone.value = true } 1 -> { @@ -247,17 +278,22 @@ class MainActivity : ComponentActivity(), LoadingCallback { //下载完成(关闭下载框) showUniDownloadDialog = false } + 3 -> { + //启动成功 + isUniMpDone.value = true + isSplashDone.value = true + } 4 -> { //启动失败 + isUniMpDone.value = false isSplashDone.value = true } } - } } } } - + //启动小程序(资源存在并且最新则直接启动,否则执行下载后启动) LaunchedEffect(isLoadingUniMp) { if(isLoadingUniMp){ downloadUniEntity?.let { uniMp -> @@ -266,34 +302,58 @@ class MainActivity : ComponentActivity(), LoadingCallback { showUniDownloadDialog = true } else { // 执行分发逻辑(异步操作) - distributeUniMp(context, uniMp, reportViewModel) {} + distributeUniMp(context, uniMp, reportViewModel) { + isUniMpDone.value = it + isSplashDone.value = true + } } } isLoadingUniMp = false } } + //启动MainScreen(如果启动小程序则延迟2秒启动,否则直接启动主界面) + LaunchedEffect(delayLoadingMainScreen) { + if (delayLoadingMainScreen) { + delay(2000) // 延迟 2 秒 + showMainScreen = true + } + } // 页面显示逻辑 AppTheme { + //显示启动页 SplashScreenContent(splashDone = isSplashDone, generalViewModel = generalViewModel){ - if(isLogout){// 退出登录后,显示登录页 - // 同意隐私协议政策,检验是否有一键登录权限 - loginViewModel.oneKeyLoginForGeTuiSdk(context as Activity) { isAllowShowOneKeyScreen -> - if (isAllowShowOneKeyScreen) { - loginViewModel.loginScreenType.value = LoginScreenType.LOGIN_ONE_KEY - } else { - // 检验是否有一键登录权限失败,显示验证码登录 - loginViewModel.loginScreenType.value = LoginScreenType.LOGIN_CAPTCHA - } - } - // 显示登录页 - LoginScreen(generalViewModel = generalViewModel, loginViewModel = loginViewModel, isVisibilityBreak = false) - }else if(isSplashDone.value){ - // 显示主界面 - MainScreen(generalViewModel = generalViewModel, loginViewModel = loginViewModel) + if(isUniMpDone.value){ + //延迟加载MainScreen + delayLoadingMainScreen = true + }else{ + showMainScreen = true } } + // 显示主界面,因为UniMp小程序启动时,要实现SplashScreen图片与小程序的UniMPSplashView无缝切换,所以延迟2秒 + if (showMainScreen) { + MainScreen( + generalViewModel = generalViewModel, + loginViewModel = loginViewModel, + isUniMpDone = isUniMpDone.value + ) + } + // 退出登录后,显示登录页 + if(isLogout){ + // 同意隐私协议政策,检验是否有一键登录权限 + loginViewModel.oneKeyLoginForGeTuiSdk(context as Activity) { isAllowShowOneKeyScreen -> + if (isAllowShowOneKeyScreen) { + loginViewModel.loginScreenType.value = LoginScreenType.LOGIN_ONE_KEY + } else { + // 检验是否有一键登录权限失败,显示验证码登录 + loginViewModel.loginScreenType.value = LoginScreenType.LOGIN_CAPTCHA + } + } + // 显示登录页 + LoginScreen(generalViewModel = generalViewModel, loginViewModel = loginViewModel, isVisibilityBreak = false) + } + //提示登录 if(showLoginDialog){ TipsDialog( diff --git a/app/src/main/java/com/img/rabbit/bean/request/ReportRequest.kt b/app/src/main/java/com/img/rabbit/bean/request/ReportRequest.kt index 6102bfb..9f62ecc 100644 --- a/app/src/main/java/com/img/rabbit/bean/request/ReportRequest.kt +++ b/app/src/main/java/com/img/rabbit/bean/request/ReportRequest.kt @@ -19,5 +19,6 @@ object ReportType { object ReportKey { const val EVENT_CLIENT_UNI_RELEASE_WGT: String = "client.uni.release.wgt" //释放资源 + const val EVENT_CLIENT_UNI_DELETE_UNI: String = "client.uni.delete.uni" //删除资源 const val EVENT_CLIENT_UNI_SPEC_PAGE_LOAD: String = "client.uni.page.load" //页面加载 } diff --git a/app/src/main/java/com/img/rabbit/pages/MainPage.kt b/app/src/main/java/com/img/rabbit/pages/MainPage.kt index 79b2af3..83a6f02 100644 --- a/app/src/main/java/com/img/rabbit/pages/MainPage.kt +++ b/app/src/main/java/com/img/rabbit/pages/MainPage.kt @@ -59,7 +59,6 @@ import com.img.rabbit.utils.GlobalEventBus import com.img.rabbit.viewmodel.BindViewModel import com.img.rabbit.viewmodel.GeneralViewModel import com.img.rabbit.viewmodel.LoginViewModel -import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -74,7 +73,7 @@ sealed class TabItem(val title: String, val router:String, val normalIconRes: In */ @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") @Composable -fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewModel) { +fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewModel, isUniMpDone: Boolean) { val coroutineScope = rememberCoroutineScope() val navController = rememberNavController() val networkStatus by generalViewModel.networkStatus.observeAsState(initial = true) @@ -121,6 +120,18 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode } } + // 处理全局事件,类似与EventBus订阅 + LaunchedEffect(Unit) { + GlobalEventBus.events.collect { event -> + when (event) { + is GlobalEvent.JumpMineNotify -> { + selectedTab = TabItem.Mine + } + else -> {} + } + } + } + Scaffold( bottomBar = { if (isNavigationBarVisible) { @@ -148,7 +159,7 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode interactionSource = remember { MutableInteractionSource() }, indication = null ){ - if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true){ + if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone){ //跳转小程序 coroutineScope.launch { GlobalEventBus.emit(GlobalEvent.StartupUniMpNotify) @@ -162,7 +173,7 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode label = { Text(item.title, color = if (selectedTab == tabItems[index]) item.selectedColor else item.normalColor) }, selected = selectedTab == tabItems[index], onClick = { - if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true){ + if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone){ //跳转小程序 coroutineScope.launch { GlobalEventBus.emit(GlobalEvent.StartupUniMpNotify) @@ -197,9 +208,8 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode // 导航主机 NavHost( navController = navController, - startDestination = ScreenRoute.Home.route - ) { - // Tab页面 + startDestination = if(PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone) ScreenRoute.Mine.route else ScreenRoute.Home.route + ) { // Tab页面 composable(ScreenRoute.Home.route) { HomeScreen( navController = navController, @@ -335,6 +345,9 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode // 根据选中的Tab切换导航路由 LaunchedEffect(selectedTab) { + if(selectedTab == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone){ + return@LaunchedEffect + } when (selectedTab) { TabItem.Home -> navController.navigate(ScreenRoute.Home.route) { popUpTo(ScreenRoute.Home.route) { inclusive = true } diff --git a/app/src/main/java/com/img/rabbit/pages/screen/SplashScreenContent.kt b/app/src/main/java/com/img/rabbit/pages/screen/SplashScreenContent.kt index a51eecd..5e54670 100644 --- a/app/src/main/java/com/img/rabbit/pages/screen/SplashScreenContent.kt +++ b/app/src/main/java/com/img/rabbit/pages/screen/SplashScreenContent.kt @@ -1,11 +1,13 @@ package com.img.rabbit.pages.screen +import android.annotation.SuppressLint import android.util.Log import androidx.compose.foundation.Image import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.layout.ContentScale @@ -21,8 +23,11 @@ import com.img.rabbit.utils.ChannelUtils import com.img.rabbit.viewmodel.GeneralViewModel import com.umeng.analytics.MobclickAgent import com.umeng.commonsdk.UMConfigure +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import kotlin.system.exitProcess +@SuppressLint("CoroutineCreationDuringComposition") @Composable fun SplashScreenContent( splashDone: MutableState, diff --git a/app/src/main/java/com/img/rabbit/utils/AppEventBus.kt b/app/src/main/java/com/img/rabbit/utils/AppEventBus.kt index 75d368c..dc81983 100644 --- a/app/src/main/java/com/img/rabbit/utils/AppEventBus.kt +++ b/app/src/main/java/com/img/rabbit/utils/AppEventBus.kt @@ -69,6 +69,7 @@ sealed class GlobalEvent { object ShowAppUpdateNotify : GlobalEvent() object ShowLoginNotify : GlobalEvent() object StartupUniMpNotify : GlobalEvent() + object JumpMineNotify : GlobalEvent() data class ShowUniDownloadNotify(val entity: UniVersionEntity) : GlobalEvent() data class ShowUniUpdateNotify(val entity: UniVersionEntity) : GlobalEvent() } diff --git a/app/src/main/java/com/img/rabbit/utils/UniAppUtils.kt b/app/src/main/java/com/img/rabbit/utils/UniAppUtils.kt index b2ebff8..bb9cdee 100644 --- a/app/src/main/java/com/img/rabbit/utils/UniAppUtils.kt +++ b/app/src/main/java/com/img/rabbit/utils/UniAppUtils.kt @@ -170,7 +170,7 @@ object UniAppUtils { }else{ startUniMpToPage(context, uniVersion.unimp_id, uniVersion.unimp_type, null) } - onResult(false) + onResult(true) } /** @@ -250,16 +250,25 @@ object UniAppUtils { //事件提交 reportViewModel.requestReport( ReportRequest( - ReportType.ERROR, - ReportKey.EVENT_CLIENT_UNI_RELEASE_WGT, - uniMpId, - "释放资源失败" + type = ReportType.ERROR, + key = ReportKey.EVENT_CLIENT_UNI_RELEASE_WGT, + value = "释放资源失败", + extra = uniMpId ) ) } } }else{ CenterToast.show("加载失败,请重试或联系客服!") + //事件提交 + reportViewModel.requestReport( + ReportRequest( + type = ReportType.ERROR, + key = ReportKey.EVENT_CLIENT_UNI_DELETE_UNI, + value = "删除资源失败", + extra = uniMpId + ) + ) } }