RouterV2.js 3.6 KB

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