GetAPIKeys 获取API密钥列表 @Summary 获取API密钥列表 @Description 获取当前企业下的API密钥列表。管理员可获取全局API密钥,普通用户只能获取与有权限的知识库关联的API密钥。 @Tags API密钥管理 @Accept json @Produce json @Security BearerAuth @Param type query string false "筛选类型:personal=个人key,library=知识库key,默认library" @Param library_id path int false "知识库ID(通过路径传递)" @S
(c *gin.Context)
| 122 | // @Router /api/api-keys [get] |
| 123 | // @Router /api/libraries/{library_id}/api-keys [get] # 知识库相关路由 |
| 124 | func (ctrl *APIKeyController) GetAPIKeys(c *gin.Context) { |
| 125 | eid := config.GetEID(c) |
| 126 | if eid == 0 { |
| 127 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("缺少企业ID参数")) |
| 128 | return |
| 129 | } |
| 130 | |
| 131 | creatorID := config.GetUserId(c) |
| 132 | role := config.GetUserRole(c) |
| 133 | keyType := strings.ToLower(strings.TrimSpace(c.Query("type"))) |
| 134 | if keyType != "" && keyType != "personal" && keyType != "library" { |
| 135 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的API密钥类型参数")) |
| 136 | return |
| 137 | } |
| 138 | |
| 139 | libraryIDParam := c.Param("library_id") |
| 140 | var libraryID *int64 |
| 141 | |
| 142 | if libraryIDParam != "" { |
| 143 | parsedID, convErr := strconv.ParseInt(libraryIDParam, 10, 64) |
| 144 | if convErr != nil { |
| 145 | c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的知识库ID参数")) |
| 146 | return |
| 147 | } |
| 148 | libraryID = &parsedID |
| 149 | } |
| 150 | |
| 151 | apiKeyService := mcpsvc.NewAPIKeyService() |
| 152 | apiKeys, err := apiKeyService.ListAPIKeys(c.Request.Context(), eid, creatorID, role, keyType, libraryID) |
| 153 | if err != nil { |
| 154 | if isPermissionRelatedError(err) { |
| 155 | c.JSON(http.StatusForbidden, model.ForbiddenError.ToResponse(err)) |
| 156 | return |
| 157 | } |
| 158 | c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err)) |
| 159 | return |
| 160 | } |
| 161 | |
| 162 | // 构建响应数据 |
| 163 | responseKeys := make([]APIKeyInfo, len(apiKeys)) |
| 164 | for i, key := range apiKeys { |
| 165 | responseKeys[i] = APIKeyInfo{ |
| 166 | ID: key.ID, |
| 167 | Name: key.Name, |
| 168 | Description: key.Description, |
| 169 | Eid: key.Eid, |
| 170 | CreatorID: key.CreatorID, |
| 171 | Status: key.Status, |
| 172 | CreatedTime: key.CreatedTime, |
| 173 | UpdatedTime: key.UpdatedTime, |
| 174 | ExpiresAt: key.ExpiresAt, |
| 175 | Key: key.Key, |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // 规范化返回值 |
| 180 | response := GetAPIKeysResponse{ |
| 181 | APIKeys: responseKeys, |
nothing calls this directly
no test coverage detected