router.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. const pages = {
  2. home: '/pages/index/index',
  3. adjust: '/pages/adjust/index',
  4. detail:'/pages/detail/index',
  5. auth:'/pages/auth/index',
  6. search:'/pages/search/index',
  7. projectlist:'/pages/projectlist/index',
  8. usercenter:'/pages/usercenter/index',
  9. feedback:'/pages/feedback/index',
  10. spacelist:'/pages/spacelist/index',
  11. search:'/pages/search/index',
  12. adjustlog:'/pages/adjustlog/index'
  13. }
  14. function to(page, data) {
  15. if (!pages[page]) { throw new Error(`${page} is not exist!`) }
  16. const _result = []
  17. for (const key in data) {
  18. const value = data[key]
  19. if (['', undefined, null].includes(value)) {
  20. continue
  21. }
  22. if (value.constructor === Array) {
  23. value.forEach(_value => {
  24. _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
  25. })
  26. } else {
  27. _result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
  28. }
  29. }
  30. const url = pages[page] + (_result.length ? `?${_result.join('&')}` : '')
  31. return url
  32. }
  33. class Router {
  34. push(page, param = {}, events = {}, callback = () => {}) {
  35. wx.navigateTo({
  36. url: to(page, param),
  37. events,
  38. success: callback
  39. })
  40. }
  41. pop(delta) {
  42. wx.navigateBack({ delta })
  43. }
  44. redirectTo(page, param) {
  45. wx.redirectTo({ url: to(page, param) })
  46. }
  47. reLaunch() {
  48. wx.reLaunch({ url: pages.home })
  49. }
  50. toHome() {
  51. this.reLaunch()
  52. // if (getCurrentPages().length > 1) { this.pop() } else { this.reLaunch() }
  53. }
  54. }
  55. export default new Router()