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