routeUtil.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Route Util
  3. * @邠心vbe on 2023/02/13
  4. */
  5. import { CommonActions } from '@react-navigation/routers';
  6. import React, { Component } from 'react';
  7. import { View } from 'react-native';
  8. import { PagerList } from '../pages/chargeV2/ChargeAdapter';
  9. import { PageList } from '../pages/Router';
  10. const routeUtil = {
  11. /**
  12. * 重置路由并导航到首页
  13. * @param {Object} props 页面道具
  14. */
  15. resetToHome: (props) => {
  16. if (props.navigation) {
  17. props.navigation.reset({
  18. index: 0,
  19. routes: [{
  20. name: PageList.home
  21. }]
  22. });
  23. }
  24. },
  25. /**
  26. * 重置路由并导航到指定路由
  27. * @param {Object} props 页面道具
  28. * @param {Array} routes 指定路由
  29. */
  30. resetHome2Page: (props, routes=[]) => {
  31. if (props.navigation) {
  32. props.navigation.reset({
  33. index: routes.length,
  34. routes: [
  35. {
  36. name: PageList.home,
  37. },
  38. ...routes
  39. ]
  40. })
  41. }
  42. },
  43. /**
  44. * 跳转到新页面并清除当前页面
  45. * @param {Object} props 页面道具
  46. * @param {Object} route 需要导航的路由
  47. */
  48. redirectTo: (props, route) => {
  49. if (props.navigation) {
  50. props.navigation.dispatch(state => {
  51. var routes = []
  52. routes = JSON.parse(JSON.stringify(state.routes))
  53. routes.pop();
  54. routes.push(route);
  55. return CommonActions.reset({
  56. index: routes.length - 1,
  57. routes: routes
  58. })
  59. });
  60. }
  61. },
  62. /**
  63. * 导航到中转页面
  64. * @param {String} route 需要导航的路由名称
  65. */
  66. bridge2Page: (route) => {
  67. startPage(PageList.bridge, {route: route})
  68. },
  69. /**
  70. * 导航到中转页面
  71. * @param {Array} routes 需要导航的路由
  72. */
  73. bridge2Pages: (routes=[]) => {
  74. startPage(PageList.bridge, {routes: routes})
  75. }
  76. }
  77. export default routeUtil;
  78. export class BridgePage extends Component {
  79. constructor(props) {
  80. super(props);
  81. this.state = {
  82. params: ""
  83. };
  84. }
  85. componentDidMount() {
  86. const routes = this.props.route?.params?.routes
  87. const route = this.props.route?.params?.route
  88. if (route) {
  89. startPage(route);
  90. } else if (routes) {
  91. console.log('bridge', routes);
  92. setTimeout(() => {
  93. routeUtil.resetHome2Page(this.props, routes)
  94. }, 500);
  95. }
  96. this.props.navigation.addListener('focus', e => {
  97. console.log('bridge.focus', this.props.route?.params)
  98. if (this.state.params) {
  99. routeUtil.resetToHome(this.props)
  100. } else {
  101. this.setState({
  102. params: true
  103. })
  104. }
  105. });
  106. }
  107. render() {
  108. return (
  109. <View style={ui.flex1}>
  110. </View>
  111. );
  112. }
  113. }