代码拉取完成,页面将自动刷新
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(),
},
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。