routeInterceptor.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { computed, unref } from 'vue';
  2. import { useUserStore } from '@/store/user.js';
  3. import { REDIRECT_KEY } from '@/settings/enums.js';
  4. // 登录后可进入页面(弹窗提示)
  5. const loginList = [];
  6. // 未登录时直接跳转首页的页面
  7. const redirectToIndexPaths = ['/pages/orderFood/index'];
  8. const navigateToInterceptor = {
  9. invoke({ url }) {
  10. const userStore = useUserStore();
  11. const getToken = computed(() => userStore.getToken);
  12. const path = url.split('?')[0];
  13. if (unref(getToken)) return true;
  14. if (redirectToIndexPaths.some((p) => path === p || path.endsWith(p))) {
  15. uni.reLaunch({ url: '/pages/index/index' });
  16. return false;
  17. }
  18. if (loginList.includes(path)) {
  19. uni.showModal({
  20. title: '提示',
  21. content: '未登录请先登录',
  22. success: function (res) {
  23. if (res.confirm) {
  24. uni.setStorageSync(REDIRECT_KEY, url);
  25. }
  26. }
  27. });
  28. return false;
  29. }
  30. return true;
  31. },
  32. success(args) {
  33. console.log('成功', args);
  34. },
  35. fail(err) {
  36. console.log('失败', err);
  37. }
  38. };
  39. export const routeInterceptor = {
  40. install() {
  41. uni.addInterceptor('navigateTo', navigateToInterceptor);
  42. uni.addInterceptor('reLaunch', navigateToInterceptor);
  43. uni.addInterceptor('redirectTo', navigateToInterceptor);
  44. }
  45. };