import store from '@/store' import config from '@/common/config.js' // 获取跳转page的带参完成路径 function getPageUrl(url, params) { if (!url) { return null } if (params) { let paramsUrl = '' for (let item in params) { paramsUrl += item + '=' + params[item] + '&' } url += '?' + paramsUrl.substr(0, paramsUrl.length - 1) } return url } // 跳转到对应的page页面 function navigateToPage(url, params, requiredLogin = false, events = {}) { _goToPage(1, url, params, requiredLogin, events) } // 关闭当前页面,跳转到应用内的page页面 function redirectToPage(url, params, requiredLogin = false) { _goToPage(2, url, params, requiredLogin) } // 跳转到switchTab的page页面 function switchTabPage(url, params, requiredLogin = false) { _goToPage(3, url, params, requiredLogin) } function _goToPage(type, url, params, requiredLogin = false, events = {}) { if (requiredLogin && !store.getters.isLogin) { // if (requiredLogin && !uni.getStorageSync(config.const.access_token)) { showNeedLoginDialog() return } const wholeUrl = getPageUrl(url, params) if (!wholeUrl) { return } if (type === 1) { uni.navigateTo({ url: wholeUrl, events }) } else if (type === 2) { uni.redirectTo({ url: wholeUrl }) } else if (type === 3) { // switchTab不能传参 uni.switchTab({ url: wholeUrl }) } else { uni.showToast({ icon: 'none', title: '跳转类型错误!' }) } } // 返回 function navigateBack(backCallback) { if (!backCallback) { if (getCurrentPages().length > 1) { uni.navigateBack() } else { // uni.switchTab({ // url: config.route.index // }) uni.navigateTo({ url: config.route.index }) } return } backCallback() } function showNeedLoginDialog() { uni.showModal({ title: '提示', content: '您还未登录,确定跳转到登录页吗?', success: function (res) { if (res.confirm) { uni.navigateTo({ url: config.route.login }) } } }) } module.exports = { getPageUrl, navigateToPage, redirectToPage, switchTabPage, navigateBack, showNeedLoginDialog }