diff --git a/api/controller/api.go b/api/controller/api.go index 73915f4..70cb0f3 100644 --- a/api/controller/api.go +++ b/api/controller/api.go @@ -55,6 +55,43 @@ func (r *AdminApi) GetApiPage(c *gin.Context) { responses.OkWithData(result, c) } +// GetApiList 获取api列表 +func (r *AdminApi) GetApiList(c *gin.Context) { + apiRequest := requests.ApiRequest{} + if err := c.ShouldBind(&apiRequest.GetApiList); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + // 参数验证 + if err := global.Validate.Struct(apiRequest.GetApiList); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + adminApiDao := dao.AdminApiDao{} + + maps := make(map[string]interface{}) + if apiRequest.GetApiList.ApiName != "" { + maps["api_name"] = apiRequest.GetApiList.ApiName + } + adminApis, err := adminApiDao.GetAdminApiList(maps) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + if len(adminApis) == 0 { + responses.Ok(c) + return + } + + // 处理返回值 + getApiPageResponses := apiResponse.GetApiListResponse(adminApis) + + responses.OkWithData(getApiPageResponses, c) +} + // AddApi 新增接口 func (r *AdminApi) AddApi(c *gin.Context) { apiRequest := requests.ApiRequest{} diff --git a/api/requests/api.go b/api/requests/api.go index 972e6ba..4bbb413 100644 --- a/api/requests/api.go +++ b/api/requests/api.go @@ -2,6 +2,7 @@ package requests type ApiRequest struct { GetApiPage // 获取接口列表-分页 + GetApiList // 获取接口列表 AddApi // 新增接口 DeleteApi // 删除接口-批量 PutApi // 修改接口 @@ -16,6 +17,11 @@ type GetApiPage struct { PageSize int `json:"page_size" form:"page_size" label:"每页个数"` } +// GetApiList 获取接口列表 +type GetApiList struct { + ApiName string `json:"api_name" form:"api_name" label:"api名称"` +} + // AddApi 新增接口 type AddApi struct { ApiName string `json:"api_name" form:"api_name" validate:"required" label:"接口名称"` @@ -30,7 +36,5 @@ type DeleteApi struct { // PutApi 修改接口 type PutApi struct { - ApiName string `json:"api_name" form:"api_name" label:"api名称"` - ApiMethod string `json:"api_method" form:"api_method" label:"请求类型"` // (put:修改 post:新增 get:获取 delete:删除) - ApiPath string `json:"api_path" form:"api_path" validate:"required" label:"接口路径"` + ApiName string `json:"api_name" form:"api_name" label:"api名称"` } diff --git a/api/responses/apiResponse/api.go b/api/responses/apiResponse/api.go index 79b1b9b..3fedbfc 100644 --- a/api/responses/apiResponse/api.go +++ b/api/responses/apiResponse/api.go @@ -16,6 +16,12 @@ type getApiPage struct { UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间 } +// getApiList 获取接口列表 +type getApiList struct { + APIID string `json:"api_id"` // 主键id + APIName string `json:"api_name"` // api名称 +} + // 接口详情 type getApi struct { APIID string `json:"api_id"` // 主键id @@ -53,6 +59,27 @@ func GetApiPageResponse(adminApi []*model.AdminAPI) []getApiPage { return getApiPageResponses } +// GetApiListResponse 获取接口列表 +func GetApiListResponse(adminApi []*model.AdminAPI) []getApiList { + // 处理返回值 + getApiListResponses := make([]getApiList, len(adminApi)) + + if len(adminApi) > 0 { + for i, v := range adminApi { + // 将原始结构体转换为新结构体 + getApiListResponse := getApiList{ + APIID: strconv.Itoa(int(v.APIID)), + APIName: v.APIName, + } + + // 将转换后的结构体添加到新切片中 + getApiListResponses[i] = getApiListResponse + } + } + + return getApiListResponses +} + // GetApiResponse 接口详情 func GetApiResponse(adminApi *model.AdminAPI) *getApi { return &getApi{ diff --git a/api/router/router.go b/api/router/router.go index ca5030e..ef9517c 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -138,6 +138,9 @@ func privateRouter(r *gin.Engine, api controller.Api) { // 获取接口列表-分页 apiGroup.GET("", api.AdminApi.GetApiPage) + // 获取接口列表 + apiGroup.GET("/list", api.AdminApi.GetApiList) + // 新增接口 apiGroup.POST("", api.AdminApi.AddApi) diff --git a/api/service/api.go b/api/service/api.go index 2896f7e..18775b2 100644 --- a/api/service/api.go +++ b/api/service/api.go @@ -161,27 +161,6 @@ func (r *ApiService) PutApi(c *gin.Context, requestApiId int64, putApiRequest re return false, errors.New("您当前为普通用户,无法修改接口") } - if putApiRequest.ApiPath != adminApi.APIPath && putApiRequest.ApiMethod != adminApi.APIMethod { - // 检测接口名称+请求方式 - maps := make(map[string]interface{}) - maps["api_path"] = putApiRequest.ApiPath - maps["api_method"] = putApiRequest.ApiMethod - adminApis, err := adminApiDao.GetAdminApiList(maps) - if len(adminApis) > 0 || err != nil { - return false, errors.New("接口重复") - } - } - - if putApiRequest.ApiPath != adminApi.APIPath { - // 检测接口路径 - pattern := `^/[\w/]+(\:\w+)?$` - r := regexp.MustCompile(pattern) - - if !r.MatchString(putApiRequest.ApiPath) { - return false, errors.New("接口路径错误") - } - } - // 开始事务 tx := global.Db.Begin() defer func() { @@ -193,8 +172,6 @@ func (r *ApiService) PutApi(c *gin.Context, requestApiId int64, putApiRequest re // 修改接口 data := make(map[string]interface{}) data["api_name"] = putApiRequest.ApiName - data["api_path"] = putApiRequest.ApiPath - data["api_method"] = putApiRequest.ApiMethod err = adminApiDao.EditAdminApiById(tx, requestApiId, data) if err != nil { tx.Rollback()