RouterV2.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * 路由配置文件
  3. * @邠心vbe on 2021/03/22
  4. */
  5. import React, { useEffect, useRef } from 'react';
  6. import { NavigationContainer } from '@react-navigation/native';
  7. import { createStackNavigator, TransitionPresets } from '@react-navigation/stack';
  8. import { enableScreens } from 'react-native-screens';
  9. import { BackButton } from '../components/Toolbar';
  10. import app from '../../app.json';
  11. import { PageList, setPageList } from './Router';
  12. import ToolbarUgly from '../components/ToolbarUgly';
  13. import utils from '../utils/utils';
  14. const Stack = createStackNavigator();
  15. enableScreens();
  16. /**
  17. * 配置APP主题色
  18. */
  19. const themeColor = {
  20. text: textPrimary,
  21. card: colorThemes,
  22. primary: colorPrimary,
  23. background: pageBackground,
  24. notification: colorDark
  25. }
  26. const noTitle = (opt = {}) => {
  27. return {
  28. title: isIOS ? 'Back' : app.displayName,
  29. headerShown: false,
  30. ...opt
  31. }
  32. }
  33. /**
  34. * 配置标题
  35. * @param {String} title 页面标题
  36. * @param {Object} opt 标题栏选项
  37. * @returns
  38. */
  39. const Title = (title, opt = {}, titleScope) => {
  40. const options = {
  41. headerShown: true,
  42. headerStyle: {
  43. ...titleHeight(),
  44. elevation: 0,
  45. shadowOpacity: 0,
  46. borderBottomWidth: 0,
  47. backgroundColor: app.isWhitelabel ? colorLight : colorPrimary //配置标题栏背景
  48. },
  49. headerTintColor: pageTitleTint, //配置标题栏文字和图标颜色
  50. headerBackTitle: ' ', //配置iOS返回按钮文字
  51. headerBackTitleVisible: false,
  52. ...opt
  53. }
  54. if (titleScope && $t) {
  55. options.header = () => <ToolbarUgly scope={titleScope} rightIcon={options?.headerRight}/>
  56. //options.headerTitle = () => <HeaderTitle scope={titleScope}/>
  57. } else {
  58. options.title = title
  59. }
  60. options.headerLeft = () => <BackButton/>
  61. return options;
  62. }
  63. var bakPages = undefined;
  64. function getPages() {
  65. let pages = [], keys = {};
  66. if (bakPages == undefined) {
  67. bakPages = Object.assign({}, PageList);
  68. } else {
  69. setPageList(bakPages);
  70. }
  71. for (const page in bakPages) {
  72. var p = bakPages[page]
  73. keys[page] = page;
  74. pages.push(
  75. <Stack.Screen
  76. key={page}
  77. name={page}
  78. component={p.component}
  79. options={p.title ? Title(p.title, p.options, p.titleScope) : noTitle(p.options)}
  80. />
  81. );
  82. }
  83. setPageList(keys);
  84. return pages;
  85. }
  86. const RouterV2 = () => {
  87. const navigation = useRef();
  88. useEffect(() => {
  89. //注入全局方法
  90. global.startPage = (name, params = {}) => {
  91. if (utils.isNotEmpty(name)) {
  92. navigation.current?.navigate(name, params);
  93. }
  94. }
  95. global.dispatchPage = (params) => {
  96. navigation.current?.dispatch(params);
  97. }
  98. global.goBack = () => {
  99. if (global.pageBackFallback && global.pageBackFallback.names) {
  100. //console.log("覆盖返回", navigation.current.getCurrentRoute()?.name);
  101. if (global.pageBackFallback.names.indexOf(navigation.current.getCurrentRoute()?.name) >= 0) {
  102. global.pageBackFallback?.callback();
  103. global.pageBackFallback = undefined;
  104. return;
  105. }
  106. }
  107. if (navigation.current?.canGoBack()) {
  108. navigation.current?.goBack();
  109. } else {
  110. startPage(PageList.home);
  111. }
  112. }
  113. return (() => {
  114. global.startPage = null;
  115. global.goBack = null;
  116. });
  117. }, []);
  118. return (
  119. <NavigationContainer
  120. ref={navigation}
  121. theme={{
  122. dark: darkMode,
  123. colors: themeColor
  124. }}>
  125. <Stack.Navigator
  126. initialRouteName='splash'
  127. screenOptions={{animationEnabled: true, ...TransitionPresets.SlideFromRightIOS }}>
  128. {getPages()}
  129. </Stack.Navigator>
  130. </NavigationContainer>
  131. )
  132. }
  133. const titleHeight = () => {
  134. return isIOS ? {} : {height: toolbarSize};
  135. }
  136. export default RouterV2;