Files
social-router/packages/xhs/src/routes/mentions/mention.ts
2025-06-21 16:31:00 +08:00

146 lines
3.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { app, xhsServices } from '@kevisual/xhs/app.ts';
import { Parse } from '@kevisual/xhs/libs/parse.ts';
import { Mention } from '@kevisual/xhs/libs/xhs-type/mention.ts';
const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
app
.route({
path: 'mention',
key: 'getUnread',
description: '获取提及列表',
})
.define(async (ctx) => {
const client = xhsServices.getClient();
const res = await client.getUnread();
if (res.code === 0) {
ctx.body = res.data;
} else {
ctx.body = {
unread_count: 0,
};
}
})
.addTo(app);
app
.route({
path: 'mention',
key: 'postRead',
description: '标记为已读',
})
.define(async (ctx) => {
const type = ctx.query.type || 'mentions';
const client = xhsServices.getClient();
const res = await client.postRead(type);
ctx.body = res.data;
})
.addTo(app);
app
.route({
path: 'mention',
key: 'getNote',
description: '获取笔记',
validator: {
node_id: {
type: 'string',
required: true,
},
xsec_token: {
type: 'string',
required: true,
},
},
isDebug: true,
})
.define(async (ctx) => {
const node_id = ctx.query.node_id;
const xsec_token = ctx.query.xsec_token;
const client = xhsServices.getClient();
try {
console.log('get note by node_id', node_id, 'xsec_token', xsec_token);
const res = await client.getNoteByIdFromHtml(node_id, xsec_token);
console.log('res====', res);
ctx.body = res;
} catch (error) {
console.log('res', error);
ctx.throw(500, '获取笔记失败');
}
})
.addTo(app);
app
.route({
path: 'mention',
key: 'addComment',
})
.define(async (ctx) => {
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 resArr: any[] = [];
for (const text of textArr) {
const res = await client.postComment({
note_id: note_id,
comment_id: comment_id,
content: text,
});
if (res.code === 0) {
resArr.push(res.data);
} else {
console.log('添加评论失败', res.code);
ctx.throw(res.code, '添加评论失败');
}
}
ctx.body = resArr;
})
.addTo(app);
app
.route({
path: 'mention',
key: 'getMention',
description: '获取提及列表',
validator: {
num: {
type: 'number',
required: true,
},
},
})
.define(async (ctx) => {
const num = ctx.query.num;
const client = xhsServices.getClient();
const res = await client.getMention(num);
if (res.code === 0) {
const mentionList = res.data.message_list;
const handleMention: any[] = [];
for (const mention of mentionList) {
const mention_id = mention.id;
const note_id = mention.item_info.id;
const xsec_token = mention.item_info.xsec_token;
let comment: any = Parse.getComment(mention);
// console.log('note_id', note_id, 'xsec_token', xsec_token, comment);
handleMention.push({
mention_id,
note_id,
xsec_token,
comment,
mention,
});
}
console.log('获取提及列表成功', res.code, res.data?.message_list?.length);
ctx.body = handleMention;
} else {
console.log('获取提及列表失败', res.code);
ctx.throw(res.code, '获取提及列表失败');
}
})
.addTo(app);