Explorar o código

feat: fetchAgents

王晓东 hai 2 días
pai
achega
a27fe642ff

+ 27 - 25
src/pages/index/components/welcome/index.tsx

@@ -3,13 +3,15 @@ import style from "./index.module.less";
 import Taro from "@tarojs/taro";
 
 import { getAgents, getAgent, createAgent, } from "@/service/agent";
-import { TAgentItem } from '@/types/agent'
+
+import { TAgentDetail } from '@/types/agent'
 
 export default function Index({children}) {
+  
   const saveAgent = async (agent: TAgentItem) => {
     const res = await createAgent(agent)
     console.log(res)
-    // Taro.navigateTo({url: '/pages/agent-gen/index'})
+    Taro.navigateTo({url: '/pages/agent-gen/index'})
   }
   const test = ()=> {
     
@@ -18,28 +20,28 @@ export default function Index({children}) {
     getAgents()
     
     console.log('gogogo')
-    return 
-    saveAgent({
-      address: '浙江杭州余杭文一西路1700号',
-      avatarUrl: 'https://cdn.wehome.cn/cmn/png/53/META-H8UKWHWU-2JNUAG2BARJF55VHU9QS3-YBQGHDAM-IW.png',
-      email: '',
-      enabledChatBg: false,
-      enabledPersonalKb: false,
-      "entId": '',
-      "entName": '',
-      "greeting": '',
-      "isDefault": true,
-      "isEnt": false,
-      "isNewEnt": false,
-      "isSystemVoice": true,
-      "mobile": '18658870618',
-      "name": 'xiaodong',
-      "personality": '一名疯瘨的数码博主',
-      "position": 'CEO',
-      "qrCodeUrl": '',
-      "questionGuides": ['想要了解相机?', '想要了解手机?'],
-      // "voice": ''
-    })
+    // return 
+    // saveAgent({
+    //   address: '浙江杭州余杭文一西路1700号',
+    //   avatarUrl: 'https://cdn.wehome.cn/cmn/png/53/META-H8UKWHWU-2JNUAG2BARJF55VHU9QS3-YBQGHDAM-IW.png',
+    //   email: '',
+    //   enabledChatBg: false,
+    //   enabledPersonalKb: false,
+    //   "entId": '',
+    //   "entName": '',
+    //   "greeting": '',
+    //   "isDefault": true,
+    //   "isEnt": false,
+    //   "isNewEnt": false,
+    //   "isSystemVoice": true,
+    //   "mobile": '18658870618',
+    //   "name": 'xiaodong',
+    //   "personality": '一名疯瘨的数码博主',
+    //   "position": 'CEO',
+    //   "qrCodeUrl": '',
+    //   "questionGuides": ['想要了解相机?', '想要了解手机?'],
+    //   // "voice": ''
+    // })
     
   }
 
@@ -63,7 +65,7 @@ export default function Index({children}) {
               <View>3.你的内容,一页呈现</View>
           </View>
           <View className="button-rounded-big" onClick={go}>创建智能体</View>
-          <View className="button-rounded-big" onClick={test}>test</View>
+          {/* <View className="button-rounded-big" onClick={test}>test</View> */}
         </View>
       </View>
       </View>

+ 8 - 3
src/pages/index/index.tsx

@@ -10,12 +10,15 @@ import { onLogout, useIsLogin } from '@/xiaolanbenlib/hooks/data/useAuth'
 import refreshUserId, { clearUserInfo, getOpenIdAsync } from '@/xiaolanbenlib/utils/auth'
 
 import WelcomeTips from './components/welcome/index'
+import { useAgentStore } from '@/store/agentStore'
 
 export default function Index() {
 
   const [userInfo, setUserInfo] = useState<UserInfoResponse>()
   const isLogin = useIsLogin()
 
+  const {fetchAgents} =  useAgentStore()
+
   function initUserInfo() {
     refreshUserId()
       .then((value) => {
@@ -23,6 +26,10 @@ export default function Index() {
         getOpenIdAsync().then((openId) => {
           console.log('🚀 ~ getOpenIdAsync ~ value:', openId)
         })
+        fetchAgents().then((value)=> {
+          const agent = value[0]
+          Taro.navigateTo({url: `/pages/profile/index?agentId=${agent.agentId}`})
+        })
       })
       .catch((error) => {
         if (error.message === 'unauthorized' || error.code === 401) {
@@ -38,10 +45,8 @@ export default function Index() {
   useEffect(() => {
     if (isLogin) {
       initUserInfo()
-    }else{
-      // Taro.navigateTo({url: '/pages/login/index'})
     }
-    
+    // Taro.navigateTo({url: '/pages/login/index'})
   }, [isLogin])
   
 

+ 8 - 1
src/pages/profile/components/top-bar/index.tsx

@@ -18,6 +18,7 @@ import AgentQRCode from "../agent-qrcode/index";
 import SharePopup from "@/components/custom-share/share-popup";
 import Taro from "@tarojs/taro";
 import PopupSheets from "@/components/popup/popup-sheets";
+import { useAgentStore } from "@/store/agentStore";
 
 export default () => {
   const headerHeight = useAppStore((state) => state.headerHeight);
@@ -26,6 +27,11 @@ export default () => {
   const [showPopup, setShowPopup] = useState(false);
   const [showShare, setShowShare] = useState(false);
 
+  const agent = useAgentStore((state)=> state.agent)
+  
+
+
+
   // 自定义背景样式
   const bgImageStyle = {
     marginTop: -headerHeight,
@@ -93,7 +99,8 @@ export default () => {
               <View
                 className={style.boxButton}
                 onClick={() => {
-                  Taro.navigateTo({ url: "/pages/editor-contact/index" });
+                  Taro.navigateTo({ url: `/pages/agent/index?agentId=${agent?.agentId}` });
+                  // Taro.navigateTo({ url: "/pages/editor-contact/index" });
                 }}
               >
                 <IconEditBlack />

+ 13 - 3
src/pages/profile/index.tsx

@@ -4,12 +4,22 @@ import PageCustom from "@/components/page-custom/index";
 import { Image, Text, View } from "@tarojs/components";
 import Logo from '@/components/logo'
 import TopBar from './components/top-bar'
-
+import { useAgentStore } from '@/store/agentStore'
+import { useEffect } from "react";
+import { useRouter } from "@tarojs/taro";
 export default function Index() {
-  
 
-  
+  const router = useRouter();
+  const { agentId } = router.params;
+ 
+  const {fetchAgent} = useAgentStore()
+
 
+  useEffect(()=> {
+    if(agentId){
+      fetchAgent(agentId)
+    }
+  }, [agentId])
   // const renderFloatingTips = () => {
   //   return isShared ? <FloatingTips></FloatingTips> : <></>;
   // };

+ 4 - 10
src/service/agent.ts

@@ -2,7 +2,7 @@ import {
   bluebookAiAgent,
 } from '@/xiaolanbenlib/api/index'
 import request from '@/xiaolanbenlib/module/axios.js'
-import { TAgentDetail, TComponentItem } from '@/types/agent'
+import { TAgentDetail, TComponentItem, TAgent } from '@/types/agent'
 
 
 // 创建一个新的智能体--用户点击创建按钮时调用获取新智能体id与名称信息
@@ -59,20 +59,14 @@ export const editAgentCard = (agentId: string, data: TAgentContactCard) => {
 
 
 
-export type TAgent = {
-  "agentId": "string",
-  "isDefault": boolean,
-  "isEnt": boolean,
-  "isNewEnt": boolean,
-  "name": "string"
-}
+
 
 
 
 
 // 我的智能体列表
-export const getAgents = () => {
-  return request.get<TAgent>(`${bluebookAiAgent}api/v1/my/agents`)
+export const getAgents = (): Promise<TAgentDetail[]> =>  {
+  return request.get(`${bluebookAiAgent}api/v1/my/agents`)
 }
 // 删除智能体--仅允许删除个人智能体
 export const deleteAgent = (agentId: string) => {

+ 74 - 0
src/store/agentStore.ts

@@ -0,0 +1,74 @@
+import { create } from 'zustand'
+import {
+  bluebookAiAgent,
+} from '@/xiaolanbenlib/api/index'
+import request from '@/xiaolanbenlib/module/axios.js'
+
+import { createAgent as _createAgent, deleteAgent as _deleteAgent } from "@/service/agent";
+import { TAgentDetail, TAgent } from '@/types/agent'
+
+export interface AgentStoreState {
+  agents: TAgent[]
+  agent: TAgentDetail | null
+  fetchAgents: () => Promise<TAgent[]>
+  fetchAgent: (agentId: string) => Promise<TAgentDetail | null>
+  createAgent: (data: TAgentDetail) => Promise<TAgentDetail | null>
+  // deleteAgent: (agentId: string) => Promise<void>
+}
+
+
+
+
+export const useAgentStore = create<AgentStoreState>((set, get) => ({
+  agents: [],
+  agent: null,
+  fetchAgents: async () => {
+    const response = await request.get<TAgent[]>(`${bluebookAiAgent}api/v1/my/agents`)
+    if(response && response?.data?.length){
+      set({ agents: response.data })
+      return response.data
+    }
+    
+    return []
+  },
+  fetchAgent: async (agentId: string) => {
+    const response = await request.get<TAgentDetail>(`${bluebookAiAgent}api/v1/my/agent/${agentId}`)
+    if(response.data){
+      set({ agent: response.data})
+      return response.data
+    }
+    return null
+  },
+  createAgent: async (data: TAgentDetail) => {
+    const newAgent = await request.post(`${bluebookAiAgent}api/v1/my/agent`, data) as TAgentDetail
+    if(newAgent){
+      set((state) => {
+        return {
+          agents: [...state.agents, {
+            "agentId": newAgent.agentId ?? '',
+            "isDefault": newAgent.isDefault ?? false,
+            "isEnt": newAgent.isEnt ?? false,
+            "isNewEnt": newAgent.isNewEnt ?? false,
+            "name": newAgent.name ?? ''
+          }]
+        }
+      })
+      return newAgent
+    }
+    return null
+  },
+  // deleteAgent: async (agentId: string)=> {
+  //   const response = await _deleteAgent(agentId)
+  //   if(response.status === 200){
+  //     set((state) => {
+  //       const agents = state.agents.filter((item: TAgentDetail) => item.agentId !== agentId)
+  //       return {
+  //         agents: [...agents]
+  //       }
+  //     })
+  //   }
+  // }
+}))
+
+
+

+ 8 - 0
src/types/agent.ts

@@ -1,3 +1,11 @@
+
+export type TAgent = {
+  "agentId": string,
+  "isDefault": boolean,
+  "isEnt": boolean,
+  "isNewEnt": boolean,
+  "name": string
+}
 export type TComponentItem = {
   "data"?: any,
   "enabled"?: boolean,

+ 1 - 1
src/xiaolanbenlib/module/axios.js

@@ -40,7 +40,7 @@ axiosIns.interceptors.response.use(
       Taro.setStorageSync('u51UserId', response.headers.userId)
       Taro.setStorageSync('u51UserToken', response.headers.token)
     }
-    return response.data
+    return response
   },
   async (err) => {
     console.log(err)