uniapp登录后重定向页面的处理
·
登录页面
// 页面加载时获取重定向参数
onLoad((options) => {
if (options?.redirect) {
let path = decodeURIComponent(options.redirect)
// 确保路径以 / 开头
if (!path.startsWith('/')) {
path = '/' + path
}
redirectPath.value = path
console.log('登录后将跳转到:', redirectPath.value)
}
})
// 登录成功后的跳转逻辑
if (redirectPath.value) {
// 确保路径以 / 开头
const targetPath = redirectPath.value.startsWith('/')
? redirectPath.value
: '/' + redirectPath.value
// 判断是否是 tabBar 页面
const tabBarPages = ['/pages/teacher-courses/index', '/pages/index/index']
const isTabBar = tabBarPages.includes(targetPath)
if (isTabBar) {
uni.switchTab({ url: targetPath })
} else {
uni.redirectTo({
url: targetPath, fail: () => {
// 如果失败,尝试切换到个人中心
uni.switchTab({
url: '/pages/profile/index'
})
}
})
}
} else {
// 无重定向,默认跳转到个人中心
uni.switchTab({
url: '/pages/profile/index'
})
}
跳转页面
// 获取当前页面名称
const getRouterPathName = () => {
// 获取当前页面栈实例数组
var pages = getCurrentPages();
// 获取当前页面的实例
let currentPage = pages[pages.length - 1];
// 获取当前页面的路由
let currentRoute = currentPage.route;
return currentRoute
}
//方法中调用
const toLogin = () => {
if (!isLogin.value) {
const currentPath = getRouterPathName()
uni.navigateTo({
url: `/pages/login/index?redirect=${encodeURIComponen(currentPath)}`
})
return
}
}
更多推荐
所有评论(0)