From a76b5063271f254bd15bb2ff932932d2a591a381 Mon Sep 17 00:00:00 2001 From: abearxiong Date: Sat, 21 Jun 2025 17:32:08 +0800 Subject: [PATCH] fix: add split and for sleep post data --- packages/xhs/src/routes/mentions/mention.ts | 21 +++++++++++---------- packages/xhs/src/test/split-text.ts | 8 ++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 packages/xhs/src/test/split-text.ts diff --git a/packages/xhs/src/routes/mentions/mention.ts b/packages/xhs/src/routes/mentions/mention.ts index 5166ed5..53efb90 100644 --- a/packages/xhs/src/routes/mentions/mention.ts +++ b/packages/xhs/src/routes/mentions/mention.ts @@ -4,6 +4,14 @@ import { Mention } from '@kevisual/xhs/libs/xhs-type/mention.ts'; const sleep = (ms: number) => { return new Promise((resolve) => setTimeout(resolve, ms)); }; +export const splitContent = (content: string, maxLength: number = 300) => { + const parts: string[] = []; + for (let i = 0; i < content.length; i += maxLength) { + parts.push(content.slice(i, i + maxLength)); + } + return parts; +}; + app .route({ path: 'mention', @@ -76,17 +84,9 @@ app const { note_id, comment_id, content } = ctx.query; const client = xhsServices.getClient(); // content 300个字内,超过cai fen - const textArr: string[] = []; - if (content.length > 300) { - const num = Math.ceil(content.length / 300); - for (let i = 0; i < num; i++) { - textArr.push(content.slice(i * 300, (i + 1) * 300)); - } - } else { - textArr.push(content); - } + const textArr: string[] = splitContent(content); const resArr: any[] = []; - for (const text of textArr) { + for (let text of textArr) { const res = await client.postComment({ note_id: note_id, comment_id: comment_id, @@ -98,6 +98,7 @@ app console.log('添加评论失败', res.code); ctx.throw(res.code, '添加评论失败'); } + await sleep(2000); } ctx.body = resArr; }) diff --git a/packages/xhs/src/test/split-text.ts b/packages/xhs/src/test/split-text.ts new file mode 100644 index 0000000..89b02f8 --- /dev/null +++ b/packages/xhs/src/test/split-text.ts @@ -0,0 +1,8 @@ +import { splitContent } from '../routes/mentions/mention.ts'; + +const content = `这是一个测试内容,长度超过300个字符。34`.repeat(43); // 模拟一个超过300个字符的内容 +const maxLength = 300; +const parts = splitContent(content, maxLength); +console.log('分割后的内容:', parts); +// 输出分割后的内容 +console.log('分割后的内容长度:', parts.map(part => part.length)); \ No newline at end of file