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 { goLogin } from "./useNavigator";
function stopCurrentAction(): Promise<void> {
return new Promise(() => {});
}
export const checkToken = async (
redirect?: RouteLocationRaw,
): Promise<void> => {
export const checkToken = async (redirect?: RouteLocationRaw) => {
try {
const token = await ensureValidAuthToken();
if (!token) {
goLogin({ redirect });
return stopCurrentAction();
return;
}
} catch {
goLogin({ redirect });
return stopCurrentAction();
}
};

View File

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