mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-10-16 04:50:41 +08:00
update gin handle
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user