first commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// Package app1hdl 是小程序 app1 的 HTTP 接入层。
|
||||
package app1hdl
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"portal-mini-go/internal/middleware"
|
||||
"portal-mini-go/internal/pkg/errors"
|
||||
"portal-mini-go/internal/pkg/response"
|
||||
app1svc "portal-mini-go/internal/service/miniapp/app1"
|
||||
)
|
||||
|
||||
// Handler app1 请求处理。
|
||||
type Handler struct {
|
||||
svc *app1svc.Service
|
||||
}
|
||||
|
||||
func NewHandler(svc *app1svc.Service) *Handler { return &Handler{svc: svc} }
|
||||
|
||||
type LoginReq struct {
|
||||
Code string `json:"code" binding:"required"`
|
||||
}
|
||||
|
||||
// Login 处理小程序登录请求。app_id 由 MiniAppAuth 中间件从请求头校验并注入。
|
||||
func (h *Handler) Login(c *gin.Context) {
|
||||
var req LoginReq
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Error(errors.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
appID := middleware.AppID(c)
|
||||
token, u, err := h.svc.Login(c.Request.Context(), appID, req.Code)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
response.OK(c, gin.H{"token": token, "user": u})
|
||||
}
|
||||
|
||||
// Register 注册 app1 的路由。g 已带 /api/ma 前缀与 MiniAppAuth 中间件(校验请求头 app_id)。
|
||||
func Register(g *gin.RouterGroup, h *Handler) {
|
||||
g.POST("/login", h.Login)
|
||||
}
|
||||
Reference in New Issue
Block a user