update gin handle

This commit is contained in:
lzh
2025-07-10 11:02:19 +08:00
parent b598b590e7
commit 6aac70de9a
11 changed files with 108 additions and 91 deletions

View File

@@ -1,7 +1,6 @@
package api
import (
"net/http"
"reflect"
"github.com/lzh-1625/go_process_manager/internal/app/constants"
@@ -10,17 +9,6 @@ import (
"github.com/gin-gonic/gin"
)
func rOk(ctx *gin.Context, message string, data any) {
jsonData := map[string]any{
"code": 0,
"msg": message,
}
if data != nil {
jsonData["data"] = data
}
ctx.JSON(http.StatusOK, jsonData)
}
func getRole(ctx *gin.Context) constants.Role {
if v, ok := ctx.Get(constants.CTXFLG_ROLE); ok {
return v.(constants.Role)
@@ -39,3 +27,40 @@ func isAdmin(ctx *gin.Context) bool {
func hasOprPermission(ctx *gin.Context, uuid int, op constants.OprPermission) bool {
return isAdmin(ctx) || reflect.ValueOf(repository.PermissionRepository.GetPermission(getUserName(ctx), uuid)).FieldByName(string(op)).Bool()
}
type Response struct {
StatusCode int
Code int
Data any
Msg string
}
func NewResponse() *Response {
return &Response{StatusCode: 200}
}
func (r *Response) SetStatusCode(code int) *Response {
r.StatusCode = code
return r
}
func (r *Response) SetDate(data any) *Response {
r.Data = data
return r
}
func (r *Response) SetCode(code int) *Response {
r.Code = code
return r
}
func (r *Response) SetMessage(msg any) *Response {
if str, ok := msg.(string); ok {
r.Msg = str
} else {
if err, ok := msg.(error); ok {
r.Msg = err.Error()
}
}
return r
}