refactor: simplify token check logic and improve code readability

This commit is contained in:
duanshuwen
2026-06-27 16:13:26 +08:00
parent df1a95f0c2
commit b3eaa2057b
2 changed files with 206 additions and 104 deletions

View File

@@ -2,21 +2,14 @@ import type { RouteLocationRaw } from "vue-router";
import { ensureValidAuthToken } from "@/utils/request"; import { ensureValidAuthToken } from "@/utils/request";
import { goLogin } from "./useNavigator"; import { goLogin } from "./useNavigator";
function stopCurrentAction(): Promise<void> { export const checkToken = async (redirect?: RouteLocationRaw) => {
return new Promise(() => {});
}
export const checkToken = async (
redirect?: RouteLocationRaw,
): Promise<void> => {
try { try {
const token = await ensureValidAuthToken(); const token = await ensureValidAuthToken();
if (!token) { if (!token) {
goLogin({ redirect }); goLogin({ redirect });
return stopCurrentAction(); return;
} }
} catch { } catch {
goLogin({ redirect }); goLogin({ redirect });
return stopCurrentAction();
} }
}; };

View File

@@ -2,100 +2,176 @@
<div class="flex h-dvh flex-col relative overflow-hidden"> <div class="flex h-dvh flex-col relative overflow-hidden">
<!-- 顶部自定义导航栏 --> <!-- 顶部自定义导航栏 -->
<div class="absolute top-0 left-0 w-full z-10"> <div class="absolute top-0 left-0 w-full z-10">
<ChatTopNavBar ref="topNavBarRef" :mainPageDataModel="mainPageDataModel" /> <ChatTopNavBar
ref="topNavBarRef"
:mainPageDataModel="mainPageDataModel"
/>
</div> </div>
<div ref="mainScrollRef" <div
ref="mainScrollRef"
class="min-h-0 flex-1 overflow-y-auto overscroll-contain scrollbar-none [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden" class="min-h-0 flex-1 overflow-y-auto overscroll-contain scrollbar-none [-ms-overflow-style:none] [&::-webkit-scrollbar]:hidden"
@scroll="handleScroll" @touchstart.capture="handleScrollAreaTouchStart" @touchstart="handleScrollAreaTouchStart" @scroll="handleScroll"
@touchmove="handleScrollAreaTouchMove"> @touchstart.capture="handleScrollAreaTouchStart"
@touchstart="handleScrollAreaTouchStart"
@touchmove="handleScrollAreaTouchMove"
>
<div class="relative"> <div class="relative">
<img class="w-full block" :src="mainPageDataModel?.initPageImages?.backgroundImageUrl" style="height: 252px;" /> <img
class="w-full block"
:src="mainPageDataModel?.initPageImages?.backgroundImageUrl"
style="height: 252px"
/>
<div class="absolute bottom-0 left-0 right-0 flex-1"> <div class="absolute bottom-0 left-0 right-0 flex-1">
<div class="px-[12px] pt-[12px]"> <div class="px-[12px] pt-[12px]">
<Welcome :mainPageDataModel="mainPageDataModel" /> <Welcome :mainPageDataModel="mainPageDataModel" />
</div> </div>
<div style="margin-bottom: -1px;"> <div style="margin-bottom: -1px">
<AiTabSwitch v-model="tabIndex" :list="tabList" @change="handleChange" /> <AiTabSwitch
v-model="tabIndex"
:list="tabList"
@change="handleChange"
/>
</div> </div>
</div> </div>
</div> </div>
<!-- 发现页 --> <!-- 发现页 -->
<div v-show="tabIndex === 0" class="min-h-0 flex-1 overflow-hidden" <div
@touchstart.capture="handleScrollAreaTouchStart" @touchstart="handleScrollAreaTouchStart" v-show="tabIndex === 0"
@touchmove="handleScrollAreaTouchMove"> class="min-h-0 flex-1 overflow-hidden"
@touchstart.capture="handleScrollAreaTouchStart"
<Discovery @scroll-touch-start="handleScrollAreaTouchStart" @scroll-touch="handleScrollAreaTouchMove" /> @touchstart="handleScrollAreaTouchStart"
@touchmove="handleScrollAreaTouchMove"
>
<Discovery
@scroll-touch-start="handleScrollAreaTouchStart"
@scroll-touch="handleScrollAreaTouchMove"
/>
</div> </div>
<!-- 消息列表可滚动区域 --> <!-- 消息列表可滚动区域 -->
<div v-show="tabIndex === 1" class="min-h-0 flex-1 overflow-hidden" :scroll-top="scrollTop" @scroll="handleScroll" <div
@scrolltolower="handleScrollToLower" @touchstart.capture="handleScrollAreaTouchStart" v-show="tabIndex === 1"
@touchstart="handleScrollAreaTouchStart" @touchmove="handleScrollAreaTouchMove"> class="min-h-0 flex-1 overflow-hidden"
:scroll-top="scrollTop"
<div class="area-msg-list-content" v-for="item in chatMsgList" :key="item.msgId" :id="item.msgId"> @scroll="handleScroll"
@scrolltolower="handleScrollToLower"
@touchstart.capture="handleScrollAreaTouchStart"
@touchstart="handleScrollAreaTouchStart"
@touchmove="handleScrollAreaTouchMove"
>
<div
class="area-msg-list-content"
v-for="item in chatMsgList"
:key="item.msgId"
:id="item.msgId"
>
<template v-if="item.msgType === MessageRole.AI"> <template v-if="item.msgType === MessageRole.AI">
<ChatCardAI class="flex justify-start" :key="`ai-${item.msgId}-${item.msg ? item.msg.length : 0}`" :text="item.componentName && isLongTextCard(item.componentName) <ChatCardAI
? '' class="flex justify-start"
: item.msg || '' :key="`ai-${item.msgId}-${item.msg ? item.msg.length : 0}`"
" :isLoading="item.isLoading"> :text="
<template #content v-if=" item.componentName && isLongTextCard(item.componentName)
item.toolCall || ? ''
(item.componentName && isLongTextCard(item.componentName)) : item.msg || ''
"> "
:isLoading="item.isLoading"
>
<template
#content
v-if="
item.toolCall ||
(item.componentName && isLongTextCard(item.componentName))
"
>
<LongTextGuideCardPreview
v-if="
item.componentName && isLongTextCard(item.componentName)
"
:componentName="item.componentName"
/>
<LongTextGuideCardPreview v-if="item.componentName && isLongTextCard(item.componentName)" <QuickBookingComponent
:componentName="item.componentName" /> v-if="
item.toolCall &&
item.toolCall.componentName === CompName.quickBookingCard
"
/>
<DiscoveryCardComponent
v-else-if="
item.toolCall &&
item.toolCall.componentName === CompName.discoveryCard
"
/>
<CreateServiceOrder
v-else-if="
item.toolCall &&
item.toolCall.componentName === CompName.callServiceCard
"
:toolCall="item.toolCall"
/>
<OpenMapComponent
v-else-if="
item.toolCall &&
item.toolCall.componentName === CompName.mapCard
"
/>
<GeneratorPhotoComponent
v-else-if="
item.toolCall &&
item.toolCall.componentName ===
CompName.aigcPhotoGeneratorCard
"
:toolCall="item.toolCall"
/>
<AigcPhotoCard
v-else-if="
item.toolCall &&
item.toolCall.componentName === CompName.videoCard
"
:toolCall="item.toolCall"
/>
<QuickBookingComponent v-if=" <Feedback
item.toolCall && v-else-if="
item.toolCall.componentName === CompName.quickBookingCard item.toolCall &&
" /> item.toolCall.componentName === CompName.feedbackCard
<DiscoveryCardComponent v-else-if=" "
item.toolCall && :toolCall="item.toolCall"
item.toolCall.componentName === CompName.discoveryCard />
" /> <DetailCardComponent
<CreateServiceOrder v-else-if=" v-else-if="
item.toolCall && item.toolCall &&
item.toolCall.componentName === CompName.callServiceCard item.toolCall.componentName ===
" :toolCall="item.toolCall" /> CompName.pictureAndCommodityCard
<OpenMapComponent v-else-if=" "
item.toolCall && :toolCall="item.toolCall"
item.toolCall.componentName === CompName.mapCard />
" /> <AddCarCard
<GeneratorPhotoComponent v-else-if=" v-else-if="
item.toolCall && item.toolCall &&
item.toolCall.componentName === CompName.aigcPhotoGeneratorCard item.toolCall.componentName ===
" :toolCall="item.toolCall" /> CompName.enterLicensePlateCard
<AigcPhotoCard v-else-if=" "
item.toolCall && :toolCall="item.toolCall"
item.toolCall.componentName === CompName.videoCard />
" :toolCall="item.toolCall" /> <SurveyQuestionnaire
v-else-if="
<Feedback v-else-if=" item.toolCall &&
item.toolCall && item.toolCall.componentName ===
item.toolCall.componentName === CompName.feedbackCard CompName.callSurveyQuestionnaire
" :toolCall="item.toolCall" /> "
<DetailCardComponent v-else-if=" :toolCall="item.toolCall"
item.toolCall && />
item.toolCall.componentName ===
CompName.pictureAndCommodityCard
" :toolCall="item.toolCall" />
<AddCarCard v-else-if="
item.toolCall &&
item.toolCall.componentName === CompName.enterLicensePlateCard
" :toolCall="item.toolCall" />
<SurveyQuestionnaire v-else-if="
item.toolCall &&
item.toolCall.componentName ===
CompName.callSurveyQuestionnaire
" :toolCall="item.toolCall" />
</template> </template>
<template #footer> <template #footer>
<!-- 这个是底部 --> <!-- 这个是底部 -->
<AttachListComponent v-if="item.question" :question="item.question" /> <AttachListComponent
v-if="item.question"
:question="item.question"
/>
</template> </template>
</ChatCardAI> </ChatCardAI>
</template> </template>
@@ -107,17 +183,23 @@
<template v-else> <template v-else>
<ChatCardOther class="flex justify-center" :text="item.msg"> <ChatCardOther class="flex justify-center" :text="item.msg">
<ChatGuide v-if="chatMsgList.length < 2" /> <ChatGuide v-if="chatMsgList.length < 2" />
<ActivityListComponent v-if=" <ActivityListComponent
mainPageDataModel.activityList && v-if="
mainPageDataModel.activityList.length mainPageDataModel.activityList &&
" :activityList="mainPageDataModel.activityList" /> mainPageDataModel.activityList.length
"
:activityList="mainPageDataModel.activityList"
/>
<!-- 先不展示了,等后续有需求再加回来 false --> <!-- 先不展示了,等后续有需求再加回来 false -->
<RecommendPostsComponent v-if=" <RecommendPostsComponent
false && v-if="
mainPageDataModel.recommendTheme && false &&
mainPageDataModel.recommendTheme.length mainPageDataModel.recommendTheme &&
" :recommendThemeList="mainPageDataModel.recommendTheme" /> mainPageDataModel.recommendTheme.length
"
:recommendThemeList="mainPageDataModel.recommendTheme"
/>
</ChatCardOther> </ChatCardOther>
</template> </template>
</div> </div>
@@ -127,9 +209,17 @@
<!-- 输入框区域 --> <!-- 输入框区域 -->
<div class="mt-auto shrink-0"> <div class="mt-auto shrink-0">
<ChatQuickAccess /> <ChatQuickAccess />
<ChatInputArea ref="inputAreaRef" v-model="inputMessage" :holdKeyboard="holdKeyboard" <ChatInputArea
:is-session-active="isSessionActive" :stop-request="stopRequest" @send="sendMessageAction" ref="inputAreaRef"
@noHideKeyboard="handleNoHideKeyboard" @keyboardShow="handleKeyboardShow" @keyboardHide="handleKeyboardHide" /> v-model="inputMessage"
:holdKeyboard="holdKeyboard"
:is-session-active="isSessionActive"
:stop-request="stopRequest"
@send="sendMessageAction"
@noHideKeyboard="handleNoHideKeyboard"
@keyboardShow="handleKeyboardShow"
@keyboardHide="handleKeyboardHide"
/>
</div> </div>
</div> </div>
</template> </template>
@@ -137,8 +227,13 @@
<script setup> <script setup>
import { computed, onMounted, nextTick, onUnmounted, ref } from "vue"; import { computed, onMounted, nextTick, onUnmounted, ref } from "vue";
import { useI18n } from "vue-i18n"; import { useI18n } from "vue-i18n";
import { emitter } from '@/utils/events' import { emitter } from "@/utils/events";
import { MessageRole, MessageType, CompName, Command } from "@/constants/ChatModel"; import {
MessageRole,
MessageType,
CompName,
Command,
} from "@/constants/ChatModel";
import WebSocketManager from "@/utils/WebSocketManager"; import WebSocketManager from "@/utils/WebSocketManager";
import { import {
SWITCH_TO_COMPANION_TAB, SWITCH_TO_COMPANION_TAB,
@@ -149,10 +244,17 @@ import {
NOTICE_EVENT_LOGOUT, NOTICE_EVENT_LOGOUT,
NOTICE_EVENT_LOGIN_SUCCESS, NOTICE_EVENT_LOGIN_SUCCESS,
} from "@/constants/constant"; } from "@/constants/constant";
import { mainPageData, conversationMsgList, recentConversation } from "@/api/home"; import {
mainPageData,
conversationMsgList,
recentConversation,
} from "@/api/home";
import { IdUtils } from "@/utils/IdUtils"; import { IdUtils } from "@/utils/IdUtils";
import { resolveChatSocketUrl } from "@/utils/socketUrl"; import { resolveChatSocketUrl } from "@/utils/socketUrl";
import { appendLongTextChunk, createLongTextData } from "@/constants/longTextCard"; import {
appendLongTextChunk,
createLongTextData,
} from "@/constants/longTextCard";
import { checkToken } from "@/hooks/useGoLogin"; import { checkToken } from "@/hooks/useGoLogin";
import { ensureValidAuthToken } from "@/utils/request"; import { ensureValidAuthToken } from "@/utils/request";
import { useAppStore } from "@/store"; import { useAppStore } from "@/store";
@@ -161,7 +263,7 @@ import Welcome from "../Welcome/index.vue";
import AiTabSwitch from "../AiTabSwitch/index.vue"; import AiTabSwitch from "../AiTabSwitch/index.vue";
import Discovery from "../Discovery/index.vue"; import Discovery from "../Discovery/index.vue";
import ChatGuide from "../ChatGuide/index.vue"; import ChatGuide from "../ChatGuide/index.vue";
import AigcPhotoCard from '../AigcPhotoCard/index.vue'; import AigcPhotoCard from "../AigcPhotoCard/index.vue";
import ChatTopNavBar from "../ChatTopNavBar/index.vue"; import ChatTopNavBar from "../ChatTopNavBar/index.vue";
import ChatCardAI from "../ChatCardAi/index.vue"; import ChatCardAI from "../ChatCardAi/index.vue";
import ChatCardMine from "../ChatCardMine/index.vue"; import ChatCardMine from "../ChatCardMine/index.vue";
@@ -253,7 +355,10 @@ const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
let currentSessionMessageId = null; let currentSessionMessageId = null;
const tabIndex = ref(0); const tabIndex = ref(0);
const tabList = computed(() => [t("home.chat.discovery"), t("home.chat.aiTour")]); const tabList = computed(() => [
t("home.chat.discovery"),
t("home.chat.aiTour"),
]);
const handleChange = (i) => { const handleChange = (i) => {
console.log("切换:", i); console.log("切换:", i);
@@ -305,8 +410,6 @@ const hideKeyboardByScroll = () => {
) { ) {
inputAreaRef.value.blurInput(); inputAreaRef.value.blurInput();
} }
}; };
const hideKeyboardAfterSend = () => { const hideKeyboardAfterSend = () => {
@@ -340,7 +443,9 @@ const handleScroll = (e) => {
hideKeyboardByScroll(); hideKeyboardByScroll();
} }
const currentScrollTop = Number(e?.detail?.scrollTop ?? e?.target?.scrollTop ?? 0); const currentScrollTop = Number(
e?.detail?.scrollTop ?? e?.target?.scrollTop ?? 0,
);
const scrollEl = e?.target; const scrollEl = e?.target;
if (topNavBarRef.value) { if (topNavBarRef.value) {
topNavBarRef.value.show = currentScrollTop > welcomeHeight.value; topNavBarRef.value.show = currentScrollTop > welcomeHeight.value;
@@ -377,7 +482,10 @@ const scrollToBottom = (force = false) => {
// 使用更大的值确保滚动到真正的底部 // 使用更大的值确保滚动到真正的底部
const targetScrollTop = 99999 + Math.random(); const targetScrollTop = 99999 + Math.random();
scrollTop.value = targetScrollTop; scrollTop.value = targetScrollTop;
if (mainScrollRef.value && typeof mainScrollRef.value.scrollTo === "function") { if (
mainScrollRef.value &&
typeof mainScrollRef.value.scrollTo === "function"
) {
mainScrollRef.value.scrollTo({ mainScrollRef.value.scrollTo({
top: mainScrollRef.value.scrollHeight, top: mainScrollRef.value.scrollHeight,
behavior: "smooth", behavior: "smooth",
@@ -515,7 +623,7 @@ const getMainPageData = async () => {
// 从个渠道获取如二维,没有的时候就返回首页的数据 // 从个渠道获取如二维,没有的时候就返回首页的数据
const sceneId = appStore.sceneId || ""; const sceneId = appStore.sceneId || "";
const res = await mainPageData({ sceneId }); const res = await mainPageData({ sceneId });
console.log("🚀 ~ getMainPageData ~ res:", res) console.log("🚀 ~ getMainPageData ~ res:", res);
if (res.code === 0) { if (res.code === 0) {
initData(); initData();
@@ -756,7 +864,8 @@ const handleWebSocketMessage = (data) => {
} }
} }
const isLongText = isLongTextCard(aiItem.componentName) || isLongTextCard(data.componentName); const isLongText =
isLongTextCard(aiItem.componentName) || isLongTextCard(data.componentName);
if (isLongText && data.contentKey) { if (isLongText && data.contentKey) {
aiItem.longTextData = aiItem.longTextData || createLongTextData(); aiItem.longTextData = aiItem.longTextData || createLongTextData();