From 91f136b2e14fa990a6dec4bb7ffe6a59713c6eec Mon Sep 17 00:00:00 2001 From: Sakura <1808479176@qq.com> Date: Wed, 16 Oct 2024 14:48:48 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configs/config.go | 13 ++++++++ internal/api/aiRole/aiRoleCreate.go | 15 ++++++---- internal/data/mysql/aiRole/aiRole.go | 44 ++++++++++++++++++++++++++-- internal/data/mysql/user/user.go | 14 +++++++++ internal/middleware/auth.go | 4 +-- internal/routers/aiRoleRouter.go | 3 +- internal/routers/routers.go | 1 + 7 files changed, 83 insertions(+), 11 deletions(-) diff --git a/configs/config.go b/configs/config.go index 290fff7..4abd18d 100644 --- a/configs/config.go +++ b/configs/config.go @@ -11,6 +11,7 @@ type Config struct { SiteInfo SiteInfo `mapstructure:"siteInfo" json:"siteInfo" yaml:"siteInfo"` Email EmailConfig `mapstructure:"email" json:"email" yaml:"email"` Jwt Jwt `mapstructure:"jwt" yaml:"jwt" json:"jwt"` + AI AIConfig `mapstructure:"ai" json:"ai" yaml:"ai"` } type Data struct { @@ -115,3 +116,15 @@ type Jwt struct { Expires time.Duration `yaml:"expires" json:"expires"` Issuer string `yaml:"issuer" json:"issuer"` } + +type AIConfig struct { + Model string `mapstructure:"model" json:"model"` + ProxyURL string `mapstructure:"proxyUrl" json:"proxyUrl"` + APIKey string `mapstructure:"apiKey" json:"apiKey"` + ChatScope int `mapstructure:"chatScope" json:"chatScope"` + CreateRoleScope int `mapstructure:"createRoleScope" json:"createRoleScope"` // 创建角色的积分消耗 + UpdateRoleScope int `mapstructure:"updateRoleScope" json:"updateRoleScope"` // 更新角色的积分消耗 + DeleteRoleScope int `mapstructure:"deleteRoleScope" json:"deleteRoleScope"` // 删除角色的积分消耗 + RecommendRoleScope int `mapstructure:"recommendRoleScope" json:"recommendRoleScope"` // 推荐角色成功的积分赠送 + RegisterUserScope int `mapstructure:"registerUserScope" json:"registerUserScope"` // 注册用户成功的积分赠送 +} diff --git a/internal/api/aiRole/aiRoleCreate.go b/internal/api/aiRole/aiRoleCreate.go index 501d40a..2851877 100644 --- a/internal/api/aiRole/aiRoleCreate.go +++ b/internal/api/aiRole/aiRoleCreate.go @@ -3,6 +3,7 @@ package aiRole import ( "AI_Server/common/jwt" "AI_Server/internal/data/mysql/aiRole" + "AI_Server/internal/data/mysql/user" "AI_Server/utils/res" "github.com/gofiber/fiber/v3" ) @@ -21,14 +22,16 @@ func (role *AiRoleApi) RoleCreate(c fiber.Ctx) error { if err != nil { return res.FailWithMsgAndReason(c, "请求参数错误", err.Error()) } - claims := c.Locals("claims").(jwt.PayLoad) - if _, err = aiRole.FindAiRole(claims.UserId, req.Title); err != nil { - return res.FailWithMsgAndReason(c, "角色已存在,同一个用户不能创建相同名称的角色", err.Error()) + // 验证角色是否存在 + findUser, err := user.FindUserByUserId(claims.UserId) + if err != nil { + return res.FailWithMsgAndReason(c, "用户不存在", err.Error()) + } + if _, err = aiRole.CreateAiRole(findUser, req.Title, req.Avatar, req.Category, req.Abstract, req.Prompt); err != nil { + return res.FailWithMsgAndReason(c, "创建角色失败", err.Error()) } - // 开启事务写入 - - return nil + return res.OkWithMsg(c, "创建角色成功") } diff --git a/internal/data/mysql/aiRole/aiRole.go b/internal/data/mysql/aiRole/aiRole.go index 2a88ab0..1ae7cbf 100644 --- a/internal/data/mysql/aiRole/aiRole.go +++ b/internal/data/mysql/aiRole/aiRole.go @@ -2,16 +2,56 @@ package aiRole import ( "AI_Server/init/data" + "AI_Server/internal/data/mysql/user" "AI_Server/internal/modeles" + "errors" + "github.com/rs/zerolog/log" + "gorm.io/gorm" ) -func CreateAiRole() { - +// CreateAiRole 用于创建一个角色 +func CreateAiRole(findUser *modeles.User, title, avatar, category, abstract, prompt string) (*modeles.AiRole, error) { + // 1.查找该用户是否创建了相同的角色 + if _, err := FindAiRole(findUser.ID, title); err == nil { + //if errors.Is(err, gorm.ErrRecordNotFound) { + // // 角色不存在,继续创建角色 + // log.Info().Msg("角色不存在,创建角色") + //} else if err != nil { + // log.Info().Msg("角色已存在") + // return nil, errors.New("角色已存在,同一个用户不能创建相同名称的角色") + //} + return nil, errors.New("角色已存在,同一个用户不能创建相同名称的角色") + } + // 2.构建角色 + aiRole := &modeles.AiRole{ + UserID: findUser.ID, + Title: title, + Avatar: avatar, + Category: category, + Abstract: abstract, + Prompt: prompt, + } + // 3.开启事务创建角色 + err := data.DB.Transaction(func(tx *gorm.DB) error { + if err := tx.Create(aiRole).Error; err != nil { + return err + } + // 4.扣除用户的积分 + if err := user.DeductUserPoints(tx, findUser, 100); err != nil { + return err + } + return nil + }) + if err != nil { + return nil, err + } + return aiRole, nil } // FindAiRole 用于寻找该用户是否创建了相同的角色 func FindAiRole(userID uint, title string) (*modeles.AiRole, error) { aiRole := &modeles.AiRole{} + log.Info().Any("userId", userID).Any("title", title).Msg("查找角色") if err := data.DB.Take(aiRole, "user_id = ? and title = ?", userID, title).Error; err != nil { return nil, err } diff --git a/internal/data/mysql/user/user.go b/internal/data/mysql/user/user.go index 94f0ad3..28942d3 100644 --- a/internal/data/mysql/user/user.go +++ b/internal/data/mysql/user/user.go @@ -1,11 +1,13 @@ package user import ( + "AI_Server/init/conf" "AI_Server/init/data" "AI_Server/internal/modeles" "AI_Server/utils/rand" "errors" "github.com/rs/zerolog/log" + "gorm.io/gorm" ) func CreateUser(registerSource modeles.RegisterSource, val string) (*modeles.User, error) { @@ -59,3 +61,15 @@ func FindUserByUserId(id uint) (*modeles.User, error) { } return user, nil } + +func DeductUserPoints(tx *gorm.DB, user *modeles.User, point int) error { + newPoints := user.Scope - conf.GlobalConfig.AI.CreateRoleScope + if newPoints < 0 { + return errors.New("积分不足") + } + // 开启数据库事务 + if err := tx.Model(user).Update("scope", newPoints).Error; err != nil { + return err + } + return nil +} diff --git a/internal/middleware/auth.go b/internal/middleware/auth.go index f6c6755..a7bed15 100644 --- a/internal/middleware/auth.go +++ b/internal/middleware/auth.go @@ -10,8 +10,8 @@ import ( func AuthToken() fiber.Handler { return func(c fiber.Ctx) error { - log.Info().Msg("Test") token := c.Get("Authorization") + log.Info().Msg(token) if token == "" { return res.FailWithMsg(c, "未携带 Token 请先登录") } @@ -20,7 +20,7 @@ func AuthToken() fiber.Handler { return res.FailWithMsg(c, "Token 解析失败") } c.Locals("claims", claims.PayLoad) - + log.Info().Msg("Token 认证通过") return c.Next() } } diff --git a/internal/routers/aiRoleRouter.go b/internal/routers/aiRoleRouter.go index f3c102e..597b275 100644 --- a/internal/routers/aiRoleRouter.go +++ b/internal/routers/aiRoleRouter.go @@ -2,10 +2,11 @@ package routers import ( "AI_Server/internal/api/aiRole" + "AI_Server/internal/middleware" "github.com/gofiber/fiber/v3" ) func AiRoleRouter(r fiber.Router) { app := aiRole.AiRoleApi{} - r.Post("/ai/role/create", app.RoleCreate) + r.Post("/ai/role/create", app.RoleCreate, middleware.AuthToken()) } diff --git a/internal/routers/routers.go b/internal/routers/routers.go index 5ccf333..6bd3c20 100644 --- a/internal/routers/routers.go +++ b/internal/routers/routers.go @@ -18,6 +18,7 @@ func InitRouters() { SettingRouter(apiRouter) UserRouter(apiRouter) + AiRoleRouter(apiRouter) log.Fatal().Err(app.Listen(conf.GlobalConfig.Server.Http.Addr, fiber.ListenConfig{ EnablePrefork: false,