123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- /**
- * 路由守卫封装
- * @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,
- }
|