1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package middleware
- import (
- "github.com/gogf/gf/v2/errors/gcode"
- "github.com/gogf/gf/v2/errors/gerror"
- "github.com/gogf/gf/v2/net/ghttp"
- "gof_ppw_api/api"
- "net/http"
- )
- func Response(r *ghttp.Request) {
- r.Middleware.Next()
- // fmt.Println("using res middleware after..")
- // fmt.Println(r.Response.BufferLength())
- if r.Response.BufferLength() > 0 {
- return
- }
- var (
- msg string
- err = r.GetError()
- res = r.GetHandlerResponse()
- code = gerror.Code(err)
- fRes interface{}
- )
- if err != nil {
- if code == gcode.CodeNil {
- code = gcode.CodeInternalError
- }
- msg = err.Error()
- fRes = struct{}{}
- } 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
- }
- fRes = struct{}{}
- } else {
- code = gcode.CodeOK
- if res.(*api.CommonRes) != nil {
- fRes = res.(*api.CommonRes).Data
- } else {
- fRes = struct{}{}
- }
- }
- r.Response.WriteJsonExit(DefaultHandlerResponse{
- Code: code.Code(),
- Message: msg,
- Data: fRes,
- })
- }
|