Skip to content

Commit

Permalink
功能完善与细节调整
Browse files Browse the repository at this point in the history
  • Loading branch information
iimeta committed Jun 9, 2024
1 parent 4a93921 commit 1f7b568
Show file tree
Hide file tree
Showing 122 changed files with 194 additions and 67 deletions.
29 changes: 29 additions & 0 deletions internal/logic/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,32 @@ func (s *sApp) KeyConfig(ctx context.Context, params model.AppKeyConfigReq) (err

return nil
}

// 应用模型权限
func (s *sApp) Models(ctx context.Context, params model.AppModelsReq) error {

oldData, err := dao.App.FindOne(ctx, bson.M{"app_id": params.AppId})
if err != nil {
logger.Error(ctx, err)
return err
}

newData, err := dao.App.FindOneAndUpdate(ctx, bson.M{"app_id": params.AppId}, bson.M{
"models": params.Models,
})
if err != nil {
logger.Error(ctx, err)
return err
}

if _, err = redis.Publish(ctx, consts.CHANGE_CHANNEL_APP, model.PubMessage{
Action: consts.ACTION_MODELS,
OldData: oldData,
NewData: newData,
}); err != nil {
logger.Error(ctx, err)
return err
}

return nil
}
29 changes: 29 additions & 0 deletions internal/logic/key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,32 @@ func (s *sKey) DetailListByKey(ctx context.Context, keys []string) ([]*entity.Ke

return results, nil
}

// 密钥模型权限
func (s *sKey) Models(ctx context.Context, params model.KeyModelsReq) error {

oldData, err := dao.Key.FindById(ctx, params.Id)
if err != nil {
logger.Error(ctx, err)
return err
}

newData, err := dao.Key.FindOneAndUpdateById(ctx, params.Id, bson.M{
"models": params.Models,
})
if err != nil {
logger.Error(ctx, err)
return err
}

if _, err = redis.Publish(ctx, consts.CHANGE_CHANNEL_KEY, model.PubMessage{
Action: consts.ACTION_MODELS,
OldData: oldData,
NewData: newData,
}); err != nil {
logger.Error(ctx, err)
return err
}

return nil
}
91 changes: 72 additions & 19 deletions internal/logic/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,33 +371,81 @@ func (s *sModel) Delete(ctx context.Context, id string) error {
return err
}

if err = dao.User.UpdateMany(ctx, bson.M{"models": bson.M{"$in": []string{id}}}, bson.M{
"$pull": bson.M{
"models": id,
},
}); err != nil {
users, err := dao.User.Find(ctx, bson.M{"models": bson.M{"$in": []string{id}}})
if err != nil {
logger.Error(ctx, err)
return err
}

if err = dao.App.UpdateMany(ctx, bson.M{"models": bson.M{"$in": []string{id}}}, bson.M{
"$pull": bson.M{
"models": id,
},
}); err != nil {
for _, user := range users {

userModelsReq := model.UserModelsReq{
UserId: user.UserId,
Models: []string{},
}

for _, m := range user.Models {
if m != id {
userModelsReq.Models = append(userModelsReq.Models, m)
}
}

if err = service.AdminUser().Models(ctx, userModelsReq); err != nil {
logger.Error(ctx, err)
return err
}
}

apps, err := dao.App.Find(ctx, bson.M{"models": bson.M{"$in": []string{id}}})
if err != nil {
logger.Error(ctx, err)
return err
}

if err = dao.Key.UpdateMany(ctx, bson.M{"models": bson.M{"$in": []string{id}}}, bson.M{
"$pull": bson.M{
"models": id,
},
}); err != nil {
for _, app := range apps {

appModelsReq := model.AppModelsReq{
AppId: app.AppId,
Models: []string{},
}

for _, m := range app.Models {
if m != id {
appModelsReq.Models = append(appModelsReq.Models, m)
}
}

if err = service.App().Models(ctx, appModelsReq); err != nil {
logger.Error(ctx, err)
return err
}
}

keys, err := dao.Key.Find(ctx, bson.M{"models": bson.M{"$in": []string{id}}})
if err != nil {
logger.Error(ctx, err)
return err
}

for _, key := range keys {

keyModelsReq := model.KeyModelsReq{
Id: key.Id,
Models: []string{},
}

for _, m := range key.Models {
if m != id {
keyModelsReq.Models = append(keyModelsReq.Models, m)
}
}

if err = service.Key().Models(ctx, keyModelsReq); err != nil {
logger.Error(ctx, err)
return err
}
}

if _, err = redis.Publish(ctx, consts.CHANGE_CHANNEL_MODEL, model.PubMessage{
Action: consts.ACTION_DELETE,
OldData: oldData,
Expand Down Expand Up @@ -538,13 +586,18 @@ func (s *sModel) Page(ctx context.Context, params model.ModelPageReq) (*model.Mo

if service.Session().IsUserRole(ctx) {

models := service.Session().GetUser(ctx).Models
if len(models) == 0 {
user, err := service.User().GetUserByUserId(ctx, service.Session().GetUserId(ctx))
if err != nil {
logger.Error(ctx, err)
return nil, err
}

if len(user.Models) == 0 {
return nil, nil
}

filter["_id"] = bson.M{
"$in": models,
"$in": user.Models,
}
}

Expand Down Expand Up @@ -935,7 +988,7 @@ func (s *sModel) Init(ctx context.Context, params model.ModelInitReq) error {
return err
}

if len(result.Data) > 0 && result.Data[0].FastAPI == nil {
if result.Data == nil || (len(result.Data) > 0 && result.Data[0].FastAPI == nil) {
return errors.New("模型接口数据格式不支持, 请联系作者...")
}

Expand Down
6 changes: 6 additions & 0 deletions internal/model/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ type AppKeyConfigReq struct {
Status int `json:"status,omitempty" d:"1"` // 状态[1:正常, 2:禁用, -1:删除]
}

// 应用模型权限接口请求参数
type AppModelsReq struct {
AppId int `json:"app_id,omitempty"` // 应用ID
Models []string `json:"models,omitempty" d:"[]"` // 模型权限
}

type App struct {
Id string `json:"id,omitempty"` // ID
AppId int `json:"app_id,omitempty"` // 应用ID
Expand Down
6 changes: 6 additions & 0 deletions internal/model/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ type KeyBatchOperateReq struct {
Value any `json:"value"` // 值
}

// 密钥模型权限接口请求参数
type KeyModelsReq struct {
Id string `json:"id,omitempty"` // ID
Models []string `json:"models,omitempty" d:"[]"` // 模型权限
}

type Key struct {
Id string `json:"id,omitempty"` // ID
AppId int `json:"app_id,omitempty"` // 应用ID
Expand Down
2 changes: 2 additions & 0 deletions internal/service/app.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/service/key.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file removed resource/fastapi-web/assets/admin.d201d209.js.gz
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added resource/fastapi-web/assets/admin.fce5849f.js.gz
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1f7b568

Please sign in to comment.