index.js 1.3 KB

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