引入
ts
import {
parseSSEChunkInH5,
parseSSEChunkInMP,
fetchSSECoreInH5,
getLoginCookie,
fetchSSECoreInMP,
fetchSSECore
} from 't-comm';
// 不支持 tree-shaking 的项目
import {
parseSSEChunkInH5,
parseSSEChunkInMP,
fetchSSECoreInH5,
getLoginCookie,
fetchSSECoreInMP,
fetchSSECore
} from 't-comm/lib/sse/index';
// 只支持 ESM 的项目
import {
parseSSEChunkInH5,
parseSSEChunkInMP,
fetchSSECoreInH5,
getLoginCookie,
fetchSSECoreInMP,
fetchSSECore
} from 't-comm/es/sse/index';parseSSEChunkInH5
描述:
解析 H5 环境下的 SSE 数据流
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| params | Object | 参数对象 |
| params.success | function | 成功回调函数 |
| params.fail | function | 失败回调函数 |
| params.complete | function | 完成回调函数 |
| params.response | Response | 响应对象 |
返回: Promise
返回一个 Promise,处理 SSE 数据流
parseSSEChunkInMP
描述:
解析 MP 环境下的 SSE 数据块
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| params | Object | 参数对象 |
| params.chunk | string | 数据块 |
| [params.fail] | Fail | 失败回调函数 |
| [params.success] | Success | 成功回调函数 |
fetchSSECoreInH5(params)
描述:
在 H5 环境下发起 SSE 请求的核心函数
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| params | Object | 参数对象 |
| params.url | string | 请求 URL |
| params.data | any | 请求数据 |
| params.success | function | 成功回调函数 |
| params.fail | function | 失败回调函数 |
| params.complete | function | 完成回调函数 |
返回: Promise
返回一个 Promise,包含响应对象
getLoginCookie(isTestEnv)
描述:
获取登录 Cookie
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| isTestEnv | function | 是否为测试环境的函数 |
返回: string
返回拼接后的 Cookie 字符串
fetchSSECoreInMP(params)
描述:
在 MP 环境下发起 SSE 请求的核心函数
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| params | RequestParams | 请求参数 |
返回: Promise
返回一个 Promise,包含请求任务对象
fetchSSECore(params)
描述:
发起 SSE 请求的核心函数,根据环境自动选择 H5 或 MP 实现
参数:
| 参数名 | 类型 | 描述 |
|---|---|---|
| params | RequestParams | 请求参数 |
| params.url | string | 请求地址 |
| params.data | Object | 请求数据 |
| params.success | function | 成功回调 |
| params.fail | function | 失败回调 |
| params.complete | function | 完成回调 |
返回: Promise
返回一个 Promise,包含请求任务或响应对象
示例
ts
import { isTestEnv } from '@tencent/pmd-tools/lib/env';
import { cookie } from '@tencent/pmd-tools/lib/storage';
import { safeJsonParse } from 't-comm/es/json';
import { fetchSSECore, type RequestParams } from 't-comm/es/sse';
// 检查是否结束,业务自定义
function checkFinish(str) {
const data: any = safeJsonParse(str);
return data?.status === 3;
}
// 业务二次封装,把自己的请求数据、自定义的处理数据的逻辑放进去
export function sendChatMessage({
input,
sessionId,
extraInfo,
expectedOp,
success,
fail,
complete,
}: {
input: string;
sessionId?: string;
extraInfo?: string;
expectedOp?: Record<string, any>;
success?: (data: any) => void;
fail?: RequestParams['fail'];
complete?: RequestParams['complete'];
}) {
const origin = isTestEnv() ? 'https://xx.com' : 'https://xx.com';
const url = `${origin}/xx.xx.xx.xx/xx?g_app_tk=${cookie.get('tip_token')}&tstamp=${Date.now()}`;
const reqData = {
input,
session_id: sessionId,
extra_info: extraInfo,
expected_op: expectedOp,
};
const parsedSuccess = (str, fullStr) => {
const data: any = safeJsonParse(str);
const fullData: any = safeJsonParse(fullStr);
if (checkFinish(str)) {
complete?.(str);
return;
}
// 检查是否失败,业务自定义
if (fullData?.err_msg) {
fail?.(data);
return;
}
success?.(data);
};
return fetchSSECore({
url,
data: reqData,
success: parsedSuccess,
fail,
complete,
isTestEnv,
});
}