返回 AiToEarn
index.ts
根目录 / project / aitoearn-web / src / store / plugin / plats / xhs / index.ts
1 /**
2 * 小红书平台交互实现
3 *
4 * 目录结构:
5 * xhs/
6 * ├── index.ts # 主类和导出入口
7 * ├── types.ts # 小红书特定类型定义
8 * └── homeFeed.ts # 首页列表功能模块
9 */
10
11 import type {
12 CommentListParams,
13 CommentListResult,
14 CommentParams,
15 CommentResult,
16 FavoriteResult,
17 GetWorkDetailParams,
18 GetWorkDetailResult,
19 HomeFeedListParams,
20 HomeFeedListResult,
21 IPlatformInteraction,
22 LikeResult,
23 SubCommentListParams,
24 } from '../types'
25 import type { XhsBaseResponse, XhsCommentResponse } from './types'
26 import { PlatType } from '@/app/config/platConfig'
27 import { getCommentList, getSubCommentList } from './comment'
28 import { getHomeFeedList, homeFeedCursor } from './homeFeed'
29
30 /**
31 * 小红书平台交互类
32 */
33 class XhsPlatformInteraction implements IPlatformInteraction {
34 readonly platformType = PlatType.Xhs
35
36 /**
37 * 检查插件是否可用
38 */
39 private checkPlugin(): void {
40 if (!window.AIToEarnPlugin) {
41 throw new Error('插件未安装或未就绪')
42 }
43 }
44
45 /**
46 * 重置首页列表游标缓存
47 * 用于刷新列表时清除缓存
48 */
49 resetHomeFeedCursor(): void {
50 homeFeedCursor.reset()
51 }
52
53 /**
54 * 点赞/取消点赞作品
55 */
56 async likeWork(workId: string, isLike: boolean): Promise<LikeResult> {
57 this.checkPlugin()
58
59 if (window.AIToEarnPlugin!.unifiedInteraction) {
60 const response = await window.AIToEarnPlugin!.unifiedInteraction({
61 platform: 'xhs',
62 action: 'like',
63 workLink: `https://www.xiaohongshu.com/explore/${workId}`,
64 targetState: isLike,
65 needScreenshot: true,
66 })
67 return {
68 success: response.success,
69 message: response.message || response.error,
70 screenshot: response.screenshot,
71 rawData: response,
72 }
73 }
74
75 const path = isLike ? '/api/sns/web/v1/note/like' : '/api/sns/web/v1/note/dislike'
76
77 const response = await window.AIToEarnPlugin!.xhsRequest<XhsBaseResponse>({
78 path,
79 method: 'POST',
80 data: { note_oid: workId },
81 })
82
83 return {
84 success: response.success,
85 message: response.msg,
86 rawData: response,
87 }
88 }
89
90 /**
91 * 评论作品
92 * 一级评论使用自动化方案;二级评论保留 API 方案
93 */
94 async commentWork(params: CommentParams): Promise<CommentResult> {
95 this.checkPlugin()
96
97 // 一级评论使用 unifiedInteraction(自动化)
98 if (!params.replyToCommentId && window.AIToEarnPlugin!.unifiedInteraction) {
99 const response = await window.AIToEarnPlugin!.unifiedInteraction({
100 platform: 'xhs',
101 action: 'comment',
102 workLink: `https://www.xiaohongshu.com/explore/${params.workId}`,
103 targetState: true,
104 content: params.content,
105 needScreenshot: true,
106 })
107 return {
108 success: response.success,
109 message: response.message || response.error,
110 screenshot: response.screenshot,
111 rawData: response,
112 }
113 }
114
115 // 二级评论或老插件回退到 API 方案
116 const data: {
117 note_id: string
118 content: string
119 at_users: Array<{ user_id: string, nickname: string }>
120 target_comment_id?: string
121 } = {
122 note_id: params.workId,
123 content: params.content,
124 at_users: [],
125 }
126
127 if (params.replyToCommentId) {
128 data.target_comment_id = params.replyToCommentId
129 }
130
131 const response = await window.AIToEarnPlugin!.xhsRequest<XhsCommentResponse>({
132 path: '/api/sns/web/v1/comment/post',
133 method: 'POST',
134 data,
135 })
136
137 return {
138 success: response.success,
139 commentId: response.data?.comment?.id,
140 message: response.msg,
141 rawData: response,
142 }
143 }
144
145 /**
146 * 收藏/取消收藏作品
147 */
148 async favoriteWork(workId: string, isFavorite: boolean): Promise<FavoriteResult> {
149 this.checkPlugin()
150
151 if (window.AIToEarnPlugin!.unifiedInteraction) {
152 const response = await window.AIToEarnPlugin!.unifiedInteraction({
153 platform: 'xhs',
154 action: 'favorite',
155 workLink: `https://www.xiaohongshu.com/explore/${workId}`,
156 targetState: isFavorite,
157 needScreenshot: true,
158 })
159 return {
160 success: response.success,
161 message: response.message || response.error,
162 screenshot: response.screenshot,
163 rawData: response,
164 }
165 }
166
167 const path = isFavorite ? '/api/sns/web/v1/note/collect' : '/api/sns/web/v1/note/uncollect'
168
169 const data = isFavorite ? { note_id: workId } : { note_ids: workId }
170
171 const response = await window.AIToEarnPlugin!.xhsRequest<XhsBaseResponse>({
172 path,
173 method: 'POST',
174 data,
175 })
176
177 return {
178 success: response.success,
179 message: response.msg,
180 rawData: response,
181 }
182 }
183
184 /**
185 * 获取首页作品列表
186 * @param params 分页参数
187 */
188 async getHomeFeedList(params: HomeFeedListParams): Promise<HomeFeedListResult> {
189 return getHomeFeedList(params)
190 }
191
192 /**
193 * 获取作品详情
194 * @param params 详情请求参数
195 */
196 async getWorkDetail(_params: GetWorkDetailParams): Promise<GetWorkDetailResult> {
197 return {
198 success: false,
199 message: '小红书作品详情抓取能力已移除',
200 }
201 }
202
203 /**
204 * 获取评论列表
205 * @param params 评论列表请求参数
206 */
207 async getCommentList(params: CommentListParams): Promise<CommentListResult> {
208 return getCommentList(params)
209 }
210
211 /**
212 * 获取子评论列表(查看更多回复)
213 * @param params 子评论列表请求参数
214 */
215 async getSubCommentList(params: SubCommentListParams): Promise<CommentListResult> {
216 return getSubCommentList(params)
217 }
218 }
219
220 /**
221 * 小红书平台交互实例
222 */
223 export const xhsInteraction = new XhsPlatformInteraction()
224
225 // 导出类型(方便外部使用)
226 export type {
227 XhsCommentItem,
228 XhsCommentListResponse,
229 XhsCommentUserInfo,
230 XhsHomeFeedItem,
231 XhsHomeFeedResponse,
232 XhsSubCommentItem,
233 XhsSubCommentListResponse,
234 } from './types'
235
235 lines TYPESCRIPT