Browse Source

fix: 访问智能体页 30 秒上报一次 log

王晓东 1 ngày trước cách đây
mục cha
commit
dbb8495dd2

+ 9 - 2
project.private.config.json

@@ -8,12 +8,19 @@
   "condition": {
     "miniprogram": {
       "list": [
+        {
+          "name": "pages/profile/index",
+          "pathName": "pages/profile/index",
+          "query": "scene=894778d0982940e5fecfc73311ff482e",
+          "scene": null,
+          "launchMode": "default"
+        },
         {
           "name": "pages/knowledge/index",
           "pathName": "pages/knowledge/index",
           "query": "",
-          "scene": null,
-          "launchMode": "default"
+          "launchMode": "default",
+          "scene": null
         },
         {
           "name": "pages/dashboard/index",

+ 4 - 4
src/components/AgentPage/components/AgentActionBar/index.tsx

@@ -50,10 +50,10 @@ export default ({agent, isVisitor}: IProps) => {
         content: <View className="w-200">{agent.deletedTip}</View>,
         showCancelButton: false,
         onConfirm() {
-          Taro.showTabBar();
+          Taro.showTabBar().catch(()=> {});
         },
         onCancel() {
-          Taro.showTabBar();
+          Taro.showTabBar().catch(()=> {});
         },
       })
       return
@@ -209,10 +209,10 @@ export default ({agent, isVisitor}: IProps) => {
                 content: <>确认删除该智能体?</>,
                 onConfirm() {
                   confirmDelete()
-                  Taro.showTabBar();
+                  Taro.showTabBar().catch(()=> {});
                 },
                 onCancel() {
-                  Taro.showTabBar();
+                  Taro.showTabBar().catch(()=> {});
                 },
               })
               

+ 2 - 2
src/components/popup/popup-sheets/index.tsx

@@ -22,7 +22,7 @@ export default ({ content, show, setShow, maskClosable = true }: Props) => {
     setSlideUp(false);
     setTimeout(() => {
       setShow(b);
-      Taro.showTabBar();
+      Taro.showTabBar().catch(()=> {});
     }, 100);
     
   };
@@ -34,7 +34,7 @@ export default ({ content, show, setShow, maskClosable = true }: Props) => {
         setSlideUp(show);
       }, 100);
     }else{
-      Taro.showTabBar();
+      Taro.showTabBar().catch(()=> {});
     }
     
   }, [show]);

+ 3 - 2
src/components/popup/popup/index.tsx

@@ -27,7 +27,7 @@ export default ({
     setSlideUp(false);
     setTimeout(() => {
       setShow(b);
-      Taro.showTabBar();
+      Taro.showTabBar().catch(()=> {});
     }, 100);
   };
 
@@ -38,7 +38,8 @@ export default ({
       }, 100);
       Taro.hideTabBar();
     } else {
-      Taro.showTabBar();
+      Taro.showTabBar().catch(()=> {});
+      
     }
   }, [show]);
 

+ 41 - 49
src/pages/profile/index.tsx

@@ -1,6 +1,6 @@
 import PageCustom from "@/components/page-custom/index";
 import { View, Image } from "@tarojs/components";
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
 import ComponentList from "@/components/component-list";
 
 import NavBarNormal from "@/components/NavBarNormal/index";
@@ -15,12 +15,36 @@ import style from "./index.module.less";
 import { sceneUnzip } from "@/service/wechat";
 import { postLog } from "./profile";
 import { useIsLogin } from "@/xiaolanbenlib/hooks/data/useAuth";
