后端: 1. 阶段 4 active-scheduler 服务边界落地,新增 `cmd/active-scheduler`、`services/active_scheduler`、`shared/contracts/activescheduler` 和 active-scheduler port,迁移 dry-run、trigger、preview、confirm zrpc 能力 2. active-scheduler outbox consumer、relay、retry loop 和 due job scanner 迁入独立服务入口,gateway `/active-schedule/*` 改为通过 zrpc client 调用 3. gateway 目录收口为 `gateway/api` + `gateway/client`,统一归档 userauth、notification、active-scheduler 的 HTTP 门面和 zrpc client 4. 将旧 `backend/active_scheduler` 领域核心下沉到 `services/active_scheduler/core`,清退旧根目录活跃实现,并补充 active-scheduler 启动期跨域依赖表检查 5. 调整单体启动与 outbox 归属,`cmd/all` 不再启动 active-scheduler workflow、scanner 或 handler 文档: 1. 更新微服务迁移计划,将阶段 4 active-scheduler 标记为首轮收口完成,并明确下一阶段进入 schedule / task / course / task-class
99 lines
2.6 KiB
Go
99 lines
2.6 KiB
Go
package userauthapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
gatewaymiddleware "github.com/LoveLosita/smartflow/backend/gateway/middleware"
|
|
"github.com/LoveLosita/smartflow/backend/respond"
|
|
contracts "github.com/LoveLosita/smartflow/backend/shared/contracts/userauth"
|
|
"github.com/LoveLosita/smartflow/backend/shared/ports"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UserHandler struct {
|
|
client ports.UserCommandClient
|
|
}
|
|
|
|
// NewUserHandler 只接收 user/auth 客户端,不再直接依赖本地 user service。
|
|
func NewUserHandler(client ports.UserCommandClient) *UserHandler {
|
|
return &UserHandler{client: client}
|
|
}
|
|
|
|
func (api *UserHandler) UserRegister(c *gin.Context) {
|
|
var req contracts.RegisterRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
|
defer cancel()
|
|
|
|
retUser, err := api.client.Register(ctx, req)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, retUser))
|
|
}
|
|
|
|
func (api *UserHandler) UserLogin(c *gin.Context) {
|
|
var req contracts.LoginRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
|
defer cancel()
|
|
|
|
tokens, err := api.client.Login(ctx, req)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, tokens))
|
|
}
|
|
|
|
func (api *UserHandler) RefreshTokenHandler(c *gin.Context) {
|
|
var req contracts.RefreshTokenRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, respond.WrongParamType)
|
|
return
|
|
}
|
|
if strings.TrimSpace(req.RefreshToken) == "" {
|
|
c.JSON(http.StatusBadRequest, respond.MissingParam)
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
|
defer cancel()
|
|
|
|
tokens, err := api.client.RefreshToken(ctx, req)
|
|
if err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.RespWithData(respond.Ok, tokens))
|
|
}
|
|
|
|
func (api *UserHandler) UserLogout(c *gin.Context) {
|
|
token := gatewaymiddleware.ExtractTokenFromAuthorization(c.GetHeader("Authorization"))
|
|
if token == "" {
|
|
c.JSON(http.StatusUnauthorized, respond.MissingToken)
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
|
defer cancel()
|
|
|
|
if err := api.client.Logout(ctx, token); err != nil {
|
|
respond.DealWithError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, respond.Ok)
|
|
}
|