navJump.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { appPlatform } from './util';
  2. import { methods, H5FnTypeToggle, Global } from './config';
  3. import { transitionTo } from '../appRouter/hooks';
  4. import { appletsTransitionTo, backCallHook } from '../appletsRouter/hooks';
  5. import uniPushTo from '../appRouter/uniNav';
  6. import appletsUniPushTo from '../appletsRouter/appletsNav';
  7. import { err, warn } from './warn';
  8. import H5PushTo from '../vueRouter/routerNav';
  9. import * as compile from './compile';
  10. /**
  11. * 返回api 触发的公共函数
  12. * @param {Object/String} rule 当前跳转规则
  13. * @param {String} fnType 跳转页面的类型方法
  14. *
  15. * this 为当前 Router 实例
  16. */
  17. const isBcakNav = function ({
  18. backLayer,
  19. delta,
  20. H5PATCH,
  21. }) {
  22. compile.H5(() => {
  23. H5PATCH.on('historyBack', {
  24. backLayer,
  25. delta,
  26. });
  27. });
  28. compile.APP(() => {
  29. Global.backLayerC = backLayer; // 告诉路由需要返回几层
  30. uni.navigateBack({
  31. delta: backLayer,
  32. complete: () => {
  33. Global.LockStatus = false; // 跳转完成解锁状态
  34. },
  35. });
  36. });
  37. compile.mp(() => {
  38. backCallHook.call(this, backLayer, () => {
  39. uni.navigateBack({
  40. delta: backLayer,
  41. });
  42. });
  43. });
  44. };
  45. /**
  46. * 非 返回api 触发的公共函数
  47. * @param {Object/String} rule 当前跳转规则
  48. * @param {String} fnType 跳转页面的类型方法
  49. *
  50. * this 为当前 Router 实例
  51. */
  52. const notBackNav = function (rule, fnType) {
  53. if (rule == null) {
  54. return err('跳转规则为空,不允许这样操作');
  55. }
  56. if (rule.constructor === String) { // 单独 path 的情况 允许?拼接参数
  57. const ruleArray = rule.split('?');
  58. if (ruleArray.length > 1) {
  59. rule = {
  60. path: ruleArray[0],
  61. query: Global.$parseQuery.parse(ruleArray[1]),
  62. };
  63. }
  64. }
  65. switch (appPlatform(true)) {
  66. case 'H5':
  67. return H5PushTo.call(this, H5FnTypeToggle[fnType], rule, methods[fnType]);
  68. case 'APP':
  69. Global.LockStatus = true; // 设置为锁住状态
  70. return transitionTo.call(this, rule, fnType, uniPushTo);
  71. case 'APPLETS':
  72. Global.LockStatus = true; // 设置为锁住状态
  73. return appletsTransitionTo.call(this, rule, fnType, appletsUniPushTo);
  74. default:
  75. err('糟糕!!!还有其他的执行环境???没听说过啊。一脸懵逼???加QQ群问问:769241495');
  76. break;
  77. }
  78. };
  79. /**
  80. * 处理正在跳转的公共api
  81. * @param {Object/String} rule 当前跳转规则
  82. * @param {String} fnType 跳转页面的类型方法
  83. * @param {Boolean} isBack 是否通过 back() api 调用的 默认为false
  84. * @param {Boolean} enforce 是否强制越过跳转加锁检查 默认false 目前只有back() api 传递了
  85. *
  86. * this 为当前 Router 实例
  87. */
  88. const navjump = function (rule, fnType, isBack = false, enforce = false) {
  89. if (Global.LockStatus && !enforce) { // 正在跳转的状态下 给出提示正在跳转
  90. return warn('当前页面正在处于跳转状态,请稍后再进行跳转....');
  91. }
  92. if (isBack) { // 是返回api触发的
  93. return isBcakNav.call(this, rule, fnType);
  94. }
  95. return notBackNav.call(this, rule, fnType);
  96. };
  97. export default navjump;