生成表结构,开始实现接口逻辑
This commit is contained in:
@ -1,11 +1,54 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gcode"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"net/http"
|
||||
"server/utility/ecode"
|
||||
)
|
||||
|
||||
// Response 统一响应中间件
|
||||
func Response(r *ghttp.Request) {
|
||||
r.Middleware.Next()
|
||||
|
||||
if r.Response.BufferLength() > 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
msg string
|
||||
err = r.GetError()
|
||||
res = r.GetHandlerResponse()
|
||||
code = gerror.Code(err)
|
||||
)
|
||||
glog.Debug(gctx.New(), "错误", err)
|
||||
if err != nil {
|
||||
if gconv.String(code.Detail()) != "customer" && code.Code() != 51 {
|
||||
msg = ecode.Fail.Message()
|
||||
} else if code.Code() == 51 {
|
||||
msg = ecode.Params.Message()
|
||||
} else {
|
||||
msg = code.Message()
|
||||
}
|
||||
} else if r.Response.Status > 0 && r.Response.Status != http.StatusOK {
|
||||
msg = http.StatusText(r.Response.Status)
|
||||
switch r.Response.Status {
|
||||
case http.StatusNotFound:
|
||||
code = gcode.CodeNotFound
|
||||
case http.StatusForbidden:
|
||||
code = gcode.CodeNotAuthorized
|
||||
default:
|
||||
code = gcode.CodeUnknown
|
||||
}
|
||||
} else {
|
||||
code = gcode.CodeOK
|
||||
}
|
||||
r.Response.WriteJson(ghttp.DefaultHandlerResponse{
|
||||
Code: code.Code(),
|
||||
Message: msg,
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user