Skip to content

Commit

Permalink
fix(harmony-hybrid): 解决混合路由的bug:直接从Taro返回时,原生侧不能同步返回
Browse files Browse the repository at this point in the history
解决思路:
1. 原生侧向Taro提供nativeBack() Api
2.
从Taro侧发起返回:拦截navigateBack()方法,判断当前页面的来源,来自原生则同步通知原生侧back
  • Loading branch information
58liuyang committed Jul 9, 2024
1 parent 8b1e79c commit d399ae9
Show file tree
Hide file tree
Showing 17 changed files with 252 additions and 37 deletions.
7 changes: 6 additions & 1 deletion examples/mini-program-example/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ const config = {
}
},
framework: 'react',
compiler: 'webpack5',
compiler: {
type: 'webpack5',
prebundle: {
enable: false
}
},
cache: {
enable: false // Webpack 持久化缓存配置,建议开启。默认配置请参考:https://docs.taro.zone/docs/config-detail#cache
},
Expand Down
5 changes: 4 additions & 1 deletion examples/mini-program-example/src/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ export default defineAppConfig({
'pages/api/thirdParty/index',
'pages/api/worker/index',
'pages/api/wxml/index',
'pages/performance/index/index'
'pages/performance/index/index',
'pages/harmony-hybrid/mix-router/home/index',
'pages/harmony-hybrid/mix-router/list/index',
'pages/harmony-hybrid/mix-router/detail/index'
],
tabBar: {
color: '#7A7E83',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
navigationBarTitleText: '混合路由:小程序首页',
enablePullDownRefresh: false,
backgroundTextStyle: 'dark',
onReachBottomDistance: 50,
pageOrientation: 'auto',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import "@/styles/variables.scss";

.container {
width: 100%;
height: 100%;
background-color: #E6FFCC;
}

.title {
font-size: 40px;
margin-top: 20px;
margin-bottom: 20px;
width: 100%;
text-align: center;
display: block;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react'
import { View, Button, Text } from '@tarojs/components'
import './index.scss'
import Taro from "@tarojs/taro";
export default class Index extends React.Component {
state = {
// syncSingleResult: '',
// requestSingleResult: '',
// requestConcurrentResult: '',
// requestHignConcurrentResult: '',
// syncResult: '',
// requestByJsResult: ''
}

render() {
// const { syncSingleResult } = this.state
return (
<View className='container'>
<Text className='title'>
混合路由测试场景:小程序详情页
</Text>
<Button
onClick={()=>{
Taro.navigateBack()
}}>
小程序端:返回
</Button>
</View>
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
navigationBarTitleText: '混合路由:小程序首页',
enablePullDownRefresh: false,
backgroundTextStyle: 'dark',
onReachBottomDistance: 50,
pageOrientation: 'auto',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@import "@/styles/variables.scss";

.container {
width: 100%;
height: 100%;
background-color: #FFCCCC;
}

.title {
font-size: 40px;
margin-top: 20px;
margin-bottom: 20px;
width: 100%;
text-align: center;
display: block;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react'
import { View, Button, Text } from '@tarojs/components'
import './index.scss'
import Taro from "@tarojs/taro";
import nativeApi from "@/util/nativeApi";
export default class Index extends React.Component {
state = {
// syncSingleResult: '',
// requestSingleResult: '',
// requestConcurrentResult: '',
// requestHignConcurrentResult: '',
// syncResult: '',
// requestByJsResult: ''
}

render() {
// const { syncSingleResult } = this.state
return (
<View className='container'>
<Text className='title'>
混合路由测试场景:小程序首页
</Text>
<Button
onClick={()=>{
Taro.navigateTo({
url: 'pages/harmony-hybrid/mix-router/list/index'
})
}}>
跳转:小程序列表页
</Button>
<Button
onClick={()=>{
nativeApi.navigateToNative({
url: 'pages/NativeList'
})
}}>
跳转:原生列表页
</Button>
</View>
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
navigationBarTitleText: '混合路由:小程序列表页',
enablePullDownRefresh: false,
backgroundTextStyle: 'dark',
onReachBottomDistance: 50,
pageOrientation: 'auto',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@import "@/styles/variables.scss";

.title {
font-size: 40px;
margin-top: 20px;
margin-bottom: 20px;
width: 100%;
text-align: center;
display: block;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react'
import { View, Button, Text } from '@tarojs/components'
import './index.scss'
import Taro from "@tarojs/taro";
export default class Index extends React.Component {
state = {
// syncSingleResult: '',
// requestSingleResult: '',
// requestConcurrentResult: '',
// requestHignConcurrentResult: '',
// syncResult: '',
// requestByJsResult: ''
}

render() {
// const { syncSingleResult } = this.state
return (
<View className='harmony-hybrid-page'>
<Text className='title'>
混合路由测试场景:小程序列表页
</Text>
<Button
onClick={()=>{
Taro.navigateTo({
url: 'pages/harmony-hybrid/mix-router/detail/index'
})
}}>
跳转:小程序详情页
</Button>
</View>
)
}
}
9 changes: 4 additions & 5 deletions examples/mini-program-example/src/pages/index/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,15 @@ export default function Index() {
</Button>
<Button
onClick={()=>{
nativeApi.harmonyNavigateTo({
indexHtmlPath: '/spa/new/index.html',
taroPath: 'pages/performance/index/index'
Taro.navigateTo({
url: 'pages/harmony-hybrid/mix-router/home/index'
})
}}>
多实例(多SPA跳转):性能列表页
多实例及混合路由测试页
</Button>
<Button
onClick={()=>{
nativeApi.harmonyNavigateTo({
nativeApi.navigateToTaroHybrid({
indexHtmlPath: '/spa/new/index.html',
taroPath: 'pages/api/index/index'
})
Expand Down
13 changes: 7 additions & 6 deletions examples/mini-program-example/src/util/nativeApi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// @ts-ignore
const decorator = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: true }) || (target => target)
const sync = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: true }) || (target => target)

// @proxyClassSign('')
class NativeApi {
// @ts-ignore
@decorator
harmonyNavigateTo(options: any) {
return options
}
@sync
navigateToTaroHybrid(_options: any) {}


@sync
navigateToNative(_options: any){}
}

const nativeApi = new NativeApi()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,10 @@ export class NativeApi {
}

@(asyncAndNotRelease)
onNativeNavigate(_options: any): void{}
onNativeNavigate (_options: any): void {}

@(syncAndRelease)
nativeBack (): void {}
}

export interface Status {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/**
* 关闭当前页面,返回上一页面或多级页面。
*
* @canUse navigateBack
* @__object [delta]
*/

/**
* 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import Taro from '@tarojs/api'
import { navigateTo as navigateToH5 } from '@tarojs/taro-h5'
import { navigateBack as navigateBackH5, navigateTo as navigateToH5 } from '@tarojs/taro-h5'

import native from '../NativeApi'

function getFromParameter (from: string|undefined): number {
let number: number = -1
if (from) {
// 尝试从from参数中提取数字
const numberMatch = from.match(/native_(\d+)/)
number = numberMatch ? parseInt(numberMatch[1], 10) : 1
}
return number
}

const delay = 300
let lastExecuteTime = 0
Expand All @@ -22,3 +34,48 @@ export function navigateTo (option: Taro.navigateTo.Option) {
lastUrl = targetUrl
return navigateToH5(option)
}

/**
* 关闭当前页面,返回上一页面或多级页面。
*
* @canUse navigateBack
* @__object [delta]
*/
export function navigateBack (options: Taro.navigateBack.Option) {
// 获取当前页面的实例
const instance = Taro.getCurrentInstance()
// 获取当前页面的完整URL
const from = instance?.router?.params?.from
const delta = getFromParameter(from)
if (delta > 0) {
// web回退到原生
native.nativeBack()
return new Promise(resolve => setTimeout(resolve, 200))
.then(() => {
// 先让native执行完返回动画,再执行Taro侧的返回
return navigateBackH5({ delta: delta })
})
}
return navigateBackH5(options)
}

// ----- 容器共用及混合路由 ------

// 监听原生路由操作(跳转及返回),Taro侧的路由同步执行操作
native.onNativeNavigate({
nativeNavigateTo: (url: string) => {
// 打开新的原生页面,但Web容器是共用的,新页面的加载,需要通过Taro侧来实现,不能通过webController.loadUrl()实现,会影响Taro的路由逻辑
navigateToH5({
url: url,
success: function () {}
})
},
nativeNavigateBack: (delta: number) => {
// 原生返回时,Taro侧也需要同步返回
navigateBackH5({
delta: delta
})
}
})


16 changes: 0 additions & 16 deletions packages/taro-platform-harmony-hybrid/src/api/apis/taro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
reLaunch,
switchTab,
} from './index'
import native from "./NativeApi";

const requirePlugin = () => {
return {
Expand Down Expand Up @@ -97,18 +96,3 @@ export {
pxTransform,
requirePlugin
}

// 监听原生的Navigate方法
native.onNativeNavigate({
nativeNavigateTo: (url: string)=>{
navigateTo({
url: url,
success: function ( ) {}
})
},
nativeNavigateBack: (delta: number) => {
navigateBack({
delta: delta
})
}
})

0 comments on commit d399ae9

Please sign in to comment.