更新前后端代码
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"filestoragesystem/internal/middleware"
|
||||
"filestoragesystem/internal/service"
|
||||
"filestoragesystem/internal/utils"
|
||||
)
|
||||
|
||||
// Base 控制器基类
|
||||
type Base struct {
|
||||
svc *service.Services
|
||||
}
|
||||
|
||||
// NewBase 创建基类
|
||||
func NewBase(svc *service.Services) *Base { return &Base{svc: svc} }
|
||||
|
||||
// opCtx 从请求上下文提取操作日志上下文
|
||||
func (b *Base) opCtx(c *gin.Context) *service.OpLogContext {
|
||||
ctx := &service.OpLogContext{
|
||||
IP: utils.ClientIP(c),
|
||||
UserAgent: c.GetHeader("User-Agent"),
|
||||
Path: c.Request.URL.Path,
|
||||
Method: c.Request.Method,
|
||||
Username: c.GetString(middleware.CtxUsername),
|
||||
}
|
||||
if uid := middleware.CurrentUserID(c); uid > 0 {
|
||||
ctx.UserID = &uid
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
// bindJSON 绑定JSON请求体
|
||||
func bindJSON(c *gin.Context, obj interface{}) bool {
|
||||
if err := c.ShouldBindJSON(obj); err != nil {
|
||||
utils.FailMsg(c, "参数错误: "+err.Error())
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// paramID 解析路径参数ID
|
||||
func paramID(c *gin.Context) (uint, bool) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
utils.FailMsg(c, "无效的资源ID")
|
||||
return 0, false
|
||||
}
|
||||
return uint(id), true
|
||||
}
|
||||
|
||||
// parseUintQuery 解析整型query参数
|
||||
func parseUintQuery(c *gin.Context, key string) uint {
|
||||
v, err := strconv.ParseUint(c.Query(key), 10, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return uint(v)
|
||||
}
|
||||
|
||||
// parseIntQuery 解析整型query参数(可为负)
|
||||
func parseIntQuery(c *gin.Context, key string, def int) int {
|
||||
n, err := strconv.Atoi(c.Query(key))
|
||||
if err != nil {
|
||||
return def
|
||||
}
|
||||
return n
|
||||
}
|
||||
Reference in New Issue
Block a user