2023-06-15 09:41:30 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"chatplus/core"
|
2023-09-12 10:49:55 +08:00
|
|
|
"chatplus/core/types"
|
2023-06-15 09:41:30 +08:00
|
|
|
logger2 "chatplus/logger"
|
2023-09-05 11:47:03 +08:00
|
|
|
"chatplus/utils"
|
2023-09-12 10:49:55 +08:00
|
|
|
"fmt"
|
2023-06-19 07:06:59 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-06-15 09:41:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var logger = logger2.GetLogger()
|
|
|
|
|
|
|
|
type BaseHandler struct {
|
2023-06-19 07:06:59 +08:00
|
|
|
App *core.AppServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) GetTrim(c *gin.Context, key string) string {
|
|
|
|
return strings.TrimSpace(c.Query(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) PostInt(c *gin.Context, key string, defaultValue int) int {
|
2023-09-05 11:47:03 +08:00
|
|
|
return utils.IntValue(c.PostForm(key), defaultValue)
|
2023-06-19 07:06:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) GetInt(c *gin.Context, key string, defaultValue int) int {
|
2023-09-05 11:47:03 +08:00
|
|
|
return utils.IntValue(c.Query(key), defaultValue)
|
2023-06-19 07:06:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) GetFloat(c *gin.Context, key string) float64 {
|
2023-09-05 11:47:03 +08:00
|
|
|
return utils.FloatValue(c.Query(key))
|
2023-06-19 07:06:59 +08:00
|
|
|
}
|
|
|
|
func (h *BaseHandler) PostFloat(c *gin.Context, key string) float64 {
|
2023-09-05 11:47:03 +08:00
|
|
|
return utils.FloatValue(c.PostForm(key))
|
2023-06-19 07:06:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *BaseHandler) GetBool(c *gin.Context, key string) bool {
|
2023-09-05 11:47:03 +08:00
|
|
|
return utils.BoolValue(c.Query(key))
|
2023-06-19 07:06:59 +08:00
|
|
|
}
|
|
|
|
func (h *BaseHandler) PostBool(c *gin.Context, key string) bool {
|
2023-09-05 11:47:03 +08:00
|
|
|
return utils.BoolValue(c.PostForm(key))
|
2023-06-15 09:41:30 +08:00
|
|
|
}
|
2023-09-12 10:49:55 +08:00
|
|
|
func (h *BaseHandler) GetUserKey(c *gin.Context) string {
|
|
|
|
userId, ok := c.Get(types.LoginUserID)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("users/%v", userId)
|
|
|
|
}
|