浏览代码

fix: 生成形象成功后回退页面层级错误

王晓东 2 周之前
父节点
当前提交
c0bc0ea096

+ 3 - 1
src/pages/agent-avatar-confirm/index.tsx

@@ -7,6 +7,7 @@ import Taro, { nextTick } from "@tarojs/taro";
 
 import { TAvatarItem } from "@/service/storage";
 import { useAgentStore, useAgentStoreActions } from "@/store/agentStore";
+import { getNavigationDelta} from '@/utils/index'
 interface IProps {
   avatarItem?: TAvatarItem
 }
@@ -18,7 +19,8 @@ export default function Index({ avatarItem }: IProps) {
   const { updateEditAgent, saveAgent } = useAgentStoreActions()
   const currentItem = { ...(currentEditAvatar || {}) }
   const [enabledChatBg, setEnabledChatBg] = useState(agentEdit?.enabledChatBg ?? false)
-
+const delta = getNavigationDelta('pages/agent/index')
+  console.log(delta)
   const handleConfirm = async (edit: TAvatarItem & { enabledChatBg: boolean }) => {
     console.log('confirm', edit)
     if (edit.avatarLogo && edit.avatarUrl) {

+ 5 - 1
src/pages/agent-avatars/index.tsx

@@ -14,13 +14,14 @@ import style from "./index.module.less";
 import { TAvatarItem } from "@/service/storage";
 import { useAgentStore, useAgentStoreActions } from "@/store/agentStore";
 
-import Taro, { useDidShow, useUnload } from "@tarojs/taro";
+import Taro, { useDidShow, useRouter, useUnload } from "@tarojs/taro";
 import { isSuccess } from "@/utils";
 import WemetaRadio from "@/components/WemetaRadio/index";
 import { useModalStore } from "@/store/modalStore";
 import { useLoadMoreInfinite, createKey } from "@/utils/loadMoreInfinite";
 import BottomBar from "@/components/BottomBar";
 import WemetaButton from "@/components/buttons/WemetaButton";
+import { getNavigationDelta} from '@/utils/index'
 
 export default function Index() {
   const agent = useAgentStore((state) => state.agent);
@@ -41,6 +42,9 @@ export default function Index() {
     fetcher
   );
 
+  const delta = getNavigationDelta('pages/agent/index')
+  console.log(delta)
+
   const [current, setCurrent] = useState<TAvatarItem | null>(null);
 
   // 选择形象

+ 10 - 2
src/pages/agent-gen/components/step/StepConfirm.tsx

@@ -3,34 +3,42 @@ import Taro, { nextTick } from "@tarojs/taro";
 import { TAvatarItem } from "@/service/storage";
 import AvatarConfirm from '@/components/AvatarConfirm/index'
 import { useAgentStore, useAgentStoreActions } from "@/store/agentStore";
+import { getNavigationDelta } from '@/utils/index'
 interface IProps {
   prev: ()=>void
   pickedAvatar: TAvatarItem
 }
+
+
+
 export default React.memo(function Index({prev, pickedAvatar}:IProps) {
   const currentItem = { ...(pickedAvatar || {}) }
   const agentEdit = useAgentStore((state) => state.agentEdit);
   const { saveAgent, updateEditAgent} = useAgentStoreActions();
   const [enabledChatBg, setEnabledChatBg] = useState(true)
 
+
   const handleConfirm = async (edit: TAvatarItem & { enabledChatBg: boolean }) => {
     if (edit.avatarLogo && edit.avatarUrl) {
       updateEditAgent({...edit})
+      // 完全新形象时需要判断回退几层页面
+      const delta = getNavigationDelta('pages/agent/index')
       // 如果是编辑,直接编辑形象成功
       if(agentEdit?.agentId){
         nextTick(()=> {
           saveAgent()
           Taro.showToast({title: '形象设置成功', icon: 'success'})
           setTimeout(()=> {
-            Taro.navigateBack({delta:2})
+            Taro.navigateBack({delta: delta})
           }, 2000)
         })
         return
       }
+
       // 如果是新建智能体,只是暂时设置并不更新至服务器
       Taro.showToast({title: '形象设置成功', icon: 'success'})
       setTimeout(()=> {
-        Taro.navigateBack({delta:2})
+        Taro.navigateBack({delta: delta})
       }, 2000)
     }
 

+ 5 - 5
src/pages/chat/components/keyboard.ts

@@ -2,13 +2,13 @@ import Taro from "@tarojs/taro";
 import { useEffect, useState, useCallback } from "react";
 import { useAppStore } from "@/store/appStore";
 export const useKeyboard = (scrollViewRef: React.MutableRefObject<any>, contentId: string, scrollViewId: string ) => {
-  
+
   const [keyboardHeight, setKeyboardHeight] = useState(0);
   const [contentHeight, setContentHeight] = useState(0);
   const [scrollViewHeight, setScrollViewHeight] = useState(0);
   const [updateTrigger, setUpdateTrigger] = useState(0);
   const bottomSafeHeight = useAppStore((state) => state.bottomSafeHeight);
-  
+
   // 计算 marginTopOffset 偏移的距离
   const marginTopOffset = (() => {
     // console.log(contentHeight, keyboardHeight,scrollViewHeight, 9999)
@@ -38,8 +38,8 @@ export const useKeyboard = (scrollViewRef: React.MutableRefObject<any>, contentI
       if (res.height <= 0) {
         return setKeyboardHeight(0);
       }
-      
-      setKeyboardHeight(res.height - bottomSafeHeight + 16);
+      // console.log(bottomSafeHeight, res.height, 3333)
+      setKeyboardHeight(res.height - bottomSafeHeight);
     });
 
     return () => {
@@ -92,4 +92,4 @@ export const useKeyboard = (scrollViewRef: React.MutableRefObject<any>, contentI
     setContentHeight,
     setScrollViewHeight,
   }
-}
+}

+ 2 - 1
src/pages/chat/hooks/useChatUI.tsx

@@ -34,7 +34,8 @@ export const useChatUI = (
 
   // 计算输入框底部偏移量
   // 针对没有 safeArea?.bottom 的手机,需要额外增加 12 高度
-  const inputContainerBottomOffset = bottomSafeHeight <= 0 ? 12 : 0;
+  // const inputContainerBottomOffset = bottomSafeHeight <= 0 ? 12 : 0;
+  const inputContainerBottomOffset = 0;
 
   // 获取背景内容
   const getBgContent = () => {

+ 1 - 1
src/pages/chat/index.tsx

@@ -247,7 +247,7 @@ export default function Index() {
 
               </View>
             )}
-            <View className={`${style.aiTips} ${keyboardHeight > 0 ? 'hidden': ''} `}>内容由AI生成,仅供参考</View>
+            <View className={`${style.aiTips} ${keyboardHeight > 0 ? 'opacity-0': ''} `}>内容由AI生成,仅供参考</View>
           </View>
         </View>
       </View>

+ 24 - 0
src/utils/index.ts

@@ -545,3 +545,27 @@ export function restrictedPage() {
     })
   }, [])
 }
+
+
+export function getNavigationDelta(targetRoute: string) {
+  const pages = Taro.getCurrentPages()
+  const currentRoute = pages[pages.length - 1].route
+  // 如果目标是首页或特殊页面
+  if (targetRoute === 'pages/index/index') {
+    return pages.length - 1 // 直接退到首页
+  }
+
+  // 查找目标页面在页面栈中的位置
+  const targetIndex = pages.findIndex(page =>
+    page.route === targetRoute ||
+    page.route === targetRoute.replace(/^\//, '')
+  )
+
+  if (targetIndex >= 0) {
+    // 目标页面在栈中,直接返回需要退的层数
+    return pages.length - 1 - targetIndex
+  } else {
+    // 目标页面不在栈中,需要新开页面
+    return -1
+  }
+}