vue
安装
npm install @microsoft/fetch-event-source
//链接fetchEventSource
方法
function connectFetchEventSource(param: any) {
const ctrl = new AbortController()
const eventSource: any = fetchEventSource(param.api_url, {
method: 'POST',
// mode: 'no-cors',
headers: {
'Content-Type': 'application/json',
// Authorrization: 'Bearer sk-q7LIaxAS5pTIofgd82C88dE2B5Db4c979976B5Da9cB15a84'
Authorization: 'Bearer ' + param.api_sk
},
body: JSON.stringify(param),//参数
signal: ctrl.signal,
onmessage(ev) {
结果
let result = JSON.parse(ev.data)
},
onclose() {
// eventSource.abort()
},
onerror(error) {
console.log(error)
// eventSource.abort()
}
})
console.log('submit!')
}
小程序
//外部js引用时这样写:import {Base64} from '/xxx/base64';
const Base64 = {
// 转码表
tables : [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O' ,'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'
],
UTF16ToUTF8 : function (str) {
let results = [], len = str.length;
for (let i = 0; i < len; i++) {
let code = str.charCodeAt(i);
if (code > 0x0000 && code <= 0x007F) {
/* 一字节,不考虑0x0000