Browse Source

公开访问页面

CzRger 2 weeks ago
parent
commit
81f620c79f

+ 1 - 2
src/views/chat/chat.ts

@@ -54,7 +54,6 @@ const handleStream = ({
               return
             }
             const { event } = bufferObj
-            console.log(bufferObj)
             if (event === 'chat_start') {
               onStart?.(bufferObj)
             } else if (event === 'message') {
@@ -70,12 +69,12 @@ const handleStream = ({
               })
               onError?.(bufferObj.message, bufferObj)
             } else {
+              console.error(bufferObj)
             }
           }
         })
         buffer = lines[lines.length - 1]
       } catch (e) {
-        console.log('zxcxllasldsll')
         hasError = true
         return
       }

+ 13 - 2
src/views/chat/index.vue

@@ -8,7 +8,12 @@
     />
   </template>
   <template v-else>
-    <normalChat ref="ref_chat" :ID="ID" :test="test">
+    <normalChat
+      ref="ref_chat"
+      :ID="ID"
+      :test="test"
+      @onStartChat="(p) => $emit('onStartChat', p)"
+    >
       <template
         v-for="key in Object.keys($slots)"
         :key="key"
@@ -21,10 +26,11 @@
 </template>
 
 <script setup lang="ts">
-import { nextTick, onMounted, reactive, ref } from 'vue'
+import { nextTick, onMounted, reactive, ref, watch } from 'vue'
 import normalChat from './normal.vue'
 import onlineChat from './online.vue'
 
+const emit = defineEmits(['onStartChat'])
 const props = defineProps({
   online: { default: false },
   test: { default: false },
@@ -33,11 +39,16 @@ const props = defineProps({
 const state: any = reactive({
   isOnline: props.online,
 })
+watch(
+  () => props.online,
+  (n) => (state.isOnline = n),
+)
 const ref_chat = ref()
 defineExpose({
   init: () => ref_chat.value?.init(),
   history: (conveargsrsationId) => ref_chat.value?.history(conveargsrsationId),
   mockAnswer: (answer) => ref_chat.value?.mockAnswer(answer),
+  send: (ask) => ref_chat.value?.send(ask),
 })
 </script>
 

+ 10 - 2
src/views/chat/normal.vue

@@ -103,6 +103,7 @@ import {
   appMessagesSuggestedQuestions,
 } from '@/api/modules/app/chat'
 
+const emit = defineEmits(['onStartChat'])
 const props = defineProps({
   ID: { default: '' },
   test: { default: false },
@@ -117,8 +118,9 @@ const state: any = reactive({
     conversationId: '',
     parentMessageId: '',
   },
-  isWaiting: false,
-  isStop: false,
+  isWaiting: false, // 是否在等待提问开始回答
+  isStop: false, // 提问是否回答完毕,回答完毕可以开始下一次提问
+  isFirst: false, // 是否为第一次提问,返回首次提问内容
 })
 const ref_text = ref()
 const ref_chatMsg = ref()
@@ -138,6 +140,7 @@ const onReset = () => {
   state.params.parentMessageId = ''
   state.isWaiting = false
   state.isStop = false
+  state.isFirst = true
 }
 const initChat = (isPrologue = true) => {
   return new Promise(async (resolve) => {
@@ -238,6 +241,10 @@ const onSend = (text = '', isSet = false) => {
       type: 'ask',
       text: state.params.query + '',
     }
+    if (state.isFirst) {
+      state.isFirst = false
+      emit('onStartChat', { ask: ask.text })
+    }
     state.chats.push(ask)
     const answer: AnswerStruct = reactive({
       type: 'answer',
@@ -349,6 +356,7 @@ defineExpose({
   init: initChat,
   history: initHistory,
   mockAnswer,
+  send: (ask) => onSend(ask, true),
 })
 </script>
 

+ 49 - 70
src/views/global/app-public/index.vue

@@ -17,18 +17,6 @@
               <div class="text-xl font-bold text-[#303133]" v-title>
                 {{ state.detail.name }}
               </div>
-              <!--              <el-tooltip-->
-              <!--                :content="false ? '取消收藏' : '收藏'"-->
-              <!--                effect="light"-->
-              <!--                placement="top"-->
-              <!--              >-->
-              <!--                <SvgIcon-->
-              <!--                  name="star"-->
-              <!--                  size="20"-->
-              <!--                  class="__hover ml-auto"-->
-              <!--                  :active="false"-->
-              <!--                />-->
-              <!--              </el-tooltip>-->
             </div>
             <div class="flex gap-2 text-sm text-[#606266]">
               <div>{{ state.detail.creatorName }}</div>
@@ -51,11 +39,8 @@
             <SvgIcon name="clean" class="mr-1" />清除记录
           </div>
         </div>
-        <div
-          class="mt-4 mr-2 flex flex-1 flex-col gap-4 overflow-y-auto pr-2"
-          v-loading="state.history.loading"
-        >
-          <template v-for="(item, index) in state.history.data">
+        <div class="mt-4 mr-2 flex flex-1 flex-col gap-4 overflow-y-auto pr-2">
+          <template v-for="(item, index) in state.history">
             <div
               class="__hover flex items-center gap-2 rounded-lg p-2"
               :style="{
@@ -78,7 +63,7 @@
                 <SvgIcon
                   name="czr_del"
                   class="__hover ml-auto"
-                  @click.stop="onDel(item)"
+                  @click.stop="onDel(index)"
                 />
               </el-tooltip>
             </div>
@@ -89,14 +74,19 @@
         class="m-1 flex-1 overflow-hidden rounded-lg border-1 border-[var(--czr-main-color)]/5 shadow"
         v-loading="state.chat.loading"
       >
-        <chat :online="state.chat.isOnline" :ID="state.ID" ref="ref_chat" />
+        <chat
+          :online="state.chat.isOnline"
+          :ID="state.ID"
+          ref="ref_chat"
+          @onStartChat="onAdd"
+        />
       </div>
     </div>
   </div>
 </template>
 
 <script setup lang="ts">
-import { nextTick, onMounted, reactive, ref } from 'vue'
+import { computed, nextTick, onMounted, reactive, ref } from 'vue'
 import { useRoute, useRouter } from 'vue-router'
 import chat from '@/views/chat/index.vue'
 import {
@@ -115,11 +105,9 @@ const colors = ['46, 155, 62', '0, 159, 188', '119, 69, 222', '255, 162, 84']
 const state: any = reactive({
   ID: route.params.id,
   CODE: route.params.code,
+  LocalStoreKey: 'appPublic' + route.params.id,
   detail: {},
-  history: {
-    loading: false,
-    data: [],
-  },
+  history: [],
   chat: {
     isOnline: false,
     loading: false,
@@ -127,75 +115,66 @@ const state: any = reactive({
 })
 const ref_chat = ref()
 const initHistory = () => {
-  state.history.loading = true
-  appConversationsSearch({
-    appId: state.ID,
-    page: 1,
-    size: 100000,
-  })
-    .then(({ data }: any) => {
-      state.history.data = data.records
-    })
-    .finally(() => {
-      state.history.loading = false
+  const arr: any = []
+  const data = localStorage.getItem(state.LocalStoreKey)
+  if (data) {
+    data.split(',')?.forEach((v) => {
+      arr.push({ name: v })
     })
+  }
+  state.history = arr
 }
-const onDel = (row) => {
+const onDel = (index) => {
   DialogStore.confirm({
     title: '删除确认',
     content: `请确认是否删除`,
     onSubmit: () => {
-      appConversationsDel(row.id, { appId: state.ID })
-        .then(() => {
-          ElMessage.success('删除成功!')
-        })
-        .catch(() => {})
-        .finally(() => {
-          initHistory()
-        })
+      const arr = state.history.filter((v, i) => i !== index).map((v) => v.name)
+      localStorage.setItem(state.LocalStoreKey, arr.join(','))
+      ElMessage.success('删除成功!')
+      initHistory()
     },
   })
 }
 const onHistory = (row) => {
-  ref_chat.value?.history(row.id)
+  ref_chat.value?.send(row.name)
 }
 const onClear = () => {
   DialogStore.confirm({
     title: '删除确认',
     content: `请确认是否清除记录?`,
     onSubmit: () => {
-      appDelConversations(state.ID)
-        .then(() => {
-          ElMessage.success('清除记录成功!')
-        })
-        .catch(() => {})
-        .finally(() => {
-          initHistory()
-        })
+      localStorage.removeItem(state.LocalStoreKey)
+      ElMessage.success('清除记录成功!')
+      initHistory()
     },
   })
 }
+const onAdd = ({ ask }) => {
+  const arr = [ask, ...state.history.map((v) => v.name)]
+  localStorage.setItem(state.LocalStoreKey, arr.join(','))
+  initHistory()
+}
 onMounted(() => {
   initDetail()
 })
 const initDetail = () => {
-  console.log(route)
-  if (state.ID) {
-    // state.chat.loading = true
-    // appDetail(state.ID)
-    //   .then(({ data }: any) => {
-    //     state.detail = data
-    //     document.title = state.detail.name
-    //     state.chat.isOnline = state.detail.userInputMethod == 1
-    //     state.chat.loading = false
-    //     nextTick(() => {
-    //       ref_chat.value?.init()
-    //     })
-    //     initHistory()
-    //   })
-    //   .catch(() => {
-    //     router.push({ name: '4806d051-e037-4d9d-99a0-78aa2f2f362b' })
-    //   })
+  if (state.ID && state.CODE) {
+    state.chat.loading = true
+    appDetail(state.ID)
+      .then(({ data }: any) => {
+        state.detail = data
+        document.title = state.detail.name
+        state.chat.isOnline = state.detail.appModelConfig.userInputMethod == 1
+        state.chat.loading = false
+        nextTick(() => {
+          ref_chat.value?.init()
+        })
+        initHistory()
+      })
+      .catch(() => {
+        router.push({ name: '4806d051-e037-4d9d-99a0-78aa2f2f362b' })
+      })
   } else {
     router.push({ name: 'NotFound' })
   }

+ 1 - 1
src/views/manage/home/app/index.vue

@@ -191,7 +191,7 @@ const initDetail = () => {
       .then(({ data }: any) => {
         state.detail = data
         document.title = state.detail.name
-        state.chat.isOnline = state.detail.userInputMethod == 1
+        state.chat.isOnline = state.detail.appModelConfig.userInputMethod == 1
         state.chat.loading = false
         nextTick(() => {
           ref_chat.value?.init()