index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { isH5, formatConfig, appPlatform } from './helpers/util';
  2. import navjump from './helpers/navJump';
  3. import { H5GetPageRoute } from './vueRouter/util';
  4. import { APPGetPageRoute } from './appRouter/util';
  5. import { AppletsPageRoute } from './appletsRouter/util';
  6. import { lifeCycle, Global } from './helpers/config';
  7. import { warn, err } from './helpers/warn';
  8. import { registerRouterHooks, registerHook } from './lifeCycle/hooks';
  9. import { vueMount } from './vueRouter/base';
  10. import appletsMount from './patch/applets-patch';
  11. import appMount from './patch/app-patch';
  12. import initMixins from './helpers/mixins';
  13. import ParseQuery from './helpers/urlQuery';
  14. // #ifdef H5
  15. import H5 from './patch/h5-patch';
  16. // #endif
  17. let H5PATCH = null;
  18. // #ifdef H5
  19. H5PATCH = new H5(isH5());
  20. // #endif
  21. const parseQuery = new ParseQuery();
  22. Global.H5RouterReady = new Promise((resolve) => Global.RouterReadyPromise = resolve);
  23. class Router {
  24. constructor(arg) {
  25. Router.$root = this;
  26. Global.Router = this; // 全局缓存一个对象,不必使用时都传递
  27. Global.$parseQuery = parseQuery;
  28. this.CONFIG = formatConfig(arg);
  29. this.lifeCycle = lifeCycle;
  30. registerRouterHooks.call(this); // 注册全局Router生命钩子
  31. if (appPlatform() === 'H5') {
  32. H5PATCH.setLoadingStatus(this.CONFIG.h5);
  33. }
  34. }
  35. get $Route() {
  36. return this.getPageRoute();
  37. }
  38. /**
  39. * 获取 url 参数帮助类实例
  40. */
  41. get $parseQuery() {
  42. return Global.$parseQuery;
  43. }
  44. /**
  45. * 获取当前是否处于正在跳转的状态
  46. * H5 状态下始终为false
  47. */
  48. get $lockStatus() {
  49. return Global.LockStatus;
  50. }
  51. /**
  52. * 动态设置拦截状态
  53. */
  54. set $lockStatus(status) {
  55. warn('你确定要这么做?你知道后果?', true);
  56. Global.LockStatus = status;
  57. }
  58. /** 动态的导航到一个新 URL 保留浏览历史
  59. * navigateTo
  60. * @param {Object} rule
  61. */
  62. push(rule) {
  63. navjump.call(this, rule, 'push');
  64. }
  65. /** 动态的导航到一个新 URL 关闭当前页面,跳转到的某个页面。
  66. * redirectTo
  67. * @param {Object} rule
  68. */
  69. replace(rule) {
  70. navjump.call(this, rule, 'replace');
  71. }
  72. /** 动态的导航到一个新 URL 关闭所有页面,打开到应用内的某个页面
  73. * reLaunch
  74. * @param {Object} rule
  75. */
  76. replaceAll(rule) {
  77. navjump.call(this, rule, 'replaceAll');
  78. }
  79. /** 动态的导航到一个新 url 关闭所有页面,打开到应用内的某个tab
  80. * @param {Object} rule
  81. */
  82. pushTab(rule) {
  83. navjump.call(this, rule, 'pushTab');
  84. }
  85. /**
  86. * 返回到指定层级页面上
  87. * @param {Number} backLayer 需要返回的页面层级 默认 1
  88. * @param {Object} delta 暂时无用
  89. * @param {enforce} 是否强制越过跳转加锁检查 默认 false
  90. */
  91. back(backLayer = 1, delta, enforce = false) {
  92. if (backLayer.constructor != Number) {
  93. return err(
  94. `返回层级参数必须是一个Number类型且必须大于1:${backLayer}`,
  95. );
  96. }
  97. navjump.call(this, {
  98. backLayer, delta, H5PATCH,
  99. }, 'back', true, enforce);
  100. }
  101. /**
  102. * 获取当前页面下的 Route 信息
  103. *
  104. * @param {Object} Vim 当前开发者可以传递一个 vue 组件对象 来获取当前下的 Route 信息
  105. */
  106. getPageRoute(Vim) {
  107. const pages = getCurrentPages();
  108. switch (appPlatform(true)) {
  109. case 'H5':
  110. return H5GetPageRoute.call(this, pages, Vim);
  111. case 'APP':
  112. return APPGetPageRoute(pages, Vim);
  113. case 'APPLETS':
  114. return AppletsPageRoute(pages, Vim);
  115. default:
  116. break;
  117. }
  118. }
  119. beforeEach(fn) {
  120. return registerHook(this.lifeCycle.beforeHooks, fn);
  121. }
  122. afterEach(fn) {
  123. return registerHook(this.lifeCycle.afterHooks, fn);
  124. }
  125. }
  126. Router.install = function (Vue) {
  127. initMixins(Vue, Router);
  128. Object.defineProperty(Vue.prototype, '$Router', {
  129. get() {
  130. return Router.$root;
  131. },
  132. });
  133. Object.defineProperty(Vue.prototype, '$Route', {
  134. get() {
  135. return Router.$root.getPageRoute(this);
  136. },
  137. });
  138. };
  139. export default Router;
  140. /**
  141. *
  142. * @param {VueComponent } Vim vue实例对象
  143. * @param {dom} el dom节点选择器
  144. */
  145. export const RouterMount = function (Vim, el) {
  146. switch (appPlatform(true)) {
  147. case 'APP':
  148. appMount(Vim, el);
  149. break;
  150. case 'APPLETS':
  151. appletsMount(Vim, el);
  152. break;
  153. case 'H5':
  154. vueMount.push({ Vim, el });
  155. break;
  156. default:
  157. warn('糟糕!!!还有其他的执行环境???没听说过啊。一脸懵逼???加QQ群问问:769241495');
  158. break;
  159. }
  160. };