2025-05-05 00:01:36 +08:00

132 lines
3.3 KiB
TypeScript

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();
const res = await client.postComment({
note_id: note_id,
comment_id: comment_id,
content,
});
if (res.code === 0) {
ctx.body = res.data;
} else {
console.log('添加评论失败', res.code);
ctx.throw(res.code, '添加评论失败');
}
})
.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);