55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
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"
|
|
)
|
|
|
|
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,
|
|
})
|
|
}
|