const pages = { home: '/pages/index/index', adjust: '/pages/adjust/index', detail:'/pages/detail/index', auth:'/pages/auth/index', search:'/pages/search/index', projectlist:'/pages/projectlist/index', usercenter:'/pages/usercenter/index', feedback:'/pages/feedback/index', spacelist:'/pages/spacelist/index', search:'/pages/search/index', adjustlog:'/pages/adjustlog/index', spacelog:'/pages/spacelog/index', } function to(page, data) { if (!pages[page]) { throw new Error(`${page} is not exist!`) } const _result = [] for (const key in data) { const value = data[key] if (['', undefined, null].includes(value)) { continue } if (value.constructor === Array) { value.forEach(_value => { _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value)) }) } else { _result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value)) } } const url = pages[page] + (_result.length ? `?${_result.join('&')}` : '') return url } class Router { push(page, param = {}, events = {}, callback = () => {}) { wx.navigateTo({ url: to(page, param), events, success: callback }) } pop(delta) { wx.navigateBack({ delta }) } redirectTo(page, param) { wx.redirectTo({ url: to(page, param) }) } reLaunch() { wx.reLaunch({ url: pages.home }) } toHome() { this.reLaunch() // if (getCurrentPages().length > 1) { this.pop() } else { this.reLaunch() } } } export default new Router()