index.js 1.4 KB

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