router.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // 对象转为路径参数
  2. export const objToUrlParams = (obj) => {
  3. if (typeof obj === "string") return obj;
  4. let result = "?";
  5. for (const element in obj) {
  6. result += `${element}=${obj[element]}&`;
  7. }
  8. return result.substring(0, result.length - 1);
  9. };
  10. // 将字符串转为对象
  11. export const urlParamsToObj = (url) => {
  12. const params = url.split("?")[1];
  13. if (!params) return {};
  14. const paramsArr = params.split("&");
  15. const result = {};
  16. paramsArr.forEach((item) => {
  17. const [key, value] = item.split("=");
  18. result[key] = value;
  19. });
  20. return result;
  21. };
  22. // 参数归一化路由参数
  23. const normalizeParams = (url, options) => {
  24. let normalizeOptions = {
  25. url: "",
  26. };
  27. if (typeof url === "string") {
  28. normalizeOptions = {
  29. url,
  30. ...options,
  31. };
  32. } else {
  33. normalizeOptions = url;
  34. }
  35. const params = {
  36. redirected: getRoute().routeList[0].path,
  37. ...normalizeOptions.params,
  38. };
  39. normalizeOptions.url += objToUrlParams(params);
  40. delete normalizeOptions.params;
  41. return normalizeOptions;
  42. };
  43. // 路由映射表
  44. export const router = {
  45. /**
  46. * 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。
  47. */
  48. push(url, options) {
  49. const normalizeOptions = normalizeParams(url, options);
  50. uni.navigateTo(normalizeOptions);
  51. },
  52. /**
  53. * 关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。
  54. */
  55. redirectTo(url, options) {
  56. const normalizeOptions = normalizeParams(url, options);
  57. uni.redirectTo(normalizeOptions);
  58. },
  59. /**
  60. * 关闭所有页面,打开到应用内的某个页面。
  61. */
  62. reLaunch(url, options) {
  63. const normalizeOptions = normalizeParams(url, options);
  64. uni.reLaunch(normalizeOptions);
  65. },
  66. /**
  67. * 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面
  68. */
  69. switchTab(url, options) {
  70. const normalizeOptions = normalizeParams(url, options);
  71. uni.switchTab(normalizeOptions);
  72. },
  73. /**
  74. * 关闭当前页面,返回上一页面或多级页面。
  75. */
  76. back() {
  77. const route = getRoute().routeList;
  78. if (route.length > 1) {
  79. uni.navigateBack();
  80. return;
  81. }
  82. const { redirected, ...params } = route[0].params;
  83. if (redirected) {
  84. this.reLaunch({
  85. url: `/${redirected}`,
  86. params,
  87. });
  88. }
  89. },
  90. };
  91. // 获取当前路由参数
  92. export const getRoute = () => {
  93. const pages = getCurrentPages();
  94. const routeList = pages
  95. .map((item, index) => {
  96. // 处理路径乱码问题
  97. // 解决乱码问题
  98. for (const param in item.options) {
  99. item.options[param] = decodeURIComponent(item.options[param]);
  100. }
  101. return {
  102. params: item.options,
  103. path: item.route,
  104. index,
  105. };
  106. })
  107. .reverse();
  108. return {
  109. routeList,
  110. params: routeList[0].params,
  111. };
  112. };