router-interceptor.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @author 大雄
  3. * @Date 2021年7月1日20:49:58
  4. * @description 路由导航守卫(简单版,后续需要功能再完善)
  5. */
  6. export default function() {
  7. // 监听路由前进
  8. function routerPush({ type = 'navigateTo' } = {}) {
  9. routeWatchClearModal();
  10. }
  11. // 监听路由后退
  12. function routerBack() {
  13. routeWatchClearModal();
  14. }
  15. // 页面跳转后,销毁当前页面未关闭的弹框
  16. function routeWatchClearModal() {
  17. try {
  18. var FyShowModalView = plus.nativeObj.View.getViewById("FyShowModalView");
  19. if (FyShowModalView) {
  20. FyShowModalView.clear();
  21. }
  22. var FyShowModalCancel = plus.nativeObj.View.getViewById("FyShowModalCancel");
  23. if (FyShowModalCancel) {
  24. FyShowModalCancel.clear();
  25. }
  26. var FyShowModalConfirm = plus.nativeObj.View.getViewById("FyShowModalConfirm");
  27. if (FyShowModalConfirm) {
  28. FyShowModalConfirm.clear();
  29. }
  30. } catch(err) {
  31. console.log(err);
  32. }
  33. }
  34. uni.addInterceptor('navigateTo', {
  35. success(e) {
  36. routerPush({ type: 'navigateTo' });
  37. }
  38. })
  39. uni.addInterceptor('redirectTo', {
  40. success(e) {
  41. routerPush({ type: 'redirectTo' });
  42. }
  43. })
  44. uni.addInterceptor('reLaunch', {
  45. success(e) {
  46. routerPush({ type: 'reLaunch' });
  47. }
  48. })
  49. uni.addInterceptor('switchTab', {
  50. success(e) {
  51. routerPush({ type: 'switchTab' });
  52. }
  53. })
  54. uni.addInterceptor('navigateBack', {
  55. success(e) {
  56. routerBack();
  57. }
  58. })
  59. }