Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/taro-platform-weapp/src/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,52 @@ export function initNativeApi (taro) {
}
}
})

// 保存原始的 navigateTo 方法
const originalNavigateTo = taro.navigateTo

/**
* 重写 navigateTo 以支持 Skyline 渲染模式
* 在 Skyline 模式下使用 routeType 参数时,直接调用微信原生 API,避免 Taro 运行时的动画处理导致节点查找失败
* @param options - 导航参数对象
* @returns Promise<any>
*/
taro.navigateTo = function (options: Record<string, any>) {
// 检查是否在 Skyline 模式下使用了特殊的 routeType
if (options?.routeType && typeof wx !== 'undefined') {
// 获取当前渲染器类型
const renderer = taro.getRenderer?.() || 'webview'

// Skyline 模式下,routeType 参数需要通过原生 API 直接调用
// 避免 Taro 运行时的动画处理导致节点查找失败
if (renderer === 'skyline') {
return new Promise((resolve, reject) => {
const { success, fail, complete, ...restOptions } = options
wx.navigateTo({
...restOptions,
success: (res) => {
success?.(res)
complete?.(res)
resolve(res)
},
fail: (err) => {
try {
fail?.(err)
} catch (e) {
// ignore
}
complete?.(err)
reject(err)
}
})
})
Comment on lines +44 to +63
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

fail 路径中 reject 可能不会执行;complete 的处理方式与 success/fail 不一致

两个问题:

  1. 如果用户提供的 fail 回调抛出异常(第 48 行),reject(err) 将不会执行,导致 Promise 永远处于 pending 状态。
  2. complete 直接透传给 wx(第 51 行),而 successfail 则被包装过。如果将来需要在 complete 中添加逻辑(或保证与 resolve/reject 的执行顺序一致),当前写法不便扩展。

建议统一包装所有回调,并对 fail 分支做防护处理:

🛠️ 建议修改
         return new Promise((resolve, reject) => {
           const { success, fail, complete, ...restOptions } = options
           wx.navigateTo({
             ...restOptions,
             success: (res) => {
               success?.(res)
+              complete?.(res)
               resolve(res)
             },
             fail: (err) => {
-              fail?.(err)
-              reject(err)
+              try {
+                fail?.(err)
+              } catch (e) {
+                // ignore
+              }
+              complete?.(err)
+              reject(err)
             },
-            complete
           })
         })
🤖 Prompt for AI Agents
In `@packages/taro-platform-weapp/src/apis.ts` around lines 39 - 53, 当前
wx.navigateTo 包装没有保护用户回调:调用 success/fail 前直接执行用户回调可能会抛出,导致 resolve/reject 不被调用,且
complete 未被统一包装。修改 wx.navigateTo 包装逻辑(在包裹的函数中操作 options 的
success/fail/complete)将 success/fail/complete 三个回调都用内部封装替换;在 success 回调中用
try/catch/ finally 或先行 resolve 再调用用户 success(或将用户回调包在 try/catch 并在 finally 中
resolve),在 fail 回调中保证不论用户 fail 回调是否抛出都执行 reject(即在 try { userFail?.(err) }
catch{} finally { reject(err) }),并把 complete 也替换为内部函数(在 resolve/reject 后统一调用用户
complete 并用 try/catch 包裹),并在代码中定位到 options, wx.navigateTo, success, fail,
complete 以应用这些改动。

}
}

// 非 Skyline 模式或无 routeType 参数时使用标准流程
return originalNavigateTo.call(taro, options)
}

taro.cloud = wx.cloud
taro.getTabBar = function (pageCtx) {
if (typeof pageCtx?.getTabBar === 'function') {
Expand Down
Loading