初始化项目

This commit is contained in:
2025-12-06 15:24:30 +08:00
parent 88a2753211
commit fd08b8925f
59 changed files with 2456 additions and 447 deletions

View File

@@ -0,0 +1,246 @@
package service
import (
"context"
"crypto/rand"
"encoding/hex"
"cidservice/dao"
"cidservice/model/dto"
"cidservice/model/entity"
"github.com/gogf/gf/v2/errors/gerror"
)
var (
Application = applicationService{}
)
type applicationService struct{}
// CreateApplication 创建应用
func (s *applicationService) CreateApplication(ctx context.Context, req *dto.CreateApplicationReq) (id int64, err error) {
// 检查应用名称是否已存在
existingApp, err := dao.Application.GetByName(ctx, req.Name)
if err != nil {
return 0, err
}
if existingApp != nil {
return 0, gerror.New("应用名称已存在")
}
// 生成API密钥
appKey, appSecret, err := s.generateAPIKeys()
if err != nil {
return 0, err
}
application := &entity.Application{
TenantId: req.TenantID,
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,
Status: "active",
AppKey: appKey,
AppSecret: appSecret,
CallbackURL: req.CallbackURL,
}
return dao.Application.Create(ctx, application)
}
// UpdateApplication 更新应用
func (s *applicationService) UpdateApplication(ctx context.Context, id int64, 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 != 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 *applicationService) GetApplicationsByTenant(ctx context.Context, tenantID int64, platform, status string, page, size int) (list []*entity.Application, total int64, err error) {
// 构建过滤条件
filter := make(map[string]interface{})
filter["tenant_id"] = tenantID
if platform != "" {
filter["platform"] = platform
}
if status != "" {
filter["status"] = status
}
// 调用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 *applicationService) GetApplicationByKey(ctx context.Context, appKey string) (application *entity.Application, err error) {
return dao.Application.GetByAPIKey(ctx, appKey)
}
// GetApplicationByID 根据ID获取应用
func (s *applicationService) GetApplicationByID(ctx context.Context, id int64) (application *entity.Application, err error) {
return dao.Application.GetByID(ctx, id)
}
// DeleteApplication 删除应用
func (s *applicationService) DeleteApplication(ctx context.Context, id int64) (affected int64, err error) {
err = dao.Application.Delete(ctx, id)
if err != nil {
return 0, err
}
return 1, nil
}
// ValidateApplication 验证应用权限
func (s *applicationService) 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 *applicationService) 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 *applicationService) ResetAPIKeys(ctx context.Context, id int64) (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
}