Skip to content

Commit

Permalink
fix(navigatePageTo): 兼容支付宝小程序
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 25, 2020
1 parent 7e84e77 commit f102307
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions src/mp/navigatePageTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,40 @@ export function navigatePageTo(
redirect?: boolean,
): Promise<any> {
return ensureInMiniProgram(mp => {
return new Promise((resolve, reject) => {
if (isUrl(url)) {
const { webUrlToMiniProgramUrl } = getMiniProgramConfig()
if (typeof webUrlToMiniProgramUrl === 'function') {
url = webUrlToMiniProgramUrl(url)
}
if (isUrl(url)) {
const { webUrlToMiniProgramUrl } = getMiniProgramConfig()
if (typeof webUrlToMiniProgramUrl === 'function') {
url = webUrlToMiniProgramUrl(url)
}
if (query && typeof query === 'object') {
const queryString = createUrlQueryString(query)
if (queryString) {
url += (url.indexOf('?') > -1 ? '&' : '?') + queryString
}
}
if (query && typeof query === 'object') {
const queryString = createUrlQueryString(query)
if (queryString) {
url += (url.indexOf('?') > -1 ? '&' : '?') + queryString
}
;(redirect
? ((mp.redirectTo as any) as typeof mp.navigateTo)
: mp.navigateTo)({
url: url,
success: resolve,
fail: () => {
mp.switchTab({
url: url,
success: resolve,
fail: reject,
})
},
}
const switchTab = () =>
new Promise((resolve, reject) => {
mp.switchTab({
url: url,
success: resolve,
fail: reject,
})
})
})
const navigateTo = () =>
new Promise((resolve, reject) => {
;(redirect
? ((mp.redirectTo as any) as typeof mp.navigateTo)
: mp.navigateTo)({
url: url,
success: resolve,
fail: reject,
})
})
// 支付宝下 navigateTo、redirectTo 也能跳转到 Tab 页并不会报错,只是不会显示 Tab 栏
if (mp.$brand === '支付宝') {
return switchTab().catch(navigateTo)
}
return navigateTo().catch(switchTab)
})
}

0 comments on commit f102307

Please sign in to comment.