+import useSWR from "swr";
 
 export default function Profile() {
-  const { agentProfile, fetchAgentProfile, createAgent } = useAgentStore();
+  const { agentProfile, fetchAgentProfile, clearProfileAgent } = useAgentStore();
   const params = useRouter().params;
   const isLogin = useIsLogin();
 
+  const [agentId, setCurrentAgentId]  = useState<null|string>(null)
+  const [shareKey, setShareKey] = useState<null|string>(null)
+
+  console.log(agentProfile, agentProfile?.status)
+  // 定时 30 秒请求一次 log 接口
+  useSWR( (agentId && (agentProfile?.status === 'normal')) ? ['/blue-aiagent/api/v1/agent/visitor/log', {
+    agentId: agentId, 
+    shareKey: shareKey,
+    }] : false
+    ,
+    ([_url, data]) => {
+      
+      if(isLogin){
+        postLog(data.agentId, data.shareKey ?? '')
+      } 
+    },
+    {
+      refreshInterval: 30000,
+      revalidateOnFocus: false,
+      shouldRetryOnError: false,
+    }
+  );
+
   // 解码
   const decodeSceneValue = async (sceneValue: string) => {
     const response = await sceneUnzip(sceneValue);
@@ -36,7 +60,9 @@ export default function Profile() {
             originalScene.agentId,
             originalScene.shareKey ?? ""
           );
-          postLog(originalScene.agentId, originalScene.shareKey ?? "");
+          setCurrentAgentId(originalScene.agentId)
+          setShareKey(originalScene.shareKey ?? "")
+          // postLog(originalScene.agentId, originalScene.shareKey ?? "");
         }
       } catch (e) {
         Taro.showToast({
@@ -47,21 +73,31 @@ export default function Profile() {
     }
   };
   console.log(params.scene, "场景值");
-  // 通过小程序码识别打开的页面
+
+  
   useEffect(() => {
+    // 通过小程序码识别打开的页面
     const scene = decodeURIComponent(params.scene ?? "");
     if (scene.length <= 32 && scene.length > 0) {
       // 有值,且小于 32 位,说明需要解码
       // 判断是否需要解压 shareKey
       decodeSceneValue(scene);
     }
+
+    return ()=> {
+      setCurrentAgentId(null)
+      setShareKey(null)
+      clearProfileAgent()
+    }
   }, []);
 
   // 正常分享出来的页面
   useEffect(() => {
     if (params.agentId) {
       fetchAgentProfile(params.agentId, params.shareKey ?? "");
-      isLogin && postLog(params.agentId, params.shareKey);
+      setCurrentAgentId(params.agentId)
+      setShareKey(params.shareKey ?? "")
+      // isLogin && postLog(params.agentId, params.shareKey);
     }
   }, [params.agentId]);
 
@@ -98,50 +134,6 @@ export default function Profile() {
         </View>
       </View>
     );
-    // return (
-    // <View className={style.navButtons}>
-    //   {pages.length > 1 && (
-    //     <>
-    //       <View
-    //         className="flex-center"
-    //         onClick={() => {
-    //           Taro.navigateBack();
-    //         }}
-    //       >
-    //         <IconArrowLeft></IconArrowLeft>
-    //       </View>
-    //       <View className={style.navGapLine}></View>
-    //     </>
-    //   )}
-
-    //   <View
-    //     className="flex-center"
-    //     onClick={() => {
-    //       Taro.switchTab({
-    //         url: "/pages/index/index",
-    //       });
-    //     }}
-    //   >
-    //     <IconHomeOutline></IconHomeOutline>
-    //   </View>
-    // </View>
-    // );
-  };
-
-  const handleCreate = async () => {
-    try {
-      const agentDetail = await createAgent();
-      if (agentDetail) {
-        if (agentDetail) {
-          Taro.navigateTo({
-            url: `/pages/agent/index?agentId=${agentDetail.agentId}`,
-          });
-        }
-        // Taro.navigateTo({
-        //   url: `/pages/agent/index?agentId=${agentDetail.agentId}`
-        // })
-      }
-    } catch (e) {}
   };
 
   const renderButton = ()=> {

+ 3 - 3
src/pages/profile/profile.ts

@@ -1,6 +1,6 @@
 import { postVisitorLog } from "@/service/agent";
-import { getLoginId, isSuccess } from "@/utils";
-export const postLog = (agentId: string, shareKey?: string) => {
+import { getLoginId } from "@/utils";
+export const postLog = (agentId: string, shareKey: string) => {
   const loginId = getLoginId();
-  postVisitorLog({ agentId: agentId, loginId, shareKey: shareKey ?? '' });
+  postVisitorLog({ agentId: agentId, loginId, shareKey: shareKey});
 }

+ 15 - 0
src/store/agentStore.ts

@@ -39,6 +39,7 @@ export interface AgentStoreState {
   // 请求智能体数据非登录状态
   fetchAgentProfile: (agentId: string, shareKey?: string) => Promise<TAgentDetail | null>;
   createAgent: () => Promise<TAgentDetail | null>;
+  clearProfileAgent: () => void;
   setDefaultAgent: (agentId: string) => Promise<TAgentDetail | null>;
   editAgentCharacter: (
     agentId: string,
@@ -148,6 +149,20 @@ export const useAgentStore = create<AgentStoreState>((set, get) => ({
     }
     return null;
   },
+  clearProfileAgent: ()=> {
+    set({
+      agentProfile: null,
+      agentProfileContactCard: {
+        address: "",
+        email: '',
+        entName: '',
+        mobile: '',
+        name: '',
+        position: '',
+        qrCodeUrl: '',
+      },
+    });
+  },
   setDefaultAgent: async (agentId: string) => {
     const response = await _setDefaultAgent(agentId);
     const result = isSuccess(response.status)