/** * 路由守卫封装 * @param {*} routerData 跳转参数 */ // 登录页面url const loginUrl = '/pages/subPackages/login/loginPhone' // 不需要登录校验的路由地址 const whiteList = [ '/pages/home/home', '/pages/optimization/optimization', '/pages/my/my', ]; // 保留当前页面,跳转到应用内的某个页面 const navigateTo = async routerData => { let { url } = routerData; if (url){ url = url.split("?")[0]; if (whiteList.includes(url)) { uni.navigateTo(routerData); } else { const user_id = uni.getStorageSync('user_id') const token = uni.getStorageSync('token') uni.setStorageSync("beforeLoginUrl", routerData); if (user_id && token) { uni.navigateTo(routerData); } else { uni.navigateTo({ url: loginUrl }); } } } }; // 关闭当前页面,跳转到应用内的某个页面。 const redirectTo = async routerData => { let { url } = routerData; if (url) { url = url.split("?")[0]; if (whiteList.includes(url)) { uni.redirectTo(routerData); } else { const user_id = uni.getStorageSync('user_id') const token = uni.getStorageSync('token') uni.setStorageSync("beforeLoginUrl", routerData); if (user_id && token) { uni.redirectTo(routerData); } else { uni.navigateTo({ url: loginUrl }); } } } }; // 关闭所有页面,打开到应用内的某个页面。 const reLaunch = async routerData => { let { url } = routerData; if (url) { url = url.split("?")[0]; if (whiteList.includes(url)) { uni.reLaunch(routerData); } else { const user_id = uni.getStorageSync('user_id') const token = uni.getStorageSync('token') uni.setStorageSync("beforeLoginUrl", routerData); if (user_id && token) { uni.reLaunch(routerData); } else { uni.navigateTo({ url: loginUrl }); } } } }; // 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。 const switchTab = async routerData => { let { url } = routerData; if (url) { url = url.split("?")[0]; if (whiteList.includes(url)) { uni.switchTab(routerData); } else { const user_id = uni.getStorageSync('user_id') const token = uni.getStorageSync('token') uni.setStorageSync("beforeLoginUrl", routerData); if (user_id && token) { uni.switchTab(routerData); } else { uni.navigateTo({ url: loginUrl }); } } } }; // 关闭当前页面,返回上一页面或多级页面 const navigateBack = routerData => { uni.navigateBack(routerData); }; module.exports = { navigateTo, redirectTo, reLaunch, switchTab, navigateBack, }