|
@@ -1,4 +1,51 @@
|
|
|
-<script setup lang="ts"></script>
|
|
|
|
|
|
|
+<script setup lang="ts">
|
|
|
|
|
+import { onMounted } from 'vue';
|
|
|
|
|
+import { useRouter } from 'vue-router';
|
|
|
|
|
+import { useUserStore } from './stores/user';
|
|
|
|
|
+import axios from 'axios';
|
|
|
|
|
+
|
|
|
|
|
+const router = useRouter();
|
|
|
|
|
+const userStore = useUserStore();
|
|
|
|
|
+
|
|
|
|
|
+onMounted(async () => {
|
|
|
|
|
+ // 从 URL 中提取 token 和 source 参数
|
|
|
|
|
+ const params = new URLSearchParams(window.location.search);
|
|
|
|
|
+ const token = params.get('token');
|
|
|
|
|
+ const source = params.get('source');
|
|
|
|
|
+
|
|
|
|
|
+ // 如果有 token,说明是从第三方跳转过来
|
|
|
|
|
+ if (token) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 调用后端接口验证 token 并获取用户信息
|
|
|
|
|
+ const { data } = await axios.post(
|
|
|
|
|
+ `${import.meta.env.VITE_API_URL}/users/login`,
|
|
|
|
|
+ { token, source },
|
|
|
|
|
+ { headers: { 'Content-Type': 'application/json' } }
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 设置用户状态
|
|
|
|
|
+ userStore.setUser({
|
|
|
|
|
+ userId: data.userId,
|
|
|
|
|
+ name: data.name,
|
|
|
|
|
+ refreshToken: data.refreshToken,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // 清除 URL 中的参数,然后导航到聊天页面
|
|
|
|
|
+ window.history.replaceState({}, '', '/chat');
|
|
|
|
|
+ router.push('/chat');
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.error('Token verification failed:', error);
|
|
|
|
|
+ router.push('/');
|
|
|
|
|
+ }
|
|
|
|
|
+ } else if (!userStore.userId) {
|
|
|
|
|
+ // 如果没有 token 且用户未登录,跳转到登录页
|
|
|
|
|
+ router.push('/');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 用户已登录,跳转到聊天页面
|
|
|
|
|
+ router.push('/chat');
|
|
|
|
|
+ }
|
|
|
|
|
+});
|
|
|
|
|
+</script>
|
|
|
|
|
|
|
|
<template>
|
|
<template>
|
|
|
<div>
|
|
<div>
|