| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- /********数据请求同时继承了文件上传(包括七牛云上传)************/
- import upload from "./upload/upload.js";
- export default upload;
- //路由拦截
- import store from '@/store';
- const whiteList = [
- "/pages/login/login",
- "/pages/registr/h5Registr",
- "/pages/login/xieyi",
- "/pages/login/mimi",
- "/pages/index/index",
- "/pages/quote/paymentCode"
- ];
- //白名单 不需要登录的页面路径组成的数组
- function hasPermission(url) {
- // 去掉url上的查询参数(如扫码注册携带的邀请码等),再做白名单匹配
- const path = url.split("?")[0];
- // 在白名单中或有token,直接放行
- if (whiteList.indexOf(path) !== -1 || store.state.token) {
- return true;
- }
- return false;
- }
- // 拦截时先清理登录态(含过期token),再跳登录页,避免过期token残留导致登录页请求401
- function goLogin() {
- store.commit("emptyUserInfo");
- uni.reLaunch({
- url: "/pages/login/login",
- });
- }
- uni.addInterceptor("navigateTo", {
- // 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
- invoke(e) {
- if (!hasPermission(e.url)) {
- goLogin();
- return false;
- }
- return true;
- },
- success(e) {},
- });
- uni.addInterceptor("switchTab", {
- // tabbar页面跳转前进行拦截
- invoke(e) {
- if (!hasPermission(e.url)) {
- goLogin();
- return false;
- }
- return true;
- },
- success(e) {},
- });
- // #ifdef H5
- const needLoginList = [
- "/pages/index/index",
- ]
- let token = uni.getStorageSync("token")
- let locationUrl = window.location.href.split("#")[1]
- if (needLoginList.includes(locationUrl) && !token) {
- window.location.href = "/";
- } else {
- }
- // #endif
|