// 需要登录的界面 let needLogin = [ "/pages/my/mySetting",// 个人资料 "/pages/mobile_web/workshops/index",// 工作区 "/pages/mobile_web/my/child_pages/contact",// 留言 "/pages/mobile_web/my/child_pages/share",// 发布记录 "/pages/mobile_web/my/child_pages/create",// 生成记录 "/pages/mobile_web/my/child_pages/recharge",// 充值记录 "/pages/mobile_web/recharge/recharge",// 支付列表 "/pages/mobile_web/recharge/detail",// 支付详情 "/pages/mobile_web/wall/wall",// 展示墙 ]; // 多端通用路由 let filterRoute = [ "/uview-ui/components/u-avatar-cropper/u-avatar-cropper", "/pages/index/rich-detail", "/pages/index/webview", ]; // 拦截类型 let list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]; const install = (vm) => { // #ifdef H5 // H5路由拦截,用于拦截用户地址栏输入地址 const token = vm.$store.state.vuex_token; let locationUrl = window.location.href.split("/#")[1]; // 参数下标——地址?下标 let quteIndex = locationUrl?.indexOf('?'); let quteUrl = locationUrl.substring(0,(!quteIndex||quteIndex===-1) ? locationUrl.length : quteIndex); if(needLogin.includes(quteUrl)&&!token){ uni.showToast({ title: '该页面需要登录才能访问,请先登录', icon: 'none' }); uni.navigateTo({ url: "/pages/mobile_web/my/child_pages/login?nextUrl="+locationUrl }); } else if(quteUrl!=='/'&&!filterRoute.includes(quteUrl)&&vm.$store.state.current_platform&& locationUrl.indexOf(vm.$store.state.current_platform)==-1){ uni.navigateTo({ url: `/pages/${vm.$store.state.current_platform}/index/index`, complete() { setTimeout(()=>{ uni.showToast({ title: '该设备端无法访问对应页面!', icon: 'none', }); },500) } }); } // #endif // uniapp跳转页面路由拦截器 list.forEach(item => { uni.addInterceptor(item, { invoke(e) { const token = vm.$store.state.vuex_token; // console.log(vm.$store.state.current_platform) const url = e.url.split('?')[0]; if (needLogin.includes(url) && !token) { uni.showToast({ title: '该页面需要登录才能访问,请先登录', icon: 'none' }); uni.navigateTo({ url: "/pages/mobile_web/my/child_pages/login?nextUrl="+e.url }); return false; } // #ifndef APP else if(!filterRoute.includes(url)&&url.indexOf(vm.$store.state.current_platform)===-1){ uni.showToast({ title: '该设备端无法访问对应页面!', icon: 'none' }); return false; } // #endif return true; } }); }); } export default { install }