Files
sundynix-pets/pets-be/internal/router/router.go
T
Blizzard ef172f4e4a feat: 宠物加性格/过敏/备注字段 + 身份卡照护信息 + 微信取手机号
- Pet 增 personality/allergy/notes;建档表单加「性格标签(多选)/过敏/备注」
- IDCard 增 color/personality/owner/phone(脱敏)/allergy/notes/疫苗驱虫状态/发证机构
- 微信取手机号:POST /api/user/phone 用 getPhoneNumber 的 code 换手机号存库
  (BindPhoneByCode 走 getuserphonenumber,复用 access_token)

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 17:12:56 +08:00

192 lines
6.3 KiB
Go

package router
import (
"github.com/gin-gonic/gin"
"github.com/sundynix/pets-be/internal/admin"
"github.com/sundynix/pets-be/internal/handler"
"github.com/sundynix/pets-be/internal/middleware"
appjwt "github.com/sundynix/pets-be/pkg/jwt"
"github.com/sundynix/pets-be/pkg/response"
)
// New 组装 gin 引擎与所有路由
func New(h *handler.Handler, jm *appjwt.Manager, mode string) *gin.Engine {
gin.SetMode(mode)
r := gin.New()
r.Use(gin.Logger(), gin.Recovery(), middleware.CORS())
// 健康检查
r.GET("/api/ping", func(c *gin.Context) { response.OK(c, gin.H{"pong": true}) })
// 根路径跳转后台
r.GET("/", func(c *gin.Context) { c.Redirect(302, "/admin/") })
// 微信图片异步安全检测的结果回调(消息推送),公开、自带签名校验
r.Any("/api/wx/sec-callback", h.WxSecCallback)
api := r.Group("/api")
registerAuth(api, h)
registerUserAPI(api, h, jm)
registerAdminAPI(api, h, jm)
// 内嵌后台 SPA
admin.Register(r)
return r
}
func registerAuth(api *gin.RouterGroup, h *handler.Handler) {
auth := api.Group("/auth")
auth.POST("/login", h.Login)
auth.POST("/wechat", h.WechatLogin)
// 刷新/退出走刷新令牌,不需要(也不可能带)有效的 access token
auth.POST("/refresh", h.RefreshToken)
auth.POST("/logout", h.Logout)
}
// 小程序用户接口(需 user JWT)
func registerUserAPI(api *gin.RouterGroup, h *handler.Handler, jm *appjwt.Manager) {
g := api.Group("")
g.Use(middleware.AuthUser(jm))
g.GET("/user/profile", h.Profile)
g.PUT("/user/profile", h.UpdateProfile)
g.POST("/user/phone", h.BindPhone)
g.GET("/user/summary", h.UserSummary)
g.PUT("/user/decoration", h.UpdateDecoration)
g.GET("/pets", h.ListPets)
g.POST("/pets", h.CreatePet)
g.POST("/onboarding", h.Onboarding)
g.GET("/pets/:id", h.GetPet)
g.PUT("/pets/:id", h.UpdatePet)
g.DELETE("/pets/:id", h.DeletePet)
g.GET("/record-types", h.RecordTypes)
g.GET("/breeds", h.Breeds)
g.GET("/life-stages", h.LifeStages)
g.GET("/pets/:id/records", h.ListRecords)
g.POST("/pets/:id/records", h.CreateRecord)
g.GET("/pets/:id/records/weight-trend", h.WeightTrend)
g.GET("/pets/:id/insights", h.PetInsights)
g.DELETE("/records/:id", h.DeleteRecord)
g.GET("/pets/:id/tasks", h.ListTasks)
g.POST("/pets/:id/tasks/complete-all", h.CompleteAllTasks)
g.POST("/tasks/:id/toggle", h.ToggleTask)
g.GET("/pets/:id/home-summary", h.HomeSummary)
g.GET("/pets/:id/plan", h.GetPlan)
g.GET("/pets/:id/plan/calendar", h.PlanCalendar)
g.GET("/pets/:id/stage-drift", h.PetStageDrift)
g.GET("/pets/:id/id-card", h.PetIDCard)
g.GET("/pets/:id/plan/month-status", h.PlanMonthStatus)
// 养护方案:列出可选的、套用一套到某个月
g.GET("/care-templates", h.CareTemplateOptions)
g.POST("/pets/:id/apply-template", h.ApplyTemplate)
g.GET("/pets/:id/day-plan", h.DayPlan)
g.POST("/pets/:id/ai-plan", h.CreateAIPlan)
g.POST("/ai-plan/:id/apply", h.ApplyAIPlan)
g.POST("/plan-tasks/:id/toggle", h.TogglePlanTask)
// 用户自己管理计划节点(原来只能勾选内置模板生成的那些)
g.POST("/pets/:id/plan-tasks", h.AddPlanTask)
g.PUT("/plan-tasks/:id", h.UpdatePlanTask)
g.DELETE("/plan-tasks/:id", h.DeletePlanTask)
g.GET("/pets/:id/reminders", h.ListReminders)
g.POST("/pets/:id/reminders", h.CreateReminder)
g.PUT("/reminders/:id", h.UpdateReminder)
g.DELETE("/reminders/:id", h.DeleteReminder)
g.GET("/pets/:id/report/weekly", h.WeeklyReport)
g.GET("/pets/:id/bill", h.Bill)
g.GET("/pets/:id/health-summary", h.HealthSummary)
g.GET("/pets/:id/poster", h.Poster)
g.GET("/users/:id/profile", h.UserCard)
g.GET("/users/:id/posts", h.UserPosts)
g.GET("/users/:id/relations", h.Relations)
g.POST("/users/:id/follow", h.FollowUser)
g.DELETE("/users/:id/follow", h.UnfollowUser)
g.GET("/posts", h.ListPosts)
g.POST("/posts", h.CreatePost)
g.GET("/posts/:id", h.GetPost)
g.POST("/posts/:id/like", h.LikePost)
g.DELETE("/posts/:id/like", h.UnlikePost)
g.GET("/posts/:id/comments", h.ListComments)
g.POST("/posts/:id/comments", h.CreateComment)
g.DELETE("/posts/:id", h.DeleteOwnPost)
g.DELETE("/comments/:id", h.DeleteOwnComment)
g.GET("/comments/:id/replies", h.ListReplies)
g.GET("/articles", h.ListArticles)
g.POST("/feedback", h.CreateFeedback)
g.GET("/articles/:id", h.GetArticle)
g.GET("/pro", h.GetPro)
g.POST("/pro/activate", h.ActivatePro)
g.POST("/ai/chat", h.AIChat)
g.GET("/ai/messages", h.ListAIMessages)
g.GET("/ai/quota", h.AIQuota)
g.POST("/pets/:id/ai/assess-symptom", h.AssessSymptom)
g.POST("/upload", h.Upload)
}
// 后台接口(登录开放,其余需 admin JWT)
func registerAdminAPI(api *gin.RouterGroup, h *handler.Handler, jm *appjwt.Manager) {
api.POST("/admin/login", h.AdminLogin)
api.POST("/admin/refresh", h.AdminRefresh)
api.POST("/admin/logout", h.AdminLogout)
g := api.Group("/admin")
g.Use(middleware.AuthAdmin(jm))
g.GET("/me", h.AdminMe)
g.GET("/stats", h.AdminStats)
g.GET("/analytics", h.AdminAnalytics)
g.GET("/users", h.AdminListUsers)
g.PUT("/users/:id/disabled", h.AdminSetUserDisabled)
g.GET("/pets", h.AdminListPets)
g.GET("/posts", h.AdminListPosts)
g.PUT("/posts/:id/status", h.AdminSetPostStatus)
g.GET("/comments", h.AdminListComments)
g.PUT("/comments/:id/status", h.AdminSetCommentStatus)
g.DELETE("/comments/:id", h.AdminDeleteComment)
g.GET("/articles", h.AdminListArticles)
g.POST("/articles", h.AdminSaveArticle)
g.DELETE("/articles/:id", h.AdminDeleteArticle)
g.GET("/breeds", h.AdminListBreeds)
g.POST("/breeds", h.AdminSaveBreed)
g.DELETE("/breeds/:id", h.AdminDeleteBreed)
g.GET("/record-types", h.AdminListRecordTypes)
g.POST("/record-types", h.AdminSaveRecordType)
g.DELETE("/record-types/:id", h.AdminDeleteRecordType)
g.GET("/care-templates/meta", h.AdminCareTemplateMeta)
g.GET("/care-templates", h.AdminListCareTemplates)
g.GET("/care-templates/:id", h.AdminGetCareTemplate)
g.POST("/care-templates", h.AdminSaveCareTemplate)
g.DELETE("/care-templates/:id", h.AdminDeleteCareTemplate)
g.POST("/care-templates/generate", h.AdminGenerateCareTemplate)
g.GET("/ai-quota", h.AdminGetAIQuota)
g.PUT("/ai-quota", h.AdminSaveAIQuota)
g.GET("/community-bot", h.AdminGetCommunityBot)
g.PUT("/community-bot", h.AdminSaveCommunityBot)
g.POST("/community-bot/generate", h.AdminGenerateCommunityPosts)
g.GET("/feedback", h.AdminListFeedback)
g.PUT("/feedback/:id/handled", h.AdminSetFeedbackHandled)
g.GET("/memberships", h.AdminListPro)
}