index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/registr/h5Registr",
  9. "/pages/login/xieyi",
  10. "/pages/login/mimi",
  11. "/pages/index/index",
  12. "/pages/quote/paymentCode"
  13. ];
  14. //白名单 不需要登录的页面路径组成的数组
  15. function hasPermission(url) {
  16. // 去掉url上的查询参数(如扫码注册携带的邀请码等),再做白名单匹配
  17. const path = url.split("?")[0];
  18. // 在白名单中或有token,直接放行
  19. if (whiteList.indexOf(path) !== -1 || store.state.token) {
  20. return true;
  21. }
  22. return false;
  23. }
  24. // 拦截时先清理登录态(含过期token),再跳登录页,避免过期token残留导致登录页请求401
  25. function goLogin() {
  26. store.commit("emptyUserInfo");
  27. uni.reLaunch({
  28. url: "/pages/login/login",
  29. });
  30. }
  31. uni.addInterceptor("navigateTo", {
  32. // 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
  33. invoke(e) {
  34. if (!hasPermission(e.url)) {
  35. goLogin();
  36. return false;
  37. }
  38. return true;
  39. },
  40. success(e) {},
  41. });
  42. uni.addInterceptor("switchTab", {
  43. // tabbar页面跳转前进行拦截
  44. invoke(e) {
  45. if (!hasPermission(e.url)) {
  46. goLogin();
  47. return false;
  48. }
  49. return true;
  50. },
  51. success(e) {},
  52. });
  53. // #ifdef H5
  54. const needLoginList = [
  55. "/pages/index/index",
  56. ]
  57. let token = uni.getStorageSync("token")
  58. let locationUrl = window.location.href.split("#")[1]
  59. if (needLoginList.includes(locationUrl) && !token) {
  60. window.location.href = "/";
  61. } else {
  62. }
  63. // #endif