Skip to content

Commit bff739c

Browse files
authored
Merge pull request #2350 from didi/feat-router-lock-addpath-v2
优化onload返回参数
2 parents c7777ff + 88017d4 commit bff739c

File tree

5 files changed

+86
-18
lines changed

5 files changed

+86
-18
lines changed

docs-vitepress/api/composition-api.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,67 @@ createComponent({
128128
`Function`
129129

130130
小程序页面 onLoad 事件,监听页面加载。
131+
::: tip 注意
132+
在页面拼接参数时,不同平台对 URL 参数的处理方式存在差异:
133+
- **微信、QQ、字节跳动、RN**:参数透传,保持原始格式
134+
- **其他平台**:会自动对 encode 后的参数进行 decode
135+
136+
为了提供统一的参数处理方式,`onLoad` 钩子额外提供了一个经过 encode 处理的参数对象,方便业务使用统一的参数格式,同时不影响各平台原有的参数返回。
137+
138+
**微信、QQ、字节跳动、RN下的效果:**
139+
```js
140+
// A页面
141+
...
142+
createPage({
143+
methods: {
144+
jumpPage () {
145+
mpx.navigateTo({
146+
url: `./pages/test?name=${encodeURIComponent('')}&encode=true`
147+
})
148+
}
149+
}
150+
})
151+
...
152+
153+
// test页面
154+
...
155+
createPage({
156+
onLoad(rawQuery, decodedQuery) {
157+
// rawQuery = {name: "%E6%88%91", encode: "true"}
158+
// decodedQuery = {name: "我", encode: "true"}
159+
...
160+
}
161+
})
162+
...
163+
```
164+
**其他平台的效果:**
165+
```js
166+
// A页面
167+
...
168+
createPage({
169+
methods: {
170+
jumpPage () {
171+
mpx.navigateTo({
172+
url: `./pages/test?name=${encodeURIComponent('')}&encode=true`
173+
})
174+
}
175+
}
176+
})
177+
...
178+
179+
// test页面
180+
...
181+
createPage({
182+
onLoad(rawQuery, decodedQuery) {
183+
// rawQuery = {name: "我", encode: "true"}
184+
// decodedQuery = {name: "我", encode: "true"}
185+
...
186+
}
187+
})
188+
...
189+
```
190+
191+
:::
131192

132193
### onShow
133194
`Function`

packages/api-proxy/src/platform/api/route/index.ios.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function navigateTo (options = {}) {
4343
if (options.events) {
4444
eventChannel._addListeners(options.events)
4545
}
46-
const { path, queryObj } = parseUrl(options.url)
46+
const { path, queryObj } = parseUrl(options.url, true)
4747
const basePath = getBasePath(navigation)
4848
const finalPath = resolvePath(path, basePath).slice(1)
4949

@@ -70,7 +70,7 @@ function redirectTo (options = {}) {
7070
return
7171
}
7272
if (navigation && navigationHelper) {
73-
const { path, queryObj } = parseUrl(options.url)
73+
const { path, queryObj } = parseUrl(options.url, true)
7474
const basePath = getBasePath(navigation)
7575
const finalPath = resolvePath(path, basePath).slice(1)
7676
navigation.replace(finalPath, queryObj)
@@ -120,7 +120,7 @@ function reLaunch (options = {}) {
120120
return
121121
}
122122
if (navigation && navigationHelper) {
123-
const { path, queryObj } = parseUrl(options.url)
123+
const { path, queryObj } = parseUrl(options.url, true)
124124
const basePath = getBasePath(navigation)
125125
const finalPath = resolvePath(path, basePath).slice(1)
126126
navigation.reset({

packages/core/src/platform/builtInMixins/pageStatusMixin.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ export default function pageStatusMixin (mixinType) {
1313
onResize (e) {
1414
this.__mpxProxy.callHook(ONRESIZE, [e])
1515
},
16-
onLoad (query) {
17-
if (__mpx_mode__ === 'wx') {
18-
const loadParams = {}
19-
// 此处单独处理微信与其他端保持一致,传入onload的参数都是经过decodeURIComponent处理过的
20-
if (isObject(query)) {
21-
for (const key in query) {
22-
loadParams[key] = decodeURIComponent(query[key])
16+
onLoad (rawQuery) {
17+
if (__mpx_mode__ === 'wx' || __mpx_mode__ === 'qq' || __mpx_mode__ === 'tt') {
18+
const decodedQuery = {}
19+
// 处理以上平台直接透传encode的结果,给到onload第二个参数供开发者使用
20+
if (isObject(rawQuery)) {
21+
for (const key in rawQuery) {
22+
decodedQuery[key] = decodeURIComponent(rawQuery[key])
2323
}
2424
}
25-
this.__mpxProxy.callHook(ONLOAD, [loadParams])
25+
this.__mpxProxy.callHook(ONLOAD, [rawQuery, decodedQuery])
2626
} else {
27-
this.__mpxProxy.callHook(ONLOAD, [query])
27+
this.__mpxProxy.callHook(ONLOAD, [rawQuery, rawQuery])
2828
}
2929
}
3030
}

packages/core/src/platform/patch/getDefaultOptions.ios.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,14 @@ function createInstance ({ propsRef, type, rawOptions, currentInject, validProps
314314

315315
if (type === 'page') {
316316
const props = propsRef.current
317-
proxy.callHook(ONLOAD, [props.route.params || {}])
317+
const decodedQuery = {}
318+
const rawQuery = props.route.params
319+
if (isObject(rawQuery)) {
320+
for (const key in rawQuery) {
321+
decodedQuery[key] = decodeURIComponent(rawQuery[key])
322+
}
323+
}
324+
proxy.callHook(ONLOAD, [rawQuery, decodedQuery])
318325
}
319326

320327
Object.assign(proxy, {

packages/utils/src/url.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const specialValues = {
8080
false: false
8181
}
8282

83-
function parseQuery (query) {
83+
function parseQuery (query, noDecode = false) {
8484
if (query.slice(0, 1) !== '?') {
8585
throw new Error(
8686
"A valid query string passed to parseQuery should begin with '?'"
@@ -104,8 +104,8 @@ function parseQuery (query) {
104104
const idx = arg.indexOf('=')
105105

106106
if (idx >= 0) {
107-
let name = decode(arg.slice(0, idx))
108-
let value = decode(arg.slice(idx + 1))
107+
let name = noDecode ? arg.slice(0, idx) : decode(arg.slice(0, idx))
108+
let value = noDecode ? arg.slice(idx + 1) : decode(arg.slice(idx + 1))
109109

110110
// eslint-disable-next-line no-prototype-builtins
111111
if (specialValues.hasOwnProperty(value)) {
@@ -137,15 +137,15 @@ function parseQuery (query) {
137137
return result
138138
}
139139

140-
function parseUrlQuery (url) {
140+
function parseUrlQuery (url, noDecode = false) {
141141
let path = url
142142
let query = ''
143143
const queryIndex = url.indexOf('?')
144144
if (queryIndex >= 0) {
145145
path = url.slice(0, queryIndex)
146146
query = url.slice(queryIndex)
147147
}
148-
const queryObj = parseQuery(query || '?')
148+
const queryObj = parseQuery(query || '?', noDecode)
149149
return {
150150
path,
151151
queryObj

0 commit comments

Comments
 (0)