Version: 0.9.66.dev.260504
后端: 1. 阶段 2 user/auth 服务边界落地,新增 `cmd/userauth` go-zero zrpc 服务、`services/userauth` 核心实现、gateway user API/zrpc client 与 shared contracts/ports,迁移注册、登录、刷新 token、登出、JWT、黑名单和 token 额度治理 2. gateway 与启动装配切流,`cmd/all` 只保留边缘路由、鉴权和轻量组合,通过 userauth zrpc 访问核心用户能力;拆分 MySQL/Redis 初始化与 AutoMigrate 边界,`userauth` 自迁 `users` 和 token 记账幂等表,`all` 不再迁用户表 3. 清退 Gin 单体旧 user/auth DAO、model、service、router、middleware 和 JWT handler,并同步调整 agent/schedule/cache/outbox 相关调用依赖 4. 补齐 refresh token 防并发重放、MySQL 幂等 token 记账、额度 `>=` 拦截和 RPC 错误映射,避免重复记账与内部错误透出 文档: 1. 新增《学习计划论坛与Token商店PRD》
This commit is contained in:
@@ -5,6 +5,7 @@ package respond
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -24,6 +25,26 @@ func (r Response) Error() string { // 实现 error 接口
|
||||
return r.Info
|
||||
}
|
||||
|
||||
// HTTPStatus 负责把项目内部响应码映射到 HTTP 状态码。
|
||||
//
|
||||
// 职责边界:
|
||||
// 1. 只根据当前响应码判断该返回 200/400/401/500 里的哪一类;
|
||||
// 2. 不负责写响应体,也不负责区分具体业务来源;
|
||||
// 3. 业务侧可以继续透传 Status/Info,HTTP 层只需要调用这个方法。
|
||||
func (r Response) HTTPStatus() int {
|
||||
switch r.Status {
|
||||
case MissingToken.Status, InvalidToken.Status, InvalidClaims.Status, InvalidRefreshToken.Status,
|
||||
WrongTokenType.Status, UserLoggedOut.Status, ErrUnauthorized.Status:
|
||||
return http.StatusUnauthorized
|
||||
case UserTasksEmpty.Status, NoOngoingOrUpcomingSchedule.Status, TaskAlreadyDeleted.Status:
|
||||
return http.StatusOK
|
||||
}
|
||||
if strings.HasPrefix(strings.TrimSpace(r.Status), "5") {
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
return http.StatusBadRequest
|
||||
}
|
||||
|
||||
func RespWithData(response Response, data interface{}) FinalResponse { //传入一个响应结构体和数据,返回一个最终响应结构体
|
||||
var finalResponse FinalResponse
|
||||
finalResponse.Status = response.Status
|
||||
@@ -42,7 +63,7 @@ func DealWithError(c *gin.Context, err error) { //处理错误,返回对应的
|
||||
return
|
||||
}
|
||||
if errors.As(err, &resp) {
|
||||
c.JSON(http.StatusBadRequest, resp)
|
||||
c.JSON(resp.HTTPStatus(), resp)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, InternalError(err))
|
||||
|
||||
Reference in New Issue
Block a user