diff --git a/controllers/admin/api.go b/controllers/admin/api.go index 23adf6b..1f41584 100644 --- a/controllers/admin/api.go +++ b/controllers/admin/api.go @@ -1,7 +1,6 @@ package admin import ( - "crypto/rand" "encoding/hex" "encoding/json" "net/http" @@ -42,8 +41,12 @@ func APIListHandler(w http.ResponseWriter, r *http.Request) { // 获取应用UUID参数(用于按应用筛选接口) appUUID := strings.TrimSpace(r.URL.Query().Get("app_uuid")) - // 获取搜索参数 - search := strings.TrimSpace(r.URL.Query().Get("search")) + // 获取接口类型参数(用于按接口类型筛选) + apiTypeStr := strings.TrimSpace(r.URL.Query().Get("api_type")) + var apiType int + if apiTypeStr != "" { + apiType, _ = strconv.Atoi(apiTypeStr) + } // 构建查询 db, err := database.GetDB() @@ -61,9 +64,9 @@ func APIListHandler(w http.ResponseWriter, r *http.Request) { query = query.Where("app_uuid = ?", appUUID) } - // 如果有搜索条件,添加搜索 - if search != "" { - query = query.Where("api_key LIKE ? OR app_uuid LIKE ?", "%"+search+"%", "%"+search+"%") + // 如果指定了接口类型,则按接口类型筛选 + if apiType > 0 { + query = query.Where("api_type = ?", apiType) } // 获取总数 @@ -274,6 +277,48 @@ func APIGetAppsHandler(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(response) } +// APIGetTypesHandler 获取接口类型列表API处理器 +func APIGetTypesHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + // 构建接口类型列表 + type APITypeItem struct { + Value int `json:"value"` + Name string `json:"name"` + } + + var apiTypes []APITypeItem + + // 获取所有有效的API类型 + validTypes := []int{ + models.APITypeGetBulletin, models.APITypeGetUpdateUrl, models.APITypeCheckAppVersion, models.APITypeGetCardInfo, + models.APITypeSingleLogin, + models.APITypeUserLogin, models.APITypeUserRegin, models.APITypeUserRecharge, models.APITypeCardRegin, + models.APITypeLogOut, + models.APITypeGetExpired, models.APITypeCheckUserStatus, models.APITypeGetAppData, models.APITypeGetVariable, + models.APITypeUpdatePwd, models.APITypeMacChangeBind, models.APITypeIPChangeBind, + models.APITypeDisableUser, models.APITypeBlackUser, models.APITypeUserDeductedTime, + } + + for _, apiType := range validTypes { + apiTypes = append(apiTypes, APITypeItem{ + Value: apiType, + Name: models.GetAPITypeName(apiType), + }) + } + + response := map[string]interface{}{ + "success": true, + "data": apiTypes, + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(response) +} + func APIGenerateKeysHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) @@ -375,64 +420,3 @@ func APIGenerateKeysHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(response) } - -func APIResetKeyHandler(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodPost { - http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) - return - } - - var req struct { - ID uint `json:"id"` - } - - if err := json.NewDecoder(r.Body).Decode(&req); err != nil { - http.Error(w, "Invalid JSON", http.StatusBadRequest) - return - } - - if req.ID == 0 { - http.Error(w, "接口ID不能为空", http.StatusBadRequest) - return - } - - db, err := database.GetDB() - if err != nil { - logrus.WithError(err).Error("Failed to get database connection") - http.Error(w, "Internal server error", http.StatusInternalServerError) - return - } - - var api models.API - if err := db.First(&api, req.ID).Error; err != nil { - http.Error(w, "接口不存在", http.StatusNotFound) - return - } - - // 生成新的16位大写十六进制密钥 - bytes := make([]byte, 8) - if _, err := rand.Read(bytes); err != nil { - logrus.WithError(err).Error("Failed to generate random API key") - http.Error(w, "生成密钥失败", http.StatusInternalServerError) - return - } - newKey := strings.ToUpper(hex.EncodeToString(bytes)) - - if err := db.Model(&api).Update("api_key", newKey).Error; err != nil { - logrus.WithError(err).Error("Failed to update API key") - http.Error(w, "更新密钥失败", http.StatusInternalServerError) - return - } - - response := map[string]interface{}{ - "success": true, - "message": "接口密钥重置成功", - "data": map[string]interface{}{ - "id": api.ID, - "api_key": newKey, - }, - } - - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(response) -} diff --git a/models/api.go b/models/api.go index 2fbbc0b..89fae2d 100644 --- a/models/api.go +++ b/models/api.go @@ -1,12 +1,7 @@ package models import ( - "crypto/rand" - "encoding/hex" - "strings" "time" - - "gorm.io/gorm" ) // API 接口表模型 @@ -21,9 +16,6 @@ type API struct { // API类型(int型) APIType int `gorm:"not null;comment:API类型" json:"api_type"` - // API密钥 - APIKey string `gorm:"size:255;not null;uniqueIndex;comment:API密钥,唯一标识" json:"api_key"` - // 应用UUID,关联到App表 AppUUID string `gorm:"size:36;not null;index;comment:关联的应用UUID" json:"app_uuid"` @@ -55,17 +47,6 @@ type API struct { UpdatedAt time.Time `gorm:"comment:更新时间" json:"updated_at"` } -// BeforeCreate 在创建记录前自动生成API密钥 -func (api *API) BeforeCreate(tx *gorm.DB) error { - if api.APIKey == "" { - // 生成16位大写十六进制API密钥 - bytes := make([]byte, 8) // 8字节 = 16位十六进制字符 - rand.Read(bytes) - api.APIKey = strings.ToUpper(hex.EncodeToString(bytes)) - } - return nil -} - // TableName 指定表名 func (API) TableName() string { return "apis" diff --git a/server/admin.go b/server/admin.go index 87538da..57c9150 100644 --- a/server/admin.go +++ b/server/admin.go @@ -76,7 +76,7 @@ func RegisterAdminRoutes(mux *http.ServeMux) { mux.HandleFunc("/admin/api/apis/list", adminctl.AdminAuthRequired(adminctl.APIListHandler)) mux.HandleFunc("/admin/api/apis/update", adminctl.AdminAuthRequired(adminctl.APIUpdateHandler)) mux.HandleFunc("/admin/api/apis/apps", adminctl.AdminAuthRequired(adminctl.APIGetAppsHandler)) - mux.HandleFunc("/admin/api/apis/reset_key", adminctl.AdminAuthRequired(adminctl.APIResetKeyHandler)) + mux.HandleFunc("/admin/api/apis/types", adminctl.AdminAuthRequired(adminctl.APIGetTypesHandler)) mux.HandleFunc("/admin/api/apis/generate_keys", adminctl.AdminAuthRequired(adminctl.APIGenerateKeysHandler)) // 系统信息API(用于仪表盘定时刷新) diff --git a/web/template/admin/apis.html b/web/template/admin/apis.html index 60a1a14..b8c1405 100644 --- a/web/template/admin/apis.html +++ b/web/template/admin/apis.html @@ -17,13 +17,14 @@
- +
- +
-
@@ -38,7 +39,6 @@