猎豆优选小程序

router.js 2.8KB

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