index.js 1.3 KB

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