router.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. spacelog: '/pages/spacelog/index',
  14. ipdauth: '/pages/ipdauth/index'
  15. }
  16. function to(page, data) {
  17. if (!pages[page]) { throw new Error(`${page} is not exist!`) }
  18. const _result = []
  19. for (const key in data) {
  20. const value = data[key]
  21. if (['', undefined, null].includes(value)) {
  22. continue
  23. }
  24. if (value.constructor === Array) {
  25. value.forEach(_value => {
  26. _result.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(_value))
  27. })
  28. } else {
  29. _result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
  30. }
  31. }
  32. const url = pages[page] + (_result.length ? `?${_result.join('&')}` : '')
  33. return url
  34. }
  35. class Router {
  36. push(page, param = {}, events = {}, callback = () => { }) {
  37. wx.navigateTo({
  38. url: to(page, param),
  39. events,
  40. success: callback
  41. })
  42. }
  43. pop(delta) {
  44. wx.navigateBack({ delta })
  45. }
  46. redirectTo(page, param) {
  47. wx.redirectTo({ url: to(page, param) })
  48. }
  49. reLaunch() {
  50. wx.reLaunch({ url: pages.home })
  51. }
  52. toHome() {
  53. this.reLaunch()
  54. // if (getCurrentPages().length > 1) { this.pop() } else { this.reLaunch() }
  55. }
  56. }
  57. export default new Router()