fix: add split and for sleep post data

This commit is contained in:
熊潇 2025-06-21 17:32:08 +08:00
parent 2c6c3dd60d
commit a76b506327
2 changed files with 19 additions and 10 deletions

View File

@ -4,6 +4,14 @@ import { Mention } from '@kevisual/xhs/libs/xhs-type/mention.ts';
const sleep = (ms: number) => { const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms)); 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 app
.route({ .route({
path: 'mention', path: 'mention',
@ -76,17 +84,9 @@ app
const { note_id, comment_id, content } = ctx.query; const { note_id, comment_id, content } = ctx.query;
const client = xhsServices.getClient(); const client = xhsServices.getClient();
// content 300个字内超过cai fen // content 300个字内超过cai fen
const textArr: string[] = []; const textArr: string[] = splitContent(content);
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 resArr: any[] = []; const resArr: any[] = [];
for (const text of textArr) { for (let text of textArr) {
const res = await client.postComment({ const res = await client.postComment({
note_id: note_id, note_id: note_id,
comment_id: comment_id, comment_id: comment_id,
@ -98,6 +98,7 @@ app
console.log('添加评论失败', res.code); console.log('添加评论失败', res.code);
ctx.throw(res.code, '添加评论失败'); ctx.throw(res.code, '添加评论失败');
} }
await sleep(2000);
} }
ctx.body = resArr; ctx.body = resArr;
}) })

View File

@ -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));