import { createRouter, createWebHistory, createWebHashHistory, } from "vue-router"; import routes from "./routes"; import LibForWeixin from "@/utils/LibForWeixin"; import { useUserStore, useAppStore } from "@/store"; import { getUrlParam } from "../utils"; const router = createRouter({ // history: createWebHistory(import.meta.env.VITE_BASE), history: createWebHashHistory(), routes, scrollBehavior: () => ({ el: "#app", top: 0, behavior: "smooth", }), }); let pattern = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/; const whiteList = ["Login", "WxLogin"]; router.beforeEach(async (to, from, next) => { console.log("to:", to); const code = getUrlParam("code"); const appStore = useAppStore(); const userStore = useUserStore(); if ( !userStore.openId && !code && !import.meta.env.VITE_NOT_NEED_WX_AUTH // &&!( // pattern.test(location.hostname) || // location.hostname === "apply-test.qmth.com.cn" // ) ) { LibForWeixin.auth(); return; } if ( !appStore.globalConfig && to.name !== "WxLogin" && (userStore.openId || pattern.test(location.hostname) || location.hostname === "apply-test.qmth.com.cn") ) { let config = await appStore.getGlobalConfig(); if (((config && !config?.taskTitle) || !config) && to.name !== "Login") { next({ name: "Login", replace: true }); return; } } if (to.meta.jsApiList) { await LibForWeixin.initJSSDK(to.meta.jsApiList) .then(() => { console.log("config JS-SDK success"); }) .catch(() => { console.log("config JS-SDK fail"); }); } if (whiteList.includes(to.name)) { next(); return; } if (!userStore.loginInfo) { next({ name: "Login", replace: true }); } else { next(); } }); router.afterEach((to) => { if (to.path !== "/favicon.icon") { document.title = to.meta.title ? to.meta.title : ""; } window.scrollTo(0, 0); }); export default router;