1 Star 0 Fork 0

CaptialSTeam/ubdframe

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
api_detail.go 4.55 KB
一键复制 编辑 原始数据 按行查看 历史
Souki 提交于 2025-12-08 17:01 +08:00 . !7modify support
package plugins
import (
"fmt"
"gitee.com/captials-team/ubdframe/src/common/utils"
"gitee.com/captials-team/ubdframe/src/pkg/atapi/config"
"gitee.com/captials-team/ubdframe/src/pkg/atapi/ifs"
"gitee.com/captials-team/ubdframe/src/pkg/logs"
"github.com/go-openapi/spec"
"github.com/spf13/cast"
"net/http"
"strings"
)
type ApiQueryPlugin struct {
//请求参数(可搜索条件字段)
idParam string //主键用参数
//返回字段
respFields []string
conf *config.PluginData
}
func NewApiQueryPlugin() *ApiQueryPlugin {
//初始化
p := &ApiQueryPlugin{}
return p
}
func (p *ApiQueryPlugin) Name() string {
return "API_QUERY"
}
func (p *ApiQueryPlugin) init() {
m := p.conf.Plugin.ExtendParams
if m == nil {
m = map[string]string{}
}
//响应字段集合
p.respFields = p.conf.Api.Data.FieldNames()
responseFields := strings.Split(m[ResponseFieldsParamsKey], ",")
if len(responseFields) > 0 {
p.respFields = utils.ArrayIntersection(p.respFields, responseFields)
}
p.respFields = utils.ArrayUnion([]string{
"id",
"created_at",
"updated_at",
}, p.respFields)
//默认值
p.idParam = m.ExtendParamByString(IdFieldKey, "id")
}
func (p *ApiQueryPlugin) Factory(conf *config.PluginData) ifs.IApiPluginFactory {
fa := &ApiQueryPlugin{conf: conf}
fa.init()
return fa
}
func (p *ApiQueryPlugin) Describe() string {
return `
提供查询单条数据的能力
`
}
func (p *ApiQueryPlugin) Extends() map[string]string {
return map[string]string{
ResponseFieldsParamsKey: "f1,f2,f3", //指定返回字段名称,多个逗号分隔
IdFieldKey: "id", //指定返回字段名称,多个逗号分隔
}
}
func (p *ApiQueryPlugin) Do(running ifs.RunningParam) {
var m = make(map[string]interface{})
running.GinCtx().ShouldBindJSON(&m)
id := cast.ToInt(m[p.idParam])
//todo 增加搜索参数的处理
//具体操作查询
data, err := running.DataExec().DoQuery(map[string]interface{}{
"id": id,
}, p.respFields)
if err != nil {
running.SetVar("error", err)
return
}
running.SetVar("data", data)
}
func (p *ApiQueryPlugin) HttpHandler() ifs.IApiPluginHttpHandler {
return &ApiQueryHttpHandler{
plugin: p,
data: p.conf,
}
}
func (p *ApiQueryPlugin) CmdHandler() ifs.IApiPluginCmdHandler {
return nil
}
type ApiQueryHttpHandler struct {
plugin *ApiQueryPlugin
//运行时参数变量
data *config.PluginData
}
func (p *ApiQueryHttpHandler) swaggerPathItem() spec.PathItem {
reqParams := map[string]spec.Schema{}
fields := map[string]spec.Schema{}
logs.Out.Info("Fields= %+v", p.data.Api.Data.Fields)
logs.Out.Info("Api= %+v", p.data.Api)
reqParams[p.plugin.idParam] = newSwaggerSpecSchema("string", "指定id", 1)
for _, v := range p.plugin.respFields {
fields[v] = newSwaggerSpecSchema("string", v, "example")
}
resp1 := spec.Response{
ResponseProps: spec.ResponseProps{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Description: "详情数据",
Type: []string{"object"},
Items: &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Description: "详情内容",
Type: []string{"object"},
Properties: fields,
},
},
},
},
},
Headers: nil,
Examples: nil,
},
}
return spec.PathItem{
PathItemProps: spec.PathItemProps{
Post: &spec.Operation{
OperationProps: spec.OperationProps{
Summary: "查询详情",
Description: "查询详情,根据传递参数返回详情信息",
Produces: []string{"application/json"},
Schemes: nil,
Tags: []string{p.data.Api.Name},
Parameters: []spec.Parameter{
{
ParamProps: spec.ParamProps{
Description: "传参",
Name: "param",
In: "body",
Required: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"object"},
Properties: reqParams,
},
},
AllowEmptyValue: true,
},
},
},
Responses: &spec.Responses{
ResponsesProps: spec.ResponsesProps{
Default: nil,
StatusCodeResponses: map[int]spec.Response{
200: newSwaggerResponseFromRef("api.common.response"),
201: resp1,
},
},
},
},
},
},
}
}
func (p *ApiQueryHttpHandler) Router() []config.GinHandleParam {
path := fmt.Sprintf("/%s/query", p.data.Api.Name)
return []config.GinHandleParam{
{
Method: http.MethodPost,
Path: path,
SwaggerPaths: map[string]spec.PathItem{
path: p.swaggerPathItem(),
},
},
}
}
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

搜索帮助