route.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. "use strict";
  2. const common_vendor = require("../../../../common/vendor.js");
  3. const 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. // navigateBack页面后退时,回退的层数
  11. params: {},
  12. // 传递的参数
  13. animationType: "pop-in",
  14. // 窗口动画,只在APP有效
  15. animationDuration: 300,
  16. // 窗口动画持续时间,单位毫秒,只在APP有效
  17. intercept: false
  18. // 是否需要拦截
  19. };
  20. this.route = this.route.bind(this);
  21. }
  22. // 判断url前面是否有"/",如果没有则加上,否则无法跳转
  23. addRootPath(url) {
  24. return url[0] === "/" ? url : `/${url}`;
  25. }
  26. // 整合路由参数
  27. mixinParam(url, params) {
  28. url = url && this.addRootPath(url);
  29. let query = "";
  30. if (/.*\/.*\?.*=.*/.test(url)) {
  31. query = uni_modules_uviewPlus_libs_function_index.queryParams(params, false);
  32. return url += `&${query}`;
  33. }
  34. query = uni_modules_uviewPlus_libs_function_index.queryParams(params);
  35. return url += query;
  36. }
  37. // 对外的方法名称
  38. async route(options = {}, params = {}) {
  39. let mergeConfig = {};
  40. if (typeof options === "string") {
  41. mergeConfig.url = this.mixinParam(options, params);
  42. mergeConfig.type = "navigateTo";
  43. } else {
  44. mergeConfig = uni_modules_uviewPlus_libs_function_index.deepMerge(this.config, options);
  45. mergeConfig.url = this.mixinParam(options.url, options.params);
  46. }
  47. if (mergeConfig.url === uni_modules_uviewPlus_libs_function_index.page())
  48. return;
  49. if (params.intercept) {
  50. this.config.intercept = params.intercept;
  51. }
  52. mergeConfig.params = params;
  53. mergeConfig = uni_modules_uviewPlus_libs_function_index.deepMerge(this.config, mergeConfig);
  54. if (typeof common_vendor.index.$u.routeIntercept === "function") {
  55. const isNext = await new Promise((resolve, reject) => {
  56. common_vendor.index.$u.routeIntercept(mergeConfig, resolve);
  57. });
  58. isNext && this.openPage(mergeConfig);
  59. } else {
  60. this.openPage(mergeConfig);
  61. }
  62. }
  63. // 执行路由跳转
  64. openPage(config) {
  65. const {
  66. url,
  67. type,
  68. delta,
  69. animationType,
  70. animationDuration
  71. } = config;
  72. if (config.type == "navigateTo" || config.type == "to") {
  73. common_vendor.index.navigateTo({
  74. url,
  75. animationType,
  76. animationDuration
  77. });
  78. }
  79. if (config.type == "redirectTo" || config.type == "redirect") {
  80. common_vendor.index.redirectTo({
  81. url
  82. });
  83. }
  84. if (config.type == "switchTab" || config.type == "tab") {
  85. common_vendor.index.switchTab({
  86. url
  87. });
  88. }
  89. if (config.type == "reLaunch" || config.type == "launch") {
  90. common_vendor.index.reLaunch({
  91. url
  92. });
  93. }
  94. if (config.type == "navigateBack" || config.type == "back") {
  95. common_vendor.index.navigateBack({
  96. delta
  97. });
  98. }
  99. }
  100. }
  101. const route = new Router().route;
  102. exports.route = route;