Prechádzať zdrojové kódy

对话前端接口token验证请求修改

Zhangbw 2 mesiacov pred
rodič
commit
f0e55f725c

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 1 - 1
dist/assets/index-COMs89qP.js


Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 0
dist/assets/index-Cg9khDjC.css


+ 2 - 2
dist/index.html

@@ -9,8 +9,8 @@
     html, body { width: 100%; height: 100%; overflow: hidden; }
     body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; }
   </style>
-  <script type="module" crossorigin src="/assets/index-BUAcQOf0.js"></script>
-  <link rel="stylesheet" crossorigin href="/assets/index-Bmqqad_0.css">
+  <script type="module" crossorigin src="/assets/index-COMs89qP.js"></script>
+  <link rel="stylesheet" crossorigin href="/assets/index-Cg9khDjC.css">
 </head>
 <body>
   <div id="app"></div>

+ 9 - 2
src/App.vue

@@ -1,5 +1,8 @@
 <template>
-  <Login v-if="!isLoggedIn" @login-success="handleLoginSuccess" />
+  <div v-if="isChecking" style="display: flex; justify-content: center; align-items: center; height: 100vh;">
+    <div>正在验证登录状态...</div>
+  </div>
+  <Login v-else-if="!isLoggedIn" @login-success="handleLoginSuccess" />
   <CustomerService v-else />
 </template>
 
@@ -10,6 +13,7 @@ import CustomerService from './CustomerService.vue'
 import { API_ENDPOINTS } from './config/api'
 
 const isLoggedIn = ref(false)
+const isChecking = ref(true)
 
 onMounted(async () => {
   // 检查是否已登录
@@ -24,7 +28,9 @@ onMounted(async () => {
         }
       })
 
-      if (response.ok) {
+      const result = await response.json()
+
+      if (response.ok && result.code === 200) {
         // Token 有效,显示对话界面
         isLoggedIn.value = true
       } else {
@@ -42,6 +48,7 @@ onMounted(async () => {
       isLoggedIn.value = false
     }
   }
+  isChecking.value = false
 })
 
 const handleLoginSuccess = () => {

+ 33 - 2
src/CustomerService.vue

@@ -561,7 +561,9 @@ const sendTextMessage = async () => {
       selectedAgent.value,
       selectedAgentData?.gender === 'male' ? '0' : '1',
       ttsVcnList.value,
-      false
+      false,
+      customerPhone.value,
+      2  // type 固定为 2,代表 ZoomPhone 客户
     )
   } catch (error) {
     console.error('发送消息失败:', error)
@@ -588,9 +590,19 @@ const fetchTtsVcnList = async () => {
       headers: getHeaders()
     })
     const data = await response.json()
-    ttsVcnList.value = data || []
+
+    // 检查响应是否成功
+    if (response.ok && Array.isArray(data)) {
+      ttsVcnList.value = data
+    } else if (data && data.code === 200 && Array.isArray(data.data)) {
+      ttsVcnList.value = data.data
+    } else {
+      console.error('获取发言人字典失败:', data)
+      ttsVcnList.value = []
+    }
   } catch (error) {
     console.error('获取发言人字典失败:', error)
+    ttsVcnList.value = []
   }
 }
 
@@ -601,6 +613,16 @@ const fetchAgents = async (silent = false) => {
       headers: getHeaders()
     })
     const result = await response.json()
+
+    // 检查token是否过期
+    if (result.code === 401) {
+      console.error('Token已过期,请重新登录')
+      localStorage.removeItem('talk_token')
+      localStorage.removeItem('talk_user')
+      window.location.reload()
+      return
+    }
+
     if (result.code === 200 && result.data) {
       const agent = result.data
       // 只显示状态为0(空闲中)或2(工作中)的客服
@@ -739,6 +761,15 @@ const startChat = async () => {
 
     const result = await response.json()
 
+    // 检查token是否过期
+    if (result.code === 401) {
+      console.error('Token已过期,请重新登录')
+      localStorage.removeItem('talk_token')
+      localStorage.removeItem('talk_user')
+      window.location.reload()
+      return
+    }
+
     // 检查后端返回结果,判断是否成功占用客服
     if (!result.success || result.code !== 200) {
       ElMessage.error(result.msg || '该客服已被其他用户占用,请选择其他客服')

+ 22 - 4
src/composables/useStreamChat.js

@@ -84,7 +84,7 @@ export function useStreamChat() {
     }
   }
 
-  const sendMessage = (message, agentId, agentGender, ttsVcnList, isGreeting = false) => {
+  const sendMessage = (message, agentId, agentGender, ttsVcnList, isGreeting = false, customerPhone = null, type = null) => {
     return new Promise((resolve, reject) => {
       // 发送新消息前,停止当前播放的音频
       stopAudio()
@@ -105,7 +105,9 @@ export function useStreamChat() {
         ttsVcnList,
         conversationId: conversationId.value,
         isGreeting,
-        requestId: thisRequestId  // 发送requestId到后端
+        requestId: thisRequestId,  // 发送requestId到后端
+        customerPhone,
+        type
       }
 
       fetch(API_ENDPOINTS.messageStream, {
@@ -113,10 +115,26 @@ export function useStreamChat() {
         headers: {
           'Content-Type': 'application/json',
           'Authorization': token ? `Bearer ${token}` : '',
-          'clientid': 'talk-web'
+          'clientid': '812b745b34558590c92e6f13fe8b716b'
         },
         body: JSON.stringify(requestBody)
-      }).then(response => {
+      }).then(async response => {
+        // 检查Content-Type,如果是JSON说明是错误响应
+        const contentType = response.headers.get('content-type')
+        if (contentType && contentType.includes('application/json')) {
+          const errorData = await response.json()
+          if (errorData.code === 401) {
+            console.error('Token已过期,请重新登录')
+            localStorage.removeItem('talk_token')
+            localStorage.removeItem('talk_user')
+            window.location.reload()
+            reject(new Error('Token已过期'))
+            return
+          }
+          reject(new Error(errorData.msg || '请求失败'))
+          return
+        }
+
         const reader = response.body.getReader()
         const decoder = new TextDecoder()
         let buffer = '' // 累积未处理的数据

+ 11 - 1
src/composables/useVoiceRecognition.js

@@ -17,10 +17,20 @@ const loadConfig = async () => {
     const response = await fetch(API_ENDPOINTS.xunfeiConfig, {
       headers: {
         'Authorization': token ? `Bearer ${token}` : '',
-        'clientid': 'talk-web'
+        'clientid': '812b745b34558590c92e6f13fe8b716b'
       }
     })
     const config = await response.json()
+
+    // 检查token是否过期
+    if (config.code === 401) {
+      console.error('Token已过期,请重新登录')
+      localStorage.removeItem('talk_token')
+      localStorage.removeItem('talk_user')
+      window.location.reload()
+      return
+    }
+
     XFYUN_CONFIG.APPID = config.appId || ''
     XFYUN_CONFIG.ACCESS_KEY_ID = config.apiKey || ''
     XFYUN_CONFIG.ACCESS_KEY_SECRET = config.apiSecret || ''

Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov