index.js 1.4 KB

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