落地 synastry/star/ask API 与 H5 页面,补齐 cece-frontend-re complete-design 证据文档,并加入 Android 模拟器截图抓取脚本。 Co-authored-by: Cursor <cursoragent@cursor.com>
264 lines
7.4 KiB
Go
264 lines
7.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/service/report"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
// ReportHandler exposes portrait reports and commerce mock.
|
|
type ReportHandler struct {
|
|
Svc *report.Service
|
|
}
|
|
|
|
// Register mounts report/commerce routes.
|
|
func (h *ReportHandler) Register(rg *gin.RouterGroup) {
|
|
rg.POST("/reports/portrait", h.CreatePortrait)
|
|
rg.POST("/reports/star", h.CreateStar)
|
|
rg.POST("/reports/synastry", h.CreateSynastry)
|
|
rg.POST("/reports/rhythm", h.CreateRhythm)
|
|
rg.GET("/reports", h.List)
|
|
rg.GET("/reports/:id", h.Get)
|
|
rg.GET("/membership/me", h.GetMembership)
|
|
rg.POST("/orders", h.CreateOrder)
|
|
rg.POST("/orders/:id/pay-mock", h.PayMock)
|
|
}
|
|
|
|
// CreatePortrait handles POST /reports/portrait.
|
|
func (h *ReportHandler) CreatePortrait(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
ProfileID string `json:"profile_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
pid, err := uuid.Parse(req.ProfileID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id")
|
|
return
|
|
}
|
|
rep, err := h.Svc.CreatePortrait(c.Request.Context(), userID, pid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30002, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// CreateStar handles POST /reports/star (星象性格).
|
|
func (h *ReportHandler) CreateStar(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
ProfileID string `json:"profile_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
pid, err := uuid.Parse(req.ProfileID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id")
|
|
return
|
|
}
|
|
rep, err := h.Svc.CreateStar(c.Request.Context(), userID, pid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30002, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// CreateSynastry handles POST /reports/synastry (五主盘 + 推运合盘).
|
|
func (h *ReportHandler) CreateSynastry(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
ProfileIDA string `json:"profile_id_a" binding:"required"`
|
|
ProfileIDB string `json:"profile_id_b" binding:"required"`
|
|
AsOf *string `json:"as_of"` // YYYY-MM-DD optional
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
aid, err := uuid.Parse(req.ProfileIDA)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id_a")
|
|
return
|
|
}
|
|
bid, err := uuid.Parse(req.ProfileIDB)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id_b")
|
|
return
|
|
}
|
|
if aid == bid {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "需要两个不同档案")
|
|
return
|
|
}
|
|
var asOfPtr *time.Time
|
|
if req.AsOf != nil && *req.AsOf != "" {
|
|
t, err := time.ParseInLocation("2006-01-02", *req.AsOf, time.FixedZone("CST", 8*3600))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid as_of")
|
|
return
|
|
}
|
|
asOfPtr = &t
|
|
}
|
|
rep, err := h.Svc.CreateSynastry(c.Request.Context(), userID, aid, bid, asOfPtr)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30002, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// CreateRhythm handles POST /reports/rhythm (身心节律) — wired when service ready.
|
|
func (h *ReportHandler) CreateRhythm(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
ProfileID string `json:"profile_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
pid, err := uuid.Parse(req.ProfileID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid profile_id")
|
|
return
|
|
}
|
|
rep, err := h.Svc.CreateRhythm(c.Request.Context(), userID, pid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30002, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// List handles GET /reports.
|
|
func (h *ReportHandler) List(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
items, err := h.Svc.List(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
// Get handles GET /reports/:id.
|
|
func (h *ReportHandler) Get(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
rid, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid id")
|
|
return
|
|
}
|
|
rep, err := h.Svc.Get(c.Request.Context(), userID, rid)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusNotFound, 40401, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, rep)
|
|
}
|
|
|
|
// GetMembership handles GET /membership/me.
|
|
func (h *ReportHandler) GetMembership(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
me, err := h.Svc.GetMembership(c.Request.Context(), userID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50000, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, me)
|
|
}
|
|
|
|
// CreateOrder handles POST /orders.
|
|
func (h *ReportHandler) CreateOrder(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
var req struct {
|
|
Kind string `json:"kind" binding:"required"`
|
|
Plan string `json:"plan"`
|
|
ReportID *string `json:"report_id"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid request")
|
|
return
|
|
}
|
|
var rid *uuid.UUID
|
|
if req.ReportID != nil && *req.ReportID != "" {
|
|
id, err := uuid.Parse(*req.ReportID)
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid report_id")
|
|
return
|
|
}
|
|
rid = &id
|
|
}
|
|
oid, err := h.Svc.CreateOrder(c.Request.Context(), userID, report.CreateOrderInput{
|
|
Kind: req.Kind, Plan: req.Plan, ReportID: rid,
|
|
})
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30003, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"order_id": oid.String()})
|
|
}
|
|
|
|
// PayMock handles POST /orders/:id/pay-mock.
|
|
func (h *ReportHandler) PayMock(c *gin.Context) {
|
|
userID, ok := middleware.UserIDFromContext(c)
|
|
if !ok {
|
|
response.Fail(c, http.StatusUnauthorized, 40100, "unauthorized")
|
|
return
|
|
}
|
|
oid, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 10000, "invalid id")
|
|
return
|
|
}
|
|
if err := h.Svc.PayMock(c.Request.Context(), userID, oid); err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 30004, err.Error())
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"paid": true})
|
|
}
|