route.js 2.9 KB

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