router.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import store from '@/store'
  2. export default {
  3. parseUrl(page, param) {
  4. let params = ''
  5. if (param) {
  6. params = '?'
  7. for (let x in param) {
  8. params += (params === '?' ? '' : '&') + x + '=' + param[x]
  9. }
  10. }
  11. return `/pages/${page}/index${params}`
  12. },
  13. push(page, param) {
  14. let url = this.parseUrl(page, param)
  15. uni.navigateTo({
  16. url
  17. })
  18. },
  19. replace(page, param) {
  20. let url = this.parseUrl(page, param)
  21. uni.redirectTo({
  22. url
  23. })
  24. },
  25. switchTab(page, param) {
  26. let url = this.parseUrl(page, param)
  27. uni.switchTab({
  28. url
  29. })
  30. },
  31. pushCheck(page, param) {
  32. if (!store.state.token) {
  33. this.push('login')
  34. return
  35. }
  36. this.push(page, param)
  37. },
  38. replaceCheck(page, param) {
  39. if (!store.state.token) {
  40. this.push('login')
  41. return
  42. }
  43. this.replace(page, param)
  44. },
  45. back() {
  46. let curPages = getCurrentPages()
  47. if (curPages.length > 1) {
  48. uni.navigateBack({
  49. delta: 1
  50. })
  51. } else {
  52. uni.reLaunch({
  53. url: '/pages/award/index'
  54. })
  55. }
  56. },
  57. index() {
  58. uni.reLaunch({
  59. url: '/pages/award/index'
  60. })
  61. },
  62. web(url, title) {
  63. uni.navigateTo({
  64. url:`/pages/web/index?title=${title}&url=${encodeURIComponent(url)}`
  65. })
  66. // this.push('web', {
  67. // title,
  68. // url: encodeURIComponent(url)
  69. // })
  70. },
  71. login() {
  72. const pages = getCurrentPages()
  73. const page = pages[pages.length - 1]
  74. if (page.route === 'pages/login/index') {
  75. return
  76. }
  77. this.push('login')
  78. }
  79. }