route.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var common_vendor = require("../../../../common/vendor.js");
  3. var uni_modules_uviewPlus_libs_function_index = require("../function/index.js");
  4. class Router {
  5. constructor() {
  6. this.config = {
  7. type: "navigateTo",
  8. url: "",
  9. delta: 1,
  10. params: {},
  11. animationType: "pop-in",
  12. animationDuration: 300,
  13. intercept: false
  14. };
  15. this.route = this.route.bind(this);
  16. }
  17. addRootPath(url) {
  18. return url[0] === "/" ? url : `/${url}`;
  19. }
  20. mixinParam(url, params) {
  21. url = url && this.addRootPath(url);
  22. let query = "";
  23. if (/.*\/.*\?.*=.*/.test(url)) {
  24. query = uni_modules_uviewPlus_libs_function_index.queryParams(params, false);
  25. return url += `&${query}`;
  26. }
  27. query = uni_modules_uviewPlus_libs_function_index.queryParams(params);
  28. return url += query;
  29. }
  30. async route(options = {}, params = {}) {
  31. let mergeConfig = {};
  32. if (typeof options === "string") {
  33. mergeConfig.url = this.mixinParam(options, params);
  34. mergeConfig.type = "navigateTo";
  35. } else {
  36. mergeConfig = uni_modules_uviewPlus_libs_function_index.deepMerge(this.config, options);
  37. mergeConfig.url = this.mixinParam(options.url, options.params);
  38. }
  39. if (mergeConfig.url === uni_modules_uviewPlus_libs_function_index.page())
  40. return;
  41. if (params.intercept) {
  42. this.config.intercept = params.intercept;
  43. }
  44. mergeConfig.params = params;
  45. mergeConfig = uni_modules_uviewPlus_libs_function_index.deepMerge(this.config, mergeConfig);
  46. if (typeof common_vendor.index.$u.routeIntercept === "function") {
  47. const isNext = await new Promise((resolve, reject) => {
  48. common_vendor.index.$u.routeIntercept(mergeConfig, resolve);
  49. });
  50. isNext && this.openPage(mergeConfig);
  51. } else {
  52. this.openPage(mergeConfig);
  53. }
  54. }
  55. openPage(config) {
  56. const {
  57. url,
  58. type,
  59. delta,
  60. animationType,
  61. animationDuration
  62. } = config;
  63. if (config.type == "navigateTo" || config.type == "to") {
  64. common_vendor.index.navigateTo({
  65. url,
  66. animationType,
  67. animationDuration
  68. });
  69. }
  70. if (config.type == "redirectTo" || config.type == "redirect") {
  71. common_vendor.index.redirectTo({
  72. url
  73. });
  74. }
  75. if (config.type == "switchTab" || config.type == "tab") {
  76. common_vendor.index.switchTab({
  77. url
  78. });
  79. }
  80. if (config.type == "reLaunch" || config.type == "launch") {
  81. common_vendor.index.reLaunch({
  82. url
  83. });
  84. }
  85. if (config.type == "navigateBack" || config.type == "back") {
  86. common_vendor.index.navigateBack({
  87. delta
  88. });
  89. }
  90. }
  91. }
  92. var route = new Router().route;
  93. exports.route = route;