hotgo/server/internal/logic/middleware/response.go

61 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package middleware
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2022 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package middleware
import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"hotgo/internal/library/contexts"
"hotgo/internal/library/response"
"hotgo/utility/charset"
)
// ResponseHandler HTTP响应预处理
func (s *sMiddleware) ResponseHandler(r *ghttp.Request) {
r.Middleware.Next()
var (
ctx = r.Context()
comResponse = contexts.Get(ctx).Response
code = gcode.CodeOK.Code()
message = "操作成功"
data interface{}
err error
)
// 模板页面响应
if "text/html" == r.Response.Header().Get("Content-Type") {
r.Middleware.Next()
return
}
// 已存在响应内容且是comResponse返回的时中断运行
if r.Response.BufferLength() > 0 && comResponse != nil {
return
}
if err = r.GetError(); err != nil {
// 记录到自定义错误日志文件
g.Log("exception").Print(ctx, "exception:", err)
code = gerror.Code(err).Code()
message = err.Error()
// 是否输出错误到页面
if g.Cfg().MustGet(ctx, "hotgo.debug", true).Bool() {
data = charset.GetStack(err)
}
} else {
data = r.GetHandlerResponse()
}
// 返回固定的友好信息
response.RJson(r, code, message, data)
}