routeInterceptor.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 navigateToInterceptor = {
  8. invoke({ url }) {
  9. const userStore = useUserStore();
  10. const getToken = computed(() => userStore.getToken);
  11. const path = url.split('?')[0];
  12. if (unref(getToken)) return true;
  13. console.log('出发前', path, loginList.includes(path));
  14. if (loginList.includes(path)) {
  15. uni.showModal({
  16. title: '提示',
  17. content: '未登录请先登录',
  18. success: function (res) {
  19. if (res.confirm) {
  20. uni.setStorageSync(REDIRECT_KEY, url);
  21. } else if (res.cancel) {
  22. }
  23. }
  24. });
  25. return false;
  26. }
  27. return true;
  28. },
  29. success(args) {
  30. console.log('成功', args);
  31. },
  32. fail(err) {
  33. console.log('失败', err);
  34. }
  35. };
  36. export const routeInterceptor = {
  37. install() {
  38. uni.addInterceptor('navigateTo', navigateToInterceptor);
  39. uni.addInterceptor('reLaunch', navigateToInterceptor);
  40. uni.addInterceptor('redirectTo', navigateToInterceptor);
  41. }
  42. };