hookehuyr

refactor(router): 移除checkWxAuth函数的to参数并优化授权跳转逻辑

简化微信授权检查逻辑,移除不必要的to参数,直接在checkWxAuth函数中处理授权跳转,提高代码可读性和维护性。
/*
* @Date: 2025-03-20 20:36:36
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-03-25 15:17:18
* @LastEditTime: 2025-03-26 08:04:21
* @FilePath: /mlaj/src/router/guards.js
* @Description: 路由守卫逻辑
*/
......@@ -25,12 +25,24 @@ export const authRequiredRoutes = [
]
// 微信授权检查
export const checkWxAuth = async (to) => {
if (!import.meta.env.DEV && wxInfo().isWeiXin && to.path !== '/auth') {
export const checkWxAuth = async () => {
if (!import.meta.env.DEV && wxInfo().isWeiXin) {
try {
const { code, data } = await getAuthInfoAPI();
if (code && !data.openid_has) {
return { path: '/auth', query: { href: location.hash } }
// 直接在这里处理授权跳转
const params = new URLSearchParams({
f: 'behalo',
a: 'openid',
res: encodeURIComponent(location.origin + location.pathname + location.hash)
});
if (import.meta.env.DEV) {
params.append('test_openid', import.meta.env.VITE_OPENID);
}
location.href = `/srv/?${params.toString()}`;
return false;
}
} catch (error) {
console.error('微信授权检查失败:', error)
......
......@@ -21,7 +21,7 @@ const router = createRouter({
// 导航守卫
router.beforeEach(async (to, from, next) => {
// 微信授权检查
const wxAuthResult = await checkWxAuth(to)
const wxAuthResult = await checkWxAuth()
if (wxAuthResult !== true) {
next(wxAuthResult)
return
......