acdr-ui/dist/dev/mp-weixin/interceptors/route.js

40 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-09-19 07:20:14 +08:00
"use strict";
const common_vendor = require("../common/vendor.js");
require("../store/index.js");
const utils_index = require("../utils/index.js");
const store_user = require("../store/user.js");
2024-10-01 09:15:35 +08:00
const loginRoute = "/modules/pet/login/index";
2024-09-19 07:20:14 +08:00
const isLogined = () => {
const userStore = store_user.useUserStore();
return userStore.isLogined;
};
const navigateToInterceptor = {
// 注意这里的url是 '/' 开头的,如 '/pages/index/index',跟 'pages.json' 里面的 path 不同
invoke({ url }) {
const path = url.split("?")[0];
let needLoginPages = [];
{
needLoginPages = utils_index.getNeedLoginPages();
}
const isNeedLogin = needLoginPages.includes(path);
if (!isNeedLogin) {
return true;
}
const hasLogin = isLogined();
if (hasLogin) {
return true;
}
const redirectRoute = `${loginRoute}?redirect=${encodeURIComponent(url)}`;
common_vendor.index.navigateTo({ url: redirectRoute });
return false;
}
};
const routeInterceptor = {
install() {
common_vendor.index.addInterceptor("navigateTo", navigateToInterceptor);
common_vendor.index.addInterceptor("reLaunch", navigateToInterceptor);
common_vendor.index.addInterceptor("redirectTo", navigateToInterceptor);
}
};
exports.routeInterceptor = routeInterceptor;