Skip to content

Commit

Permalink
完善用户编辑
Browse files Browse the repository at this point in the history
  • Loading branch information
bbbbbbbbbbbbba committed Feb 24, 2024
1 parent 42d6d8b commit f8a4b2d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 32 deletions.
5 changes: 2 additions & 3 deletions admin/src/views/pages/user/components/Edit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
</a-form-item>

<a-form-item label="角色" field="roles">
<a-select placeholder="请选择角色">
<a-select v-model="form.roleIds" multiple placeholder="请选择角色">
<a-option
v-for="role in roles"
:key="role.id"
Expand Down Expand Up @@ -86,8 +86,7 @@
// birthday: undefined,
homePage: undefined,
description: undefined,
roles: undefined,
roleIds: undefined,
});
const roles = ref();
Expand Down
30 changes: 17 additions & 13 deletions server/internal/controllers/admin/user_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bbs-go/internal/models/constants"
"bbs-go/internal/pkg/errs"
"strconv"
"strings"

"bbs-go/internal/models"

Expand Down Expand Up @@ -32,7 +31,7 @@ func (c *UserController) GetBy(id int64) *web.JsonResult {
if t == nil {
return web.JsonErrorMsg("Not found, id=" + strconv.FormatInt(id, 10))
}
return web.JsonData(c.buildUserItem(t))
return web.JsonData(c.buildUserItem(t, true))
}

func (c *UserController) AnyList() *web.JsonResult {
Expand All @@ -44,7 +43,7 @@ func (c *UserController) AnyList() *web.JsonResult {
PageByReq().Desc("id"))
var itemList []map[string]interface{}
for _, user := range list {
itemList = append(itemList, c.buildUserItem(&user))
itemList = append(itemList, c.buildUserItem(&user, false))
}
return web.JsonData(&web.PageResult{Results: itemList, Page: paging})
}
Expand All @@ -59,7 +58,7 @@ func (c *UserController) PostCreate() *web.JsonResult {
if err != nil {
return web.JsonError(err)
}
return web.JsonData(c.buildUserItem(user))
return web.JsonData(c.buildUserItem(user, true))
}

func (c *UserController) PostUpdate() *web.JsonResult {
Expand All @@ -73,7 +72,7 @@ func (c *UserController) PostUpdate() *web.JsonResult {
gender = params.FormValue(c.Ctx, "gender")
homePage = params.FormValue(c.Ctx, "homePage")
description = params.FormValue(c.Ctx, "description")
roles = params.FormValueStringArray(c.Ctx, "roles")
roleIds = params.FormValueInt64Array(c.Ctx, "roleIds")
status = params.FormValueIntDefault(c.Ctx, "status", 0)
)

Expand All @@ -90,14 +89,16 @@ func (c *UserController) PostUpdate() *web.JsonResult {
user.Gender = constants.Gender(gender)
user.HomePage = homePage
user.Description = description
user.Roles = strings.Join(roles, ",")
user.Status = status

err := services.UserService.Update(user)
if err != nil {
if err := services.UserService.Update(user); err != nil {
return web.JsonError(err)
}
if err := services.UserRoleService.UpdateUserRoles(user.Id, roleIds); err != nil {
return web.JsonError(err)
}
return web.JsonData(c.buildUserItem(user))
user = services.UserService.Get(user.Id)
return web.JsonData(c.buildUserItem(user, true))
}

// 禁言
Expand Down Expand Up @@ -127,12 +128,15 @@ func (c *UserController) PostForbidden() *web.JsonResult {
return web.JsonSuccess()
}

func (c *UserController) buildUserItem(user *models.User) map[string]interface{} {
return web.NewRspBuilder(user).
func (c *UserController) buildUserItem(user *models.User, buildRoleIds bool) map[string]interface{} {
b := web.NewRspBuilder(user).
Put("roles", user.GetRoles()).
Put("username", user.Username.String).
Put("email", user.Email.String).
Put("score", user.Score).
Put("forbidden", user.IsForbidden()).
Build()
Put("forbidden", user.IsForbidden())
if buildRoleIds {
b.Put("roleIds", services.UserRoleService.GetUserRoleIds(user.Id))
}
return b.Build()
}
57 changes: 41 additions & 16 deletions server/internal/services/user_role_service.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package services

import (
"bbs-go/internal/cache"
"bbs-go/internal/models"
"bbs-go/internal/repositories"
"strings"

"github.com/mlogclub/simple/common/dates"
"github.com/mlogclub/simple/sqls"
"github.com/mlogclub/simple/web/params"
"gorm.io/gorm"
)

var UserRoleService = newUserRoleService()
Expand Down Expand Up @@ -45,26 +49,47 @@ func (s *userRoleService) Count(cnd *sqls.Cnd) int64 {
return repositories.UserRoleRepository.Count(sqls.DB(), cnd)
}

func (s *userRoleService) Create(t *models.UserRole) error {
return repositories.UserRoleRepository.Create(sqls.DB(), t)
}

func (s *userRoleService) Update(t *models.UserRole) error {
return repositories.UserRoleRepository.Update(sqls.DB(), t)
}

func (s *userRoleService) Updates(id int64, columns map[string]interface{}) error {
return repositories.UserRoleRepository.Updates(sqls.DB(), id, columns)
}

func (s *userRoleService) UpdateColumn(id int64, name string, value interface{}) error {
return repositories.UserRoleRepository.UpdateColumn(sqls.DB(), id, name, value)
}

func (s *userRoleService) Delete(id int64) {
repositories.UserRoleRepository.Delete(sqls.DB(), id)
}

func (s *userRoleService) UpdateUserRoles(userId int64, roleIds []int64) error {
err := sqls.DB().Transaction(func(tx *gorm.DB) error {
var roles []models.Role
if len(roleIds) > 0 {
roles = repositories.RoleRepository.Find(tx, sqls.NewCnd().In("id", roleIds))
}

var roleCodes []string
for _, role := range roles {
roleCodes = append(roleCodes, role.Code)
}

if err := tx.Delete(&models.UserRole{}, "user_id = ?", userId).Error; err != nil {
return err
}
if len(roles) == 0 {
return repositories.UserRepository.UpdateColumn(tx, userId, "roles", "")
} else {
for _, role := range roles {
if err := repositories.UserRoleRepository.Create(tx, &models.UserRole{
UserId: userId,
RoleId: role.Id,
CreateTime: dates.NowTimestamp(),
}); err != nil {
return err
}
}
return repositories.UserRepository.UpdateColumn(tx, userId, "roles", strings.Join(roleCodes, ","))
}
})
if err != nil {
return err
}
cache.UserCache.Invalidate(userId)
return nil
}

func (s *userRoleService) GetUserRoleIds(userId int64) (roleIds []int64) {
list := s.Find(sqls.NewCnd().Eq("user_id", userId))
for _, userRole := range list {
Expand Down

0 comments on commit f8a4b2d

Please sign in to comment.