-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathhandlers_api.go
More file actions
295 lines (250 loc) · 8.62 KB
/
handlers_api.go
File metadata and controls
295 lines (250 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"net/http"
"github.com/xpzouying/xiaohongshu-mcp/cookies"
"github.com/xpzouying/xiaohongshu-mcp/xiaohongshu"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
// respondError 返回错误响应
func respondError(c *gin.Context, statusCode int, code, message string, details any) {
response := ErrorResponse{
Error: message,
Code: code,
Details: details,
}
logrus.Errorf("%s %s %s %d", c.Request.Method, c.Request.URL.Path,
c.GetString("account"), statusCode)
c.JSON(statusCode, response)
}
// respondSuccess 返回成功响应
func respondSuccess(c *gin.Context, data any, message string) {
response := SuccessResponse{
Success: true,
Data: data,
Message: message,
}
logrus.Infof("%s %s %s %d", c.Request.Method, c.Request.URL.Path,
c.GetString("account"), http.StatusOK)
c.JSON(http.StatusOK, response)
}
// checkLoginStatusHandler 检查登录状态
func (s *AppServer) checkLoginStatusHandler(c *gin.Context) {
status, err := s.xiaohongshuService.CheckLoginStatus(c.Request.Context())
if err != nil {
respondError(c, http.StatusInternalServerError, "STATUS_CHECK_FAILED",
"检查登录状态失败", err.Error())
return
}
c.Set("account", "ai-report")
respondSuccess(c, status, "检查登录状态成功")
}
// getLoginQrcodeHandler 处理 [GET /api/login/qrcode] 请求。
// 用于生成并返回登录二维码(Base64 图片 + 超时时间),供前端展示给用户扫码登录。
func (s *AppServer) getLoginQrcodeHandler(c *gin.Context) {
result, err := s.xiaohongshuService.GetLoginQrcode(c.Request.Context())
if err != nil {
respondError(c, http.StatusInternalServerError, "STATUS_CHECK_FAILED",
"获取登录二维码失败", err.Error())
return
}
respondSuccess(c, result, "获取登录二维码成功")
}
// deleteCookiesHandler 删除 cookies,重置登录状态
func (s *AppServer) deleteCookiesHandler(c *gin.Context) {
err := s.xiaohongshuService.DeleteCookies(c.Request.Context())
if err != nil {
respondError(c, http.StatusInternalServerError, "DELETE_COOKIES_FAILED",
"删除 cookies 失败", err.Error())
return
}
cookiePath := cookies.GetCookiesFilePath()
respondSuccess(c, map[string]interface{}{
"cookie_path": cookiePath,
"message": "Cookies 已成功删除,登录状态已重置。下次操作时需要重新登录。",
}, "删除 cookies 成功")
}
// publishHandler 发布内容
func (s *AppServer) publishHandler(c *gin.Context) {
var req PublishRequest
if err := c.ShouldBindJSON(&req); err != nil {
respondError(c, http.StatusBadRequest, "INVALID_REQUEST",
"请求参数错误", err.Error())
return
}
// 执行发布
result, err := s.xiaohongshuService.PublishContent(c.Request.Context(), &req)
if err != nil {
respondError(c, http.StatusInternalServerError, "PUBLISH_FAILED",
"发布失败", err.Error())
return
}
respondSuccess(c, result, "发布成功")
}
// publishVideoHandler 发布视频内容
func (s *AppServer) publishVideoHandler(c *gin.Context) {
var req PublishVideoRequest
if err := c.ShouldBindJSON(&req); err != nil {
respondError(c, http.StatusBadRequest, "INVALID_REQUEST",
"请求参数错误", err.Error())
return
}
// 执行视频发布
result, err := s.xiaohongshuService.PublishVideo(c.Request.Context(), &req)
if err != nil {
respondError(c, http.StatusInternalServerError, "PUBLISH_VIDEO_FAILED",
"视频发布失败", err.Error())
return
}
respondSuccess(c, result, "视频发布成功")
}
// listFeedsHandler 获取Feeds列表
func (s *AppServer) listFeedsHandler(c *gin.Context) {
// 获取 Feeds 列表
result, err := s.xiaohongshuService.ListFeeds(c.Request.Context())
if err != nil {
respondError(c, http.StatusInternalServerError, "LIST_FEEDS_FAILED",
"获取Feeds列表失败", err.Error())
return
}
c.Set("account", "ai-report")
respondSuccess(c, result, "获取Feeds列表成功")
}
// searchFeedsHandler 搜索Feeds
func (s *AppServer) searchFeedsHandler(c *gin.Context) {
var keyword string
var filters xiaohongshu.FilterOption
switch c.Request.Method {
case http.MethodPost:
// 对于POST请求,从JSON中获取keyword
var searchReq SearchFeedsRequest
if err := c.ShouldBindJSON(&searchReq); err != nil {
respondError(c, http.StatusBadRequest, "INVALID_REQUEST",
"请求参数错误", err.Error())
return
}
keyword = searchReq.Keyword
filters = searchReq.Filters
default:
keyword = c.Query("keyword")
}
if keyword == "" {
respondError(c, http.StatusBadRequest, "MISSING_KEYWORD",
"缺少关键词参数", "keyword parameter is required")
return
}
// 搜索 Feeds
result, err := s.xiaohongshuService.SearchFeeds(c.Request.Context(), keyword, filters)
if err != nil {
respondError(c, http.StatusInternalServerError, "SEARCH_FEEDS_FAILED",
"搜索Feeds失败", err.Error())
return
}
c.Set("account", "ai-report")
respondSuccess(c, result, "搜索Feeds成功")
}
// getFeedDetailHandler 获取Feed详情
func (s *AppServer) getFeedDetailHandler(c *gin.Context) {
var req FeedDetailRequest
if err := c.ShouldBindJSON(&req); err != nil {
respondError(c, http.StatusBadRequest, "INVALID_REQUEST",
"请求参数错误", err.Error())
return
}
var result *FeedDetailResponse
var err error
if req.CommentConfig != nil {
// 使用配置参数
config := xiaohongshu.CommentLoadConfig{
ClickMoreReplies: req.CommentConfig.ClickMoreReplies,
MaxRepliesThreshold: req.CommentConfig.MaxRepliesThreshold,
MaxCommentItems: req.CommentConfig.MaxCommentItems,
ScrollSpeed: req.CommentConfig.ScrollSpeed,
}
result, err = s.xiaohongshuService.GetFeedDetailWithConfig(c.Request.Context(), req.FeedID, req.XsecToken, req.LoadAllComments, config)
} else {
// 使用默认配置
result, err = s.xiaohongshuService.GetFeedDetail(c.Request.Context(), req.FeedID, req.XsecToken, req.LoadAllComments)
}
if err != nil {
respondError(c, http.StatusInternalServerError, "GET_FEED_DETAIL_FAILED",
"获取Feed详情失败", err.Error())
return
}
c.Set("account", "ai-report")
respondSuccess(c, result, "获取Feed详情成功")
}
// userProfileHandler 用户主页
func (s *AppServer) userProfileHandler(c *gin.Context) {
var req UserProfileRequest
if err := c.ShouldBindJSON(&req); err != nil {
respondError(c, http.StatusBadRequest, "INVALID_REQUEST",
"请求参数错误", err.Error())
return
}
// 获取用户信息
result, err := s.xiaohongshuService.UserProfile(c.Request.Context(), req.UserID, req.XsecToken)
if err != nil {
respondError(c, http.StatusInternalServerError, "GET_USER_PROFILE_FAILED",
"获取用户主页失败", err.Error())
return
}
c.Set("account", "ai-report")
respondSuccess(c, map[string]any{"data": result}, "result.Message")
}
// postCommentHandler 发表评论到Feed
func (s *AppServer) postCommentHandler(c *gin.Context) {
var req PostCommentRequest
if err := c.ShouldBindJSON(&req); err != nil {
respondError(c, http.StatusBadRequest, "INVALID_REQUEST",
"请求参数错误", err.Error())
return
}
// 发表评论
result, err := s.xiaohongshuService.PostCommentToFeed(c.Request.Context(), req.FeedID, req.XsecToken, req.Content)
if err != nil {
respondError(c, http.StatusInternalServerError, "POST_COMMENT_FAILED",
"发表评论失败", err.Error())
return
}
c.Set("account", "ai-report")
respondSuccess(c, result, result.Message)
}
// replyCommentHandler 回复指定评论
func (s *AppServer) replyCommentHandler(c *gin.Context) {
var req ReplyCommentRequest
if err := c.ShouldBindJSON(&req); err != nil {
respondError(c, http.StatusBadRequest, "INVALID_REQUEST",
"请求参数错误", err.Error())
return
}
result, err := s.xiaohongshuService.ReplyCommentToFeed(c.Request.Context(), req.FeedID, req.XsecToken, req.CommentID, req.UserID, req.Content)
if err != nil {
respondError(c, http.StatusInternalServerError, "REPLY_COMMENT_FAILED",
"回复评论失败", err.Error())
return
}
c.Set("account", "ai-report")
respondSuccess(c, result, result.Message)
}
// healthHandler 健康检查
func healthHandler(c *gin.Context) {
respondSuccess(c, map[string]any{
"status": "healthy",
"service": "xiaohongshu-mcp",
"account": "ai-report",
"timestamp": "now",
}, "服务正常")
}
// myProfileHandler 我的信息
func (s *AppServer) myProfileHandler(c *gin.Context) {
// 获取当前登录用户信息
result, err := s.xiaohongshuService.GetMyProfile(c.Request.Context())
if err != nil {
respondError(c, http.StatusInternalServerError, "GET_MY_PROFILE_FAILED",
"获取我的主页失败", err.Error())
return
}
c.Set("account", "ai-report")
respondSuccess(c, map[string]any{"data": result}, "获取我的主页成功")
}