Files
wechat_ipad_pro/api/swagger/register.go
2026-02-17 13:06:23 +08:00

88 lines
1.9 KiB
Go

package swagger
import (
"reflect"
)
// 手动注册的API
var manualAPIs = make(map[string]map[string]SwgMap)
// 手动注册的模型
var manualModels = make(map[string]interface{})
// RegisterAPI 手动注册API到Swagger文档
func RegisterAPI(path, method, summary string, model interface{}) {
if manualAPIs[path] == nil {
manualAPIs[path] = make(map[string]SwgMap)
}
params := []SwgMap{
{
"in": "query",
"name": "key",
"type": "string",
"description": "账号唯一标识",
},
}
// 如果模型不为空,添加请求体参数
if model != nil {
modelType := reflect.TypeOf(model)
modelName := modelType.Name()
if modelName == "" && modelType.Kind() == reflect.Ptr {
modelName = modelType.Elem().Name()
}
params = append(params, SwgMap{
"in": "body",
"name": "body",
"description": "请求参数",
"schema": SwgMap{
"$ref": "#/definitions/" + modelName,
},
})
}
manualAPIs[path][method] = SwgMap{
"summary": summary,
"parameters": params,
"responses": SwgMap{
"200": SwgMap{
"description": "",
},
},
"tags": []string{getTag(path)},
}
}
// RegisterModel 手动注册模型到Swagger文档
func RegisterModel(model interface{}) {
modelType := reflect.TypeOf(model)
modelName := modelType.Name()
if modelName == "" && modelType.Kind() == reflect.Ptr {
modelName = modelType.Elem().Name()
}
manualModels[modelName] = model
}
// GetManualAPIs 获取所有手动注册的API
func GetManualAPIs() map[string]SwgMap {
result := make(map[string]SwgMap)
for path, methods := range manualAPIs {
pathMap := make(SwgMap)
for method, spec := range methods {
pathMap[method] = spec
}
result[path] = pathMap
}
return result
}
// GetManualModels 获取所有手动注册的模型
func GetManualModels() map[string]interface{} {
return manualModels
}