diff --git a/api/simulation.go b/api/simulation.go index 8d641d2..ee35e84 100644 --- a/api/simulation.go +++ b/api/simulation.go @@ -22,10 +22,12 @@ import ( ) func InitSimulationRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) { + authed := api.Group("/v1/simulation").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware) authed.POST("/createByProject", createByProjectId) authed.POST("/destroy/:id", destroy) authed.GET("/list", findAllSimulations) + authed.POST("/check/data", checkSimMapData) authed.POST("/train/add", addTrain) authed.POST("/train/config", configTrain) diff --git a/api/user.go b/api/user.go index d1e0080..23b2163 100644 --- a/api/user.go +++ b/api/user.go @@ -21,6 +21,7 @@ func InitUserRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) authed := api.Group("/v1/user").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware) authed.GET("/paging", pageQueryUser) authed.GET("/current", findUserInfo) + } // 用户注册 diff --git a/middleware/auth.go b/middleware/auth.go index fc74586..19cfed7 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -2,6 +2,7 @@ package middleware import ( "log/slog" + "net/http" "regexp" "strings" @@ -36,12 +37,16 @@ func permissionMiddleware() gin.HandlerFunc { return } path, method := c.Request.URL.Path, c.Request.Method + if method == http.MethodGet { + c.Next() + return + } if validateUserPath(path, method, userAuth.AuthPaths) { // 用户有权限 c.Next() return } slog.Error("无权限操作请求路径", "path", path, "method", method) - panic(sys_error.New("权限不足")) + panic(sys_error.NewCode("权限不足", 403)) } } diff --git a/starter/init.go b/starter/init.go index 49e3ef2..0281b84 100644 --- a/starter/init.go +++ b/starter/init.go @@ -86,7 +86,11 @@ func initServer() *gin.Engine { } } c.Error(be) - c.JSON(http.StatusInternalServerError, &dto.ErrorDto{ + statusCode := http.StatusInternalServerError + if be.ErrorCode > 0 { + statusCode = http.StatusForbidden + } + c.JSON(statusCode, &dto.ErrorDto{ Tip: be.UserMsg, Message: be.Error(), }) diff --git a/sys_error/error.go b/sys_error/error.go index fe89c01..5c1fc02 100644 --- a/sys_error/error.go +++ b/sys_error/error.go @@ -10,7 +10,8 @@ type BusinessError struct { // 用户提示信息 UserMsg string // 错误信息传递(用于开发回溯定位,不给用户展示) - Errors []string + Errors []string + ErrorCode int } // 新建业务错误 @@ -34,7 +35,26 @@ func New(userMsg string, errs ...error) *BusinessError { // Errors: convert(errs), } } - +func NewCode(userMsg string, errCode int, errs ...error) *BusinessError { + if len(errs) == 1 { + be, ok := errs[0].(*BusinessError) + if ok { + be.prependUserMsg(userMsg) + return be + } else { + return &BusinessError{ + UserMsg: userMsg, + ErrorCode: errCode, + Errors: []string{errs[0].Error()}, + } + } + } + return &BusinessError{ + UserMsg: userMsg, + ErrorCode: errCode, + // Errors: convert(errs), + } +} func IsBusinessError(err error) bool { _, ok := err.(*BusinessError) return ok