index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. "/pages/login/xieyi",
  10. "/pages/login/mimi",
  11. "/pages/ad/ad",
  12. ];
  13. //白名单 不需要登录的页面路径组成的数组
  14. function hasPermission(url) {
  15. // 在白名单中或有token,直接跳转
  16. if (whiteList.indexOf(url) !== -1 || store.state.token) {
  17. return true;
  18. }
  19. return false;
  20. }
  21. uni.addInterceptor("navigateTo", {
  22. // 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
  23. invoke(e) {
  24. if (!hasPermission(e.url) ) {
  25. uni.reLaunch({
  26. url: "/pages/login/login",
  27. });
  28. return false;
  29. }
  30. return true;
  31. },
  32. success(e) {},
  33. });
  34. uni.addInterceptor("switchTab", {
  35. // tabbar页面跳转前进行拦截
  36. invoke(e) {
  37. if (!hasPermission(e.url)) {
  38. uni.reLaunch({
  39. url: "/pages/login/login",
  40. });
  41. return false;
  42. }
  43. return true;
  44. },
  45. success(e) {},
  46. });
  47. // #ifdef H5
  48. const needLoginList = [
  49. "/pages/index/index",
  50. ]
  51. let token = uni.getStorageSync("token")
  52. let locationUrl = window.location.href.split("#")[1]
  53. if (needLoginList.includes(locationUrl) && !token) {
  54. window.location.href = "/";
  55. } else {
  56. }
  57. // #endif