1、修正上报数据
2、处理首页启动小程序逻辑
3、处理小程序跳转时间
This commit is contained in:
shenzuqiang 2026-03-27 12:04:45 +08:00
parent 8877763a29
commit 14853a281f
6 changed files with 120 additions and 31 deletions

View File

@ -3,6 +3,7 @@ package com.img.rabbit
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Activity import android.app.Activity
import android.os.Bundle import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
@ -16,6 +17,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.rememberCoroutineScope
@ -30,6 +32,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.permissions.ExperimentalPermissionsApi import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.img.rabbit.bean.response.UniVersionEntity import com.img.rabbit.bean.response.UniVersionEntity
import com.img.rabbit.components.CenterToast
import com.img.rabbit.components.GlobalToast import com.img.rabbit.components.GlobalToast
import com.img.rabbit.pages.LoadingCallback import com.img.rabbit.pages.LoadingCallback
import com.img.rabbit.pages.LoginScreen import com.img.rabbit.pages.LoginScreen
@ -81,6 +84,8 @@ class MainActivity : ComponentActivity(), LoadingCallback {
val reportViewModel:ReportViewModel = viewModel() val reportViewModel:ReportViewModel = viewModel()
generalViewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(application))[GeneralViewModel::class.java] generalViewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(application))[GeneralViewModel::class.java]
var lastClickTime by remember { mutableLongStateOf(0L) }
// UniMp小程序资源下载更新实体 // UniMp小程序资源下载更新实体
var downloadUniEntity by remember { mutableStateOf<UniVersionEntity?>(null) } var downloadUniEntity by remember { mutableStateOf<UniVersionEntity?>(null) }
var updateUniEntity by remember { mutableStateOf<UniVersionEntity?>(null) } var updateUniEntity by remember { mutableStateOf<UniVersionEntity?>(null) }
@ -107,11 +112,17 @@ class MainActivity : ComponentActivity(), LoadingCallback {
// 是否加载splashScreen完成 // 是否加载splashScreen完成
val isSplashDone = remember { mutableStateOf(false) } val isSplashDone = remember { mutableStateOf(false) }
// 是否启动小程序完成
val isUniMpDone = remember { mutableStateOf(false) }
// 加载UniMp小程序 // 加载UniMp小程序
var isLoadingUniMp by remember { mutableStateOf(false) } var isLoadingUniMp by remember { mutableStateOf(false) }
// 是否退出登录 // 是否退出登录
var isLogout 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 { splashScreen.setKeepOnScreenCondition {
@ -149,6 +160,7 @@ class MainActivity : ComponentActivity(), LoadingCallback {
showLoginDialog = true showLoginDialog = true
} }
// ... 可以处理其他事件 // ... 可以处理其他事件
else -> {}
} }
} }
} }
@ -175,9 +187,11 @@ class MainActivity : ComponentActivity(), LoadingCallback {
} }
} }
// UniMp事件监听
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
DCUniMPSDK.getInstance().setOnUniMPEventCallBack { appid, event, data, callback ->//appid, event, data, callback -> 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 weixinMpOriId = JSONObject(data.toString()).optString("weixinMpOriId")
val outTradeNo = JSONObject(data.toString()).optString("outTradeNo") val outTradeNo = JSONObject(data.toString()).optString("outTradeNo")
@ -186,6 +200,22 @@ class MainActivity : ComponentActivity(), LoadingCallback {
weixinMpOriId = weixinMpOriId, weixinMpOriId = weixinMpOriId,
outTradeNo = outTradeNo 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) { when (result) {
-1 -> { -1 -> {
//下载失败 //下载失败
isUniMpDone.value = false
isSplashDone.value = true isSplashDone.value = true
} }
1 -> { 1 -> {
@ -247,17 +278,22 @@ class MainActivity : ComponentActivity(), LoadingCallback {
//下载完成(关闭下载框) //下载完成(关闭下载框)
showUniDownloadDialog = false showUniDownloadDialog = false
} }
3 -> {
//启动成功
isUniMpDone.value = true
isSplashDone.value = true
}
4 -> { 4 -> {
//启动失败 //启动失败
isUniMpDone.value = false
isSplashDone.value = true isSplashDone.value = true
} }
} }
} }
} }
} }
} }
//启动小程序(资源存在并且最新则直接启动,否则执行下载后启动)
LaunchedEffect(isLoadingUniMp) { LaunchedEffect(isLoadingUniMp) {
if(isLoadingUniMp){ if(isLoadingUniMp){
downloadUniEntity?.let { uniMp -> downloadUniEntity?.let { uniMp ->
@ -266,34 +302,58 @@ class MainActivity : ComponentActivity(), LoadingCallback {
showUniDownloadDialog = true showUniDownloadDialog = true
} else { } else {
// 执行分发逻辑(异步操作) // 执行分发逻辑(异步操作)
distributeUniMp(context, uniMp, reportViewModel) {} distributeUniMp(context, uniMp, reportViewModel) {
isUniMpDone.value = it
isSplashDone.value = true
}
} }
} }
isLoadingUniMp = false isLoadingUniMp = false
} }
} }
//启动MainScreen(如果启动小程序则延迟2秒启动否则直接启动主界面)
LaunchedEffect(delayLoadingMainScreen) {
if (delayLoadingMainScreen) {
delay(2000) // 延迟 2 秒
showMainScreen = true
}
}
// 页面显示逻辑 // 页面显示逻辑
AppTheme { AppTheme {
//显示启动页
SplashScreenContent(splashDone = isSplashDone, generalViewModel = generalViewModel){ SplashScreenContent(splashDone = isSplashDone, generalViewModel = generalViewModel){
if(isLogout){// 退出登录后,显示登录页 if(isUniMpDone.value){
// 同意隐私协议政策,检验是否有一键登录权限 //延迟加载MainScreen
loginViewModel.oneKeyLoginForGeTuiSdk(context as Activity) { isAllowShowOneKeyScreen -> delayLoadingMainScreen = true
if (isAllowShowOneKeyScreen) { }else{
loginViewModel.loginScreenType.value = LoginScreenType.LOGIN_ONE_KEY showMainScreen = true
} else {
// 检验是否有一键登录权限失败,显示验证码登录
loginViewModel.loginScreenType.value = LoginScreenType.LOGIN_CAPTCHA
}
}
// 显示登录页
LoginScreen(generalViewModel = generalViewModel, loginViewModel = loginViewModel, isVisibilityBreak = false)
}else if(isSplashDone.value){
// 显示主界面
MainScreen(generalViewModel = generalViewModel, loginViewModel = loginViewModel)
} }
} }
// 显示主界面因为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){ if(showLoginDialog){
TipsDialog( TipsDialog(

View File

@ -19,5 +19,6 @@ object ReportType {
object ReportKey { object ReportKey {
const val EVENT_CLIENT_UNI_RELEASE_WGT: String = "client.uni.release.wgt" //释放资源 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" //页面加载 const val EVENT_CLIENT_UNI_SPEC_PAGE_LOAD: String = "client.uni.page.load" //页面加载
} }

View File

@ -59,7 +59,6 @@ import com.img.rabbit.utils.GlobalEventBus
import com.img.rabbit.viewmodel.BindViewModel import com.img.rabbit.viewmodel.BindViewModel
import com.img.rabbit.viewmodel.GeneralViewModel import com.img.rabbit.viewmodel.GeneralViewModel
import com.img.rabbit.viewmodel.LoginViewModel import com.img.rabbit.viewmodel.LoginViewModel
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -74,7 +73,7 @@ sealed class TabItem(val title: String, val router:String, val normalIconRes: In
*/ */
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") @SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable @Composable
fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewModel) { fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewModel, isUniMpDone: Boolean) {
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val navController = rememberNavController() val navController = rememberNavController()
val networkStatus by generalViewModel.networkStatus.observeAsState(initial = true) 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( Scaffold(
bottomBar = { bottomBar = {
if (isNavigationBarVisible) { if (isNavigationBarVisible) {
@ -148,7 +159,7 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode
interactionSource = remember { MutableInteractionSource() }, interactionSource = remember { MutableInteractionSource() },
indication = null indication = null
){ ){
if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true){ if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone){
//跳转小程序 //跳转小程序
coroutineScope.launch { coroutineScope.launch {
GlobalEventBus.emit(GlobalEvent.StartupUniMpNotify) 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) }, label = { Text(item.title, color = if (selectedTab == tabItems[index]) item.selectedColor else item.normalColor) },
selected = selectedTab == tabItems[index], selected = selectedTab == tabItems[index],
onClick = { onClick = {
if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true){ if (item == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone){
//跳转小程序 //跳转小程序
coroutineScope.launch { coroutineScope.launch {
GlobalEventBus.emit(GlobalEvent.StartupUniMpNotify) GlobalEventBus.emit(GlobalEvent.StartupUniMpNotify)
@ -197,9 +208,8 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode
// 导航主机 // 导航主机
NavHost( NavHost(
navController = navController, navController = navController,
startDestination = ScreenRoute.Home.route startDestination = if(PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone) ScreenRoute.Mine.route else ScreenRoute.Home.route
) { ) { // Tab页面
// Tab页面
composable(ScreenRoute.Home.route) { composable(ScreenRoute.Home.route) {
HomeScreen( HomeScreen(
navController = navController, navController = navController,
@ -335,6 +345,9 @@ fun MainScreen(generalViewModel: GeneralViewModel, loginViewModel: LoginViewMode
// 根据选中的Tab切换导航路由 // 根据选中的Tab切换导航路由
LaunchedEffect(selectedTab) { LaunchedEffect(selectedTab) {
if(selectedTab == TabItem.Home && PreferenceUtil.getUserConfig()?.config?.isUniMpOpen == true && isUniMpDone){
return@LaunchedEffect
}
when (selectedTab) { when (selectedTab) {
TabItem.Home -> navController.navigate(ScreenRoute.Home.route) { TabItem.Home -> navController.navigate(ScreenRoute.Home.route) {
popUpTo(ScreenRoute.Home.route) { inclusive = true } popUpTo(ScreenRoute.Home.route) { inclusive = true }

View File

@ -1,11 +1,13 @@
package com.img.rabbit.pages.screen package com.img.rabbit.pages.screen
import android.annotation.SuppressLint
import android.util.Log import android.util.Log
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState import androidx.compose.runtime.MutableState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
@ -21,8 +23,11 @@ import com.img.rabbit.utils.ChannelUtils
import com.img.rabbit.viewmodel.GeneralViewModel import com.img.rabbit.viewmodel.GeneralViewModel
import com.umeng.analytics.MobclickAgent import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure import com.umeng.commonsdk.UMConfigure
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.system.exitProcess import kotlin.system.exitProcess
@SuppressLint("CoroutineCreationDuringComposition")
@Composable @Composable
fun SplashScreenContent( fun SplashScreenContent(
splashDone: MutableState<Boolean>, splashDone: MutableState<Boolean>,

View File

@ -69,6 +69,7 @@ sealed class GlobalEvent {
object ShowAppUpdateNotify : GlobalEvent() object ShowAppUpdateNotify : GlobalEvent()
object ShowLoginNotify : GlobalEvent() object ShowLoginNotify : GlobalEvent()
object StartupUniMpNotify : GlobalEvent() object StartupUniMpNotify : GlobalEvent()
object JumpMineNotify : GlobalEvent()
data class ShowUniDownloadNotify(val entity: UniVersionEntity) : GlobalEvent() data class ShowUniDownloadNotify(val entity: UniVersionEntity) : GlobalEvent()
data class ShowUniUpdateNotify(val entity: UniVersionEntity) : GlobalEvent() data class ShowUniUpdateNotify(val entity: UniVersionEntity) : GlobalEvent()
} }

View File

@ -170,7 +170,7 @@ object UniAppUtils {
}else{ }else{
startUniMpToPage(context, uniVersion.unimp_id, uniVersion.unimp_type, null) startUniMpToPage(context, uniVersion.unimp_id, uniVersion.unimp_type, null)
} }
onResult(false) onResult(true)
} }
/** /**
@ -250,16 +250,25 @@ object UniAppUtils {
//事件提交 //事件提交
reportViewModel.requestReport( reportViewModel.requestReport(
ReportRequest( ReportRequest(
ReportType.ERROR, type = ReportType.ERROR,
ReportKey.EVENT_CLIENT_UNI_RELEASE_WGT, key = ReportKey.EVENT_CLIENT_UNI_RELEASE_WGT,
uniMpId, value = "释放资源失败",
"释放资源失败" extra = uniMpId
) )
) )
} }
} }
}else{ }else{
CenterToast.show("加载失败,请重试或联系客服!") CenterToast.show("加载失败,请重试或联系客服!")
//事件提交
reportViewModel.requestReport(
ReportRequest(
type = ReportType.ERROR,
key = ReportKey.EVENT_CLIENT_UNI_DELETE_UNI,
value = "删除资源失败",
extra = uniMpId
)
)
} }
} }