2e3345cdfc
修复雪花 ID 精度丢失(真 bug) - bottom-sheet 的 postId 声明为 Number,而 ID 是 18 位雪花字符串, 超出 JS 安全整数范围:339346584095428608 会被转成 ...600, 评论弹层实际查的是不存在的帖子。改为 String 记录 - 后端 DELETE /records/:id 早已就绪但前端从未调用,记错了删不掉。 时间轴支持长按删除 - 时间轴加类型筛选(体重/便便/饮食/异常/用药/疫苗/消费/照片), 后端 ?type= 本就支持 用户侧内容删除(UGC 合规:用户应能删除自己发布的内容) - 新增 DELETE /posts/:id 与 DELETE /comments/:id,仅限本人, 越权返回 40300;帖子软删除,评论删除同步递减 comment_count - 评论列表返回 is_self;社区长按自己的帖子、长按自己的评论可删 AI 聊天历史 - ai_messages 一直在写但没有查询接口,每次打开弹层都是空白。 新增 GET /api/ai/messages,弹层打开时接在开场白后加载 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
191 lines
4.4 KiB
Go
191 lines
4.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/sundynix/pets-be/internal/middleware"
|
|
"github.com/sundynix/pets-be/internal/service"
|
|
"github.com/sundynix/pets-be/pkg/response"
|
|
)
|
|
|
|
// ListArticles GET /api/articles
|
|
func (h *Handler) ListArticles(c *gin.Context) {
|
|
articles, err := h.svc.ListArticles()
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, articles)
|
|
}
|
|
|
|
// GetArticle GET /api/articles/:id
|
|
func (h *Handler) GetArticle(c *gin.Context) {
|
|
article, err := h.svc.GetArticle(idParam(c, "id"))
|
|
if err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, article)
|
|
}
|
|
|
|
// GetPro GET /api/pro
|
|
func (h *Handler) GetPro(c *gin.Context) {
|
|
info, err := h.svc.GetPro(middleware.UserID(c))
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, info)
|
|
}
|
|
|
|
// ActivatePro POST /api/pro/activate
|
|
func (h *Handler) ActivatePro(c *gin.Context) {
|
|
info, err := h.svc.ActivatePro(middleware.UserID(c))
|
|
if err != nil {
|
|
// 支付未接入不是服务器错误,按参数错返回,前端直接展示提示文案
|
|
if errors.Is(err, service.ErrPayNotReady) {
|
|
response.FailParams(c, err.Error())
|
|
return
|
|
}
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, info)
|
|
}
|
|
|
|
type aiChatReq struct {
|
|
PetID *string `json:"pet_id"`
|
|
Session string `json:"session"`
|
|
Text string `json:"text"`
|
|
}
|
|
|
|
// AIChat POST /api/ai/chat
|
|
func (h *Handler) AIChat(c *gin.Context) {
|
|
var req aiChatReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailParams(c, err.Error())
|
|
return
|
|
}
|
|
if req.Text == "" {
|
|
response.FailParams(c, "问题不能为空")
|
|
return
|
|
}
|
|
reply, err := h.svc.AIChat(middleware.UserID(c), req.PetID, req.Session, req.Text)
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"reply": reply})
|
|
}
|
|
|
|
// HomeSummary GET /api/pets/:id/home-summary
|
|
func (h *Handler) HomeSummary(c *gin.Context) {
|
|
res, err := h.svc.GetHomeSummary(middleware.UserID(c), idParam(c, "id"))
|
|
if err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, res)
|
|
}
|
|
|
|
// UserSummary GET /api/user/summary
|
|
func (h *Handler) UserSummary(c *gin.Context) {
|
|
res, err := h.svc.GetUserSummary(middleware.UserID(c))
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, res)
|
|
}
|
|
|
|
type assessSymptomReq struct {
|
|
Symptoms []string `json:"symptoms"`
|
|
Duration string `json:"duration"`
|
|
Spirit string `json:"spirit"`
|
|
}
|
|
|
|
// AssessSymptom POST /api/pets/:id/ai/assess-symptom
|
|
func (h *Handler) AssessSymptom(c *gin.Context) {
|
|
var req assessSymptomReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailParams(c, err.Error())
|
|
return
|
|
}
|
|
res, err := h.svc.AssessSymptom(middleware.UserID(c), idParam(c, "id"), service.SymptomInput{
|
|
Symptoms: req.Symptoms, Duration: req.Duration, Spirit: req.Spirit,
|
|
})
|
|
if err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, res)
|
|
}
|
|
|
|
type feedbackReq struct {
|
|
Content string `json:"content"`
|
|
Contact string `json:"contact"`
|
|
}
|
|
|
|
// CreateFeedback POST /api/feedback
|
|
func (h *Handler) CreateFeedback(c *gin.Context) {
|
|
var req feedbackReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailParams(c, err.Error())
|
|
return
|
|
}
|
|
f, err := h.svc.CreateFeedback(middleware.UserID(c), req.Content, req.Contact)
|
|
if err != nil {
|
|
response.FailParams(c, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, f)
|
|
}
|
|
|
|
// AdminListFeedback GET /api/admin/feedback
|
|
func (h *Handler) AdminListFeedback(c *gin.Context) {
|
|
var req adminListReq
|
|
_ = c.ShouldBindQuery(&req)
|
|
req.Normalize()
|
|
list, total, err := h.svc.ListFeedbackAdmin(req.Offset(), req.Limit())
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, response.NewPage(list, total, req.PageQuery))
|
|
}
|
|
|
|
type handledReq struct {
|
|
Handled bool `json:"handled"`
|
|
}
|
|
|
|
// AdminSetFeedbackHandled PUT /api/admin/feedback/:id/handled
|
|
func (h *Handler) AdminSetFeedbackHandled(c *gin.Context) {
|
|
var req handledReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.FailParams(c, err.Error())
|
|
return
|
|
}
|
|
if err := h.svc.SetFeedbackHandled(idParam(c, "id"), req.Handled); err != nil {
|
|
respondErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"ok": true})
|
|
}
|
|
|
|
// ListAIMessages GET /api/ai/messages?session=&limit=
|
|
func (h *Handler) ListAIMessages(c *gin.Context) {
|
|
limit := 0
|
|
if v := c.Query("limit"); v != "" {
|
|
limit, _ = strconv.Atoi(v)
|
|
}
|
|
msgs, err := h.svc.ListAIMessages(middleware.UserID(c), c.Query("session"), limit)
|
|
if err != nil {
|
|
response.FailErr(c, err)
|
|
return
|
|
}
|
|
response.OK(c, msgs)
|
|
}
|