package service import ( "context" "crypto/rand" "encoding/hex" "cid/dao" "cid/model/dto" "cid/model/entity" "github.com/gogf/gf/v2/errors/gerror" ) type application struct{} // Application 应用服务 var Application = new(application) // CreateApplication 创建应用 func (s *application) CreateApplication(ctx context.Context, req *dto.CreateApplicationReq) (id string, err error) { // 检查应用名称是否已存在 existingApp, err := dao.Application.GetByName(ctx, req.Name) if err != nil { return "", err } if existingApp != nil { return "", gerror.New("应用名称已存在") } // 生成API密钥 appKey, appSecret, err := s.generateAPIKeys() if err != nil { return "", err } application := &entity.Application{ Name: req.Name, Code: req.Code, Description: req.Description, Platform: req.Platform, PackageName: req.PackageName, AppStoreURL: req.AppStoreURL, Categories: req.Categories, Tags: req.Tags, AdTypes: req.AdTypes, AppKey: appKey, AppSecret: appSecret, CallbackURL: req.CallbackURL, } // 设置状态 application.Status = "active" return dao.Application.Create(ctx, application) } // UpdateApplication 更新应用 func (s *application) UpdateApplication(ctx context.Context, id string, req *dto.UpdateApplicationReq) (affected int64, err error) { // 检查应用是否存在 existingApp, err := dao.Application.GetByID(ctx, id) if err != nil { return 0, err } if existingApp == nil { return 0, gerror.New("应用不存在") } // 如果更新名称,检查是否与其他应用冲突 if req.Name != "" && req.Name != existingApp.Name { conflictApp, err := dao.Application.GetByName(ctx, req.Name) if err != nil { return 0, err } if conflictApp != nil && conflictApp.Id.Hex() != id { return 0, gerror.New("应用名称已存在") } } // 构建更新数据 updateData := &entity.Application{} if req.Name != "" { updateData.Name = req.Name } if req.Description != "" { updateData.Description = req.Description } if req.Platform != "" { updateData.Platform = req.Platform } if req.PackageName != "" { updateData.PackageName = req.PackageName } if req.AppStoreURL != "" { updateData.AppStoreURL = req.AppStoreURL } if req.CallbackURL != "" { updateData.CallbackURL = req.CallbackURL } if len(req.Categories) > 0 { updateData.Categories = req.Categories } if len(req.Tags) > 0 { updateData.Tags = req.Tags } if len(req.AdTypes) > 0 { updateData.AdTypes = req.AdTypes } // 使用Update方法更新应用 err = dao.Application.Update(ctx, updateData) if err != nil { return 0, err } return 1, nil } // GetApplicationsByTenant 获取租户下的应用列表 func (s *application) GetApplicationsByTenant(ctx context.Context, tenantID string, platform, status string, page, size int) (list []*entity.Application, total int64, err error) { // 调用DAO的GetByTenantID方法获取租户下的所有应用 apps, err := dao.Application.GetByTenantID(ctx, tenantID) if err != nil { return nil, 0, err } // 应用额外的过滤条件 var filteredApps []*entity.Application for _, app := range apps { if platform != "" && app.Platform != platform { continue } if status != "" && app.Status != status { continue } filteredApps = append(filteredApps, app) } // 实现简单的分页 startIndex := (page - 1) * size endIndex := startIndex + size if startIndex >= len(filteredApps) { return []*entity.Application{}, int64(len(filteredApps)), nil } if endIndex > len(filteredApps) { endIndex = len(filteredApps) } return filteredApps[startIndex:endIndex], int64(len(filteredApps)), nil } // GetApplicationByKey 根据API密钥获取应用 func (s *application) GetApplicationByKey(ctx context.Context, appKey string) (application *entity.Application, err error) { return dao.Application.GetByAPIKey(ctx, appKey) } // GetApplicationByID 根据ID获取应用 func (s *application) GetApplicationByID(ctx context.Context, id string) (application *entity.Application, err error) { return dao.Application.GetByID(ctx, id) } // DeleteApplication 删除应用 func (s *application) DeleteApplication(ctx context.Context, id string) (affected int64, err error) { err = dao.Application.Delete(ctx, id) if err != nil { return 0, err } return 1, nil } // ValidateApplication 验证应用权限 func (s *application) ValidateApplication(ctx context.Context, appKey, appSecret string) (application *entity.Application, err error) { app, err := dao.Application.GetByAPIKey(ctx, appKey) if err != nil { return nil, err } if app == nil { return nil, gerror.New("应用不存在") } if app.Status != "active" { return nil, gerror.New("应用状态异常") } if app.AppSecret != appSecret { return nil, gerror.New("密钥验证失败") } return app, nil } // generateAPIKeys 生成API密钥 func (s *application) generateAPIKeys() (appKey, appSecret string, err error) { // 生成32位随机字符串作为AppKey keyBytes := make([]byte, 16) if _, err := rand.Read(keyBytes); err != nil { return "", "", err } appKey = hex.EncodeToString(keyBytes) // 生成64位随机字符串作为AppSecret secretBytes := make([]byte, 32) if _, err := rand.Read(secretBytes); err != nil { return "", "", err } appSecret = hex.EncodeToString(secretBytes) return appKey, appSecret, nil } // ResetAPIKeys 重置API密钥 func (s *application) ResetAPIKeys(ctx context.Context, id string) (appKey, appSecret string, err error) { // 检查应用是否存在 existingApp, err := dao.Application.GetByID(ctx, id) if err != nil { return "", "", err } if existingApp == nil { return "", "", gerror.New("应用不存在") } // 生成新的API密钥 appKey, appSecret, err = s.generateAPIKeys() if err != nil { return "", "", err } // 更新应用密钥 updateData := &entity.Application{ AppKey: appKey, AppSecret: appSecret, } err = dao.Application.Update(ctx, updateData) if err != nil { return "", "", err } return appKey, appSecret, nil }