version temp2
This commit is contained in:
59
src/App.vue
59
src/App.vue
@@ -1,23 +1,58 @@
|
||||
<script setup>
|
||||
import { RouterLink, RouterView } from "vue-router";
|
||||
import { RouterLink, RouterView, useRouter } from "vue-router";
|
||||
const { isWeChat } = useEnv();
|
||||
import { useAgentStore } from "@/stores/agentStore";
|
||||
import { useUserStore } from "@/stores/userStore";
|
||||
import { useDialogStore } from "@/stores/dialogStore";
|
||||
import { useAuthStore } from "@/stores/authStore";
|
||||
import { useWeixinShare } from "@/composables/useWeixinShare";
|
||||
|
||||
const router = useRouter();
|
||||
const agentStore = useAgentStore();
|
||||
const userStore = useUserStore();
|
||||
const dialogStore = useDialogStore();
|
||||
const authStore = useAuthStore();
|
||||
const { configWeixinShare, setDynamicShare } = useWeixinShare();
|
||||
|
||||
onMounted(() => {
|
||||
// 检查token版本,如果版本不匹配则清除旧token
|
||||
checkTokenVersion()
|
||||
|
||||
// 恢复微信授权状态(页面刷新后)
|
||||
authStore.restoreFromStorage();
|
||||
|
||||
// 检查是否是微信授权回调
|
||||
const url = new URL(window.location.href);
|
||||
const hasWeixinCode = url.searchParams.has('code') && url.searchParams.has('state');
|
||||
|
||||
if (hasWeixinCode) {
|
||||
// 如果是授权回调,标记为正在授权
|
||||
authStore.startWeixinAuth();
|
||||
}
|
||||
|
||||
RefreshToken();
|
||||
const token = localStorage.getItem("token");
|
||||
if (token) {
|
||||
agentStore.fetchAgentStatus();
|
||||
userStore.fetchUserInfo();
|
||||
}
|
||||
|
||||
// 配置微信分享
|
||||
// 延迟执行,确保微信SDK已加载
|
||||
setTimeout(async () => {
|
||||
if (isWeChat.value && window.jWeixin) {
|
||||
await setDynamicShare();
|
||||
}
|
||||
}, 500);
|
||||
|
||||
// 监听路由变化,更新微信分享配置
|
||||
router.afterEach(() => {
|
||||
setTimeout(async () => {
|
||||
if (isWeChat.value && window.jWeixin) {
|
||||
await setDynamicShare();
|
||||
}
|
||||
}, 300);
|
||||
});
|
||||
});
|
||||
|
||||
const checkTokenVersion = () => {
|
||||
@@ -105,7 +140,9 @@ const h5WeixinLogin = async () => {
|
||||
// 获取特定参数值
|
||||
const code = params.get("code");
|
||||
const state = params.get("state");
|
||||
|
||||
if (code && state) {
|
||||
// 这是微信授权回调,处理授权结果
|
||||
const { data, error } = await useApiFetch("/user/wxh5Auth")
|
||||
.post({ code })
|
||||
.json();
|
||||
@@ -130,11 +167,27 @@ const h5WeixinLogin = async () => {
|
||||
}?${params.toString()}`;
|
||||
window.history.replaceState({}, "", newUrl);
|
||||
|
||||
agentStore.fetchAgentStatus();
|
||||
userStore.fetchUserInfo();
|
||||
// 标记微信授权完成
|
||||
authStore.completeWeixinAuth();
|
||||
|
||||
// 获取用户和代理信息
|
||||
await Promise.all([
|
||||
agentStore.fetchAgentStatus(),
|
||||
userStore.fetchUserInfo()
|
||||
]);
|
||||
|
||||
// 如果有待处理的路由,跳转到该路由
|
||||
if (authStore.pendingRoute) {
|
||||
router.replace(authStore.pendingRoute);
|
||||
authStore.clearPendingRoute();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 没有授权参数,需要开始微信授权
|
||||
// 保存当前路由作为授权完成后的目标路由
|
||||
const currentRoute = router.currentRoute.value;
|
||||
authStore.startWeixinAuth(currentRoute);
|
||||
h5WeixinGetCode();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user