抖音平台小程序

router.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * 路由守卫封装
  3. * @param {*} routerData 跳转参数
  4. */
  5. // 登录页面url
  6. const loginUrl = '/pages/subPackages/login/loginPhone'
  7. // 不需要登录校验的路由地址
  8. const whiteList = [
  9. '/pages/home/home',
  10. '/pages/optimization/optimization',
  11. '/pages/my/my',
  12. ];
  13. // 保留当前页面,跳转到应用内的某个页面
  14. const navigateTo = async routerData => {
  15. let { url } = routerData;
  16. if (url){
  17. url = url.split("?")[0];
  18. if (whiteList.includes(url)) {
  19. uni.navigateTo(routerData);
  20. } else {
  21. const user_id = uni.getStorageSync('user_id')
  22. const token = uni.getStorageSync('token')
  23. uni.setStorageSync("beforeLoginUrl", routerData);
  24. if (user_id && token) {
  25. uni.navigateTo(routerData);
  26. } else {
  27. uni.navigateTo({ url: loginUrl });
  28. }
  29. }
  30. }
  31. };
  32. // 关闭当前页面,跳转到应用内的某个页面。
  33. const redirectTo = async routerData => {
  34. let { url } = routerData;
  35. if (url) {
  36. url = url.split("?")[0];
  37. if (whiteList.includes(url)) {
  38. uni.redirectTo(routerData);
  39. } else {
  40. const user_id = uni.getStorageSync('user_id')
  41. const token = uni.getStorageSync('token')
  42. uni.setStorageSync("beforeLoginUrl", routerData);
  43. if (user_id && token) {
  44. uni.redirectTo(routerData);
  45. } else {
  46. uni.navigateTo({ url: loginUrl });
  47. }
  48. }
  49. }
  50. };
  51. // 关闭所有页面,打开到应用内的某个页面。
  52. const reLaunch = async routerData => {
  53. let { url } = routerData;
  54. if (url) {
  55. url = url.split("?")[0];
  56. if (whiteList.includes(url)) {
  57. uni.reLaunch(routerData);
  58. } else {
  59. const user_id = uni.getStorageSync('user_id')
  60. const token = uni.getStorageSync('token')
  61. uni.setStorageSync("beforeLoginUrl", routerData);
  62. if (user_id && token) {
  63. uni.reLaunch(routerData);
  64. } else {
  65. uni.navigateTo({ url: loginUrl });
  66. }
  67. }
  68. }
  69. };
  70. // 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
  71. const switchTab = async routerData => {
  72. let { url } = routerData;
  73. if (url) {
  74. url = url.split("?")[0];
  75. if (whiteList.includes(url)) {
  76. uni.switchTab(routerData);
  77. } else {
  78. const user_id = uni.getStorageSync('user_id')
  79. const token = uni.getStorageSync('token')
  80. uni.setStorageSync("beforeLoginUrl", routerData);
  81. if (user_id && token) {
  82. uni.switchTab(routerData);
  83. } else {
  84. uni.navigateTo({ url: loginUrl });
  85. }
  86. }
  87. }
  88. };
  89. // 关闭当前页面,返回上一页面或多级页面
  90. const navigateBack = routerData => {
  91. uni.navigateBack(routerData);
  92. };
  93. module.exports = {
  94. navigateTo,
  95. redirectTo,
  96. reLaunch,
  97. switchTab,
  98. navigateBack,
  99. }