index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /********数据请求同时继承了文件上传(包括七牛云上传)************/
  2. import upload from "./upload/upload.js";
  3. export default upload;
  4. //路由拦截
  5. import store from '@/store';
  6. const whiteList = [
  7. "/pages/login/login",
  8. "/pages/register/register",
  9. ];
  10. //白名单 不需要登录的页面路径组成的数组
  11. function hasPermission(url) {
  12. // 在白名单中或有token,直接跳转
  13. if (whiteList.indexOf(url) !== -1 || store.state.token) {
  14. return true;
  15. }
  16. return false;
  17. }
  18. uni.addInterceptor("navigateTo", {
  19. // 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
  20. invoke(e) {
  21. if (!hasPermission(e.url)) {
  22. uni.reLaunch({
  23. url: "/pages/login/login",
  24. });
  25. return false;
  26. }
  27. return true;
  28. },
  29. success(e) {},
  30. });
  31. uni.addInterceptor("switchTab", {
  32. // tabbar页面跳转前进行拦截
  33. invoke(e) {
  34. if (!hasPermission(e.url)) {
  35. uni.reLaunch({
  36. url: "/pages/login/login",
  37. });
  38. return false;
  39. }
  40. return true;
  41. },
  42. success(e) {},
  43. });
  44. // #ifdef H5
  45. const needLoginList = [
  46. "/pages/index/index",
  47. ]
  48. let token = uni.getStorageSync("token")
  49. let locationUrl = window.location.href.split("#")[1]
  50. if (needLoginList.includes(locationUrl) && !token) {
  51. window.location.href = "/";
  52. } else {
  53. }
  54. // #endif