1 Star 0 Fork 0

fkil555/gin-extend

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
app.go 3.42 KB
一键复制 编辑 原始数据 按行查看 历史
fkil555 提交于 2024-08-02 14:10 . param json
package gcontext
import (
"errors"
"strconv"
"strings"
jsoniter "github.com/json-iterator/go"
)
// 二次封装gin的json返回方法
func (geCtx *GEContext) JSON(code int, obj interface{}) {
geCtx.Gin.JSON(code, obj)
}
// 二次封装gin的string返回方法
func (geCtx *GEContext) String(code int, format string, values ...interface{}) {
geCtx.Gin.String(code, format, values...)
}
// 二次封装gin的取参方法, 会依次从POST, GET, URI 取参
func (geCtx *GEContext) AnyParam(key string) (value string) {
if value = geCtx.Gin.PostForm(key); len(value) != 0 {
return
}
if value = geCtx.Gin.Query(key); len(value) != 0 {
return
}
if value = geCtx.Gin.Param(key); len(key) != 0 {
return
}
return
}
// 二次封装gin的取参方法, 支持传入默认值 取不到会返回默认值
func (geCtx *GEContext) DefaultAnyParam(key string, defaultValue string) (value string) {
if value = geCtx.AnyParam(key); len(value) != 0 {
return
}
return defaultValue
}
func (geCtx *GEContext) ParamInt(key string) (value int, err error) {
value, err = strconv.Atoi(geCtx.AnyParam(key))
return
}
func (geCtx *GEContext) ParamInt64(key string) (value int64, err error) {
value, err = strconv.ParseInt(geCtx.AnyParam(key), 10, 64)
return
}
func (geCtx *GEContext) ParamBool(key string) (value bool, err error) {
value, err = strconv.ParseBool(geCtx.AnyParam(key))
return
}
func (geCtx *GEContext) ParamArrayInt(key string) (vals []int, err error) {
value := geCtx.Gin.PostFormArray(key)
if len(value) == 0 {
value = geCtx.Gin.QueryArray(key)
}
if len(value) > 0 {
for _, v := range value {
tmp, err := strconv.Atoi(v)
if err == nil {
vals = append(vals, tmp)
}
}
}
return
}
func (geCtx *GEContext) ParamArrayInt64(key string) (vals []int64, err error) {
value := geCtx.Gin.PostFormArray(key)
if len(value) == 0 {
value = geCtx.Gin.QueryArray(key)
}
if len(value) > 0 {
for _, v := range value {
tmp, err := strconv.ParseInt(v, 10, 64)
if err == nil {
vals = append(vals, tmp)
}
}
}
return
}
func (geCtx *GEContext) ParamIntDefault(key string, def int) (value int, err error) {
value, err = strconv.Atoi(geCtx.AnyParam(key))
if err != nil {
value = def
}
return
}
func (geCtx *GEContext) ParamInt64Default(key string, def int64) (value int64, err error) {
value, err = strconv.ParseInt(geCtx.AnyParam(key), 10, 64)
if err != nil {
value = def
}
return
}
func (geCtx *GEContext) ParamBoolDefault(key string, def bool) (value bool, err error) {
value, err = strconv.ParseBool(geCtx.AnyParam(key))
if err != nil {
value = def
}
return
}
func (geCtx *GEContext) GetLoginID() (userID int64, err error) {
userID, ok := geCtx.Value("loginUserId").(int64)
if !ok {
err = errors.New("token error")
return
}
return
}
func (geCtx *GEContext) GetLoginExtends() (extends string, err error) {
extends, ok := geCtx.Value("loginExtends").(string)
if !ok {
err = errors.New("token error")
return
}
return
}
func (geCtx *GEContext) GetTokenExtends(result interface{}) (err error) {
exts, err := geCtx.GetLoginExtends()
if err != nil {
return
}
return jsoniter.UnmarshalFromString(exts, result)
}
func (geCtx *GEContext) ParamStringArray(key, sep string) []string {
val := geCtx.AnyParam(key)
return strings.Split(val, sep)
}
func (geCtx *GEContext) ParamJson(key string, result interface{}) (err error) {
val := geCtx.AnyParam(key)
return jsoniter.UnmarshalFromString(val, &result)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fkil555/gin-extend.git
git@gitee.com:fkil555/gin-extend.git
fkil555
gin-extend
gin-extend
v0.1.25

搜索帮助