fix(util-navigate): 完善鸿蒙元服务编码逻辑注释与说明

更新了util-navigate和对应测试文件的注释,补充鸿蒙原生编码的细节,说明双重编码的问题以及&符号的处理逻辑,修正了注释里的重复表述
This commit is contained in:
2026-07-18 09:24:10 +08:00
parent f3032a6004
commit 1143ef7b72
2 changed files with 7 additions and 4 deletions

View File

@@ -701,13 +701,15 @@ export function _safeEncodeForPlatform(v) {
// 微信小程序: split on '&' / '=' 解析 url,但不 decode value 给 onLoad。
// - 中文 / 字母 / 数字 → 原样保留(否则 onLoad 拿到 %xx 乱码)
// - `&` `=` `?` `#` 空格 → 必须 encode(否则 query 边界被破坏, src 等含 URL 的字段被截断)
// - `&` `=` `?` `#` 空格 → 必须 encode(否则 query 边界被破坏, src 等含 URL 的字段被截断,webview 打不开)
if (platform === 'mp-weixin') {
return _encodeUrlDelimiters(once);
}
// 鸿蒙元服务: 整体 encode url(包括中文和已编码的 %xx)。
// 鸿蒙元服务: uni.navigateTo 内部整体 encode url(把中文、& 等都 encode)。
// JS 侧完全不 encode,直接返回 decode roundtrip 后的原文,让鸿蒙原生做唯一一次 encode。
// (再 encode 会双重编码 → onLoad 拿到 %E6%B2%99%E5%8F%91 乱码)
// webview URL 含 & 时, JS 不 encode,鸿蒙原生 encode 后 & 变 %26,webview 能正常打开。
if (platform === 'mp-harmony') {
return once;
}

View File

@@ -240,8 +240,9 @@ test('_safeEncodeForPlatform: H5 / 华为快应用 → 主动 encode', () => {
}
});
test('_safeEncodeForPlatform: 鸿蒙元服务 → JS 侧不做 encode', () => {
// 2026-07-17 修正(二次): 鸿蒙整体 encode url, JS 完全不 encode
test('_safeEncodeForPlatform: 鸿蒙元服务 → JS 侧不做 encode(让平台去做)', () => {
// 2026-07-17 修正: 鸿蒙整体 encode url, JS 完全不 encode
// (再 encode 会双重编码; webview src 含 & 时,鸿蒙原生 encode 会自动变 %26,webview 能正常打开)
const nav = loadNavInSandbox();
const api = nav.createNavigateApi({ platform: 'mp-harmony' });
const r = api._safeEncodeForPlatform('a&b');