sse连接
import { fetchEventSource } from '@microsoft/fetch-event-source'
import { getToken } from '@/utils/auth'
// 封装调用千问api方法,data:请求bady, key:密钥
export function aiSse(data) {
let controller = new AbortController()
const eventSource = fetchEventSource('http://cloudwarnapi.ztyc.com/event/stream', {
method: 'POST',
headers: {
Authorization: getToken(),
'Content-Type': 'application/json',
'Accept': 'text/event-stream',
'X-DashScope-SSE': 'enable'
},
body: JSON.stringify(
data
),
signal: controller.signal,
onopen() {
console.log('open')
},
onmessage(event) {
console.log('onMessage', event.data.replace('{"success":true,"msg":"成功!","code":0,"result":"', '').replace('}', ''))
},
onclose() {
controller.abort()// 出错后不要重试
eventSource.close()
},
onerror(error) {
console.log('连接错误', error)
// controller.abort();//出错后不要重试
eventSource.close()
}
})
}
// 获取ai回答接口key
export async function getAuxiliaryKeyFn() {
// 调用通义千问api
const data = {
'content': '杭州在哪里',
'requestId': 'aaaaa',
'requestName': '测试中'
}
aiSse(data)
}