直接完成

此提交包含在:
2022-11-23 20:56:28 +08:00
當前提交 b924e94c63
共有 21 個檔案被更改,包括 829 行新增0 行删除

11
pkg/app/handler_func.go 一般檔案
查看文件

@@ -0,0 +1,11 @@
package app
import "github.com/gin-gonic/gin"
func HandlerFunc(handler func(aw *Wrapper) Result) func(c *gin.Context) {
return func(c *gin.Context) {
aw := NewWrapper(c)
res := handler(aw)
c.JSON(200, res)
}
}

72
pkg/app/wrapper.go 一般檔案
查看文件

@@ -0,0 +1,72 @@
package app
import (
"github.com/gin-gonic/gin"
"integrity-checkin/pkg/util"
)
type Result struct {
Code int `json:"code"`
Msg string `json:"msg,omitempty"`
Data interface{} `json:"data,omitempty"`
wrapper *Wrapper
}
func (r Result) SendJSON() {
r.wrapper.Ctx.JSON(200, r)
}
type Wrapper struct {
Ctx *gin.Context
}
func NewWrapper(c *gin.Context) *Wrapper {
return &Wrapper{Ctx: c}
}
func (w Wrapper) OK() Result {
return Result{
Code: 0,
Msg: "",
Data: nil,
wrapper: &w,
}
}
func (w Wrapper) Success(data interface{}) Result {
return Result{
Code: 0,
Msg: "",
Data: data,
wrapper: &w,
}
}
func (w Wrapper) Error(msg string) Result {
return Result{
Code: -1,
Msg: msg,
Data: nil,
wrapper: &w,
}
}
func (w Wrapper) ErrorWithCode(code int, msg string) Result {
return Result{
Code: code,
Msg: msg,
Data: nil,
wrapper: &w,
}
}
func (w Wrapper) GetIP() string {
return w.Ctx.ClientIP()
}
func (w Wrapper) ExtractUserClaims() *util.UserClaims {
raw, exist := w.Ctx.Get("userClaims")
if !exist {
panic("userClaims not exists")
}
uc, ok := raw.(*util.UserClaims)
if !ok {
panic("userClaims failed to convert")
}
return uc
}