navigate.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import store from '@/store'
  2. import config from '@/common/config.js'
  3. // 获取跳转page的带参完成路径
  4. function getPageUrl(url, params) {
  5. if (!url) {
  6. return null
  7. }
  8. if (params) {
  9. let paramsUrl = ''
  10. for (let item in params) {
  11. paramsUrl += item + '=' + params[item] + '&'
  12. }
  13. url += '?' + paramsUrl.substr(0, paramsUrl.length - 1)
  14. }
  15. return url
  16. }
  17. // 跳转到对应的page页面
  18. function navigateToPage(url, params, requiredLogin = false, events = {}) {
  19. _goToPage(1, url, params, requiredLogin, events)
  20. }
  21. // 关闭当前页面,跳转到应用内的page页面
  22. function redirectToPage(url, params, requiredLogin = false) {
  23. _goToPage(2, url, params, requiredLogin)
  24. }
  25. // 跳转到switchTab的page页面
  26. function switchTabPage(url, params, requiredLogin = false) {
  27. _goToPage(3, url, params, requiredLogin)
  28. }
  29. function _goToPage(type, url, params, requiredLogin = false, events = {}) {
  30. if (requiredLogin && !store.getters.isLogin) {
  31. // if (requiredLogin && !uni.getStorageSync(config.const.access_token)) {
  32. showNeedLoginDialog()
  33. return
  34. }
  35. const wholeUrl = getPageUrl(url, params)
  36. if (!wholeUrl) {
  37. return
  38. }
  39. if (type === 1) {
  40. uni.navigateTo({
  41. url: wholeUrl,
  42. events
  43. })
  44. } else if (type === 2) {
  45. uni.redirectTo({
  46. url: wholeUrl
  47. })
  48. } else if (type === 3) {
  49. // switchTab不能传参
  50. uni.switchTab({
  51. url: wholeUrl
  52. })
  53. } else {
  54. uni.showToast({
  55. icon: 'none',
  56. title: '跳转类型错误!'
  57. })
  58. }
  59. }
  60. // 返回
  61. function navigateBack(backCallback) {
  62. if (!backCallback) {
  63. if (getCurrentPages().length > 1) {
  64. uni.navigateBack()
  65. } else {
  66. // uni.switchTab({
  67. // url: config.route.index
  68. // })
  69. uni.navigateTo({
  70. url: config.route.index
  71. })
  72. }
  73. return
  74. }
  75. backCallback()
  76. }
  77. function showNeedLoginDialog() {
  78. uni.showModal({
  79. title: '提示',
  80. content: '您还未登录,确定跳转到登录页吗?',
  81. success: function (res) {
  82. if (res.confirm) {
  83. uni.navigateTo({
  84. url: config.route.login
  85. })
  86. }
  87. }
  88. })
  89. }
  90. module.exports = {
  91. getPageUrl,
  92. navigateToPage,
  93. redirectToPage,
  94. switchTabPage,
  95. navigateBack,
  96. showNeedLoginDialog
  97. }