1 Star 0 Fork 0

CaptialSTeam/ubdframe

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api_response.go 2.41 KB
一键复制 编辑 原始数据 按行查看 历史
Souki 提交于 2025-12-08 17:01 +08:00 . !7modify support
package plugins
import (
"gitee.com/captials-team/ubdframe/src/pkg/atapi/config"
"gitee.com/captials-team/ubdframe/src/pkg/atapi/ifs"
"github.com/go-openapi/spec"
"net/http"
)
type ApiResponsePlugin struct {
conf *config.PluginData
}
func NewApiResponsePlugin() *ApiResponsePlugin {
//初始化
p := &ApiResponsePlugin{}
return p
}
func (p *ApiResponsePlugin) Name() string {
return "API_RESPONSE"
}
func (p *ApiResponsePlugin) Describe() string {
return `
提供返回响应数据,http路由定义的能力
`
}
func (p *ApiResponsePlugin) Factory(conf *config.PluginData) ifs.IApiPluginFactory {
fa := &ApiResponsePlugin{conf: conf}
return fa
}
func (p *ApiResponsePlugin) Do(running ifs.RunningParam) {
ctx := running.GinCtx()
resp := struct {
Code int `json:"code"` //返回码,0:成功,>0为对应错误码
Msg string `json:"msg"`
Data interface{} `json:"data"`
}{
Code: 0,
Msg: "SUCCESS",
Data: nil,
}
//支持error直接处理
err, ok := running.GetVar("error").(error)
if ok {
resp.Code = 1
resp.Msg = err.Error()
ctx.JSON(http.StatusOK, resp)
return
}
code, ok := running.GetVar("code").(int)
if ok {
resp.Code = code
}
msg, ok := running.GetVar("msg").(string)
if ok {
resp.Msg = msg
}
data := running.GetVar("data")
if data != nil {
resp.Data = data
}
ctx.JSON(http.StatusOK, resp)
}
func (p *ApiResponsePlugin) HttpHandler() ifs.IApiPluginHttpHandler {
return &ApiResponseHttpHandler{
action: p,
data: p.conf,
}
}
func (p *ApiResponsePlugin) CmdHandler() ifs.IApiPluginCmdHandler {
return nil
}
type ApiResponseHttpHandler struct {
action *ApiResponsePlugin
data *config.PluginData
}
func (p *ApiResponseHttpHandler) swaggerDefinitions() map[string]spec.Schema {
resp := newSwaggerSpecSchema("object", "通用返回信息", "")
resp.SchemaProps = spec.SchemaProps{
Description: "响应数据",
Type: []string{"object"},
Properties: map[string]spec.Schema{
"code": newSwaggerSpecSchema("int", "状态码,0为正常,非0为有错误", 1),
"msg": newSwaggerSpecSchema("string", "提示消息", "SUCCESS"),
"data": newSwaggerSpecSchema("object", "具体数据", nil),
},
}
schema := map[string]spec.Schema{
"api.common.response": resp,
}
return schema
}
func (p *ApiResponseHttpHandler) Router() []config.GinHandleParam {
return []config.GinHandleParam{
{
SwaggerDefinitions: p.swaggerDefinitions(),
},
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/captials-team/ubdframe.git
git@gitee.com:captials-team/ubdframe.git
captials-team
ubdframe
ubdframe
v1.0.4

搜索帮助