优化结构
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"kra/internal/biz/system"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -62,9 +63,9 @@ func (r *userRepo) loadUser(ctx context.Context, po *userPO) (*system.User, erro
|
||||
for _, item := range authorityPOs {
|
||||
authorities = append(authorities, toBizAuthority(item))
|
||||
}
|
||||
setting := map[string]any(nil)
|
||||
if po.OriginSetting != "" {
|
||||
_ = json.Unmarshal([]byte(po.OriginSetting), &setting)
|
||||
setting, err := parseOriginSetting(po.OriginSetting)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var departmentPOs []departmentPO
|
||||
if err := r.data.DB().WithContext(ctx).Table("sys_departments").Joins("JOIN sys_user_departments ON sys_user_departments.sys_department_id = sys_departments.id").Where("sys_user_departments.sys_user_id = ? AND sys_departments.deleted_at IS NULL", po.ID).Find(&departmentPOs).Error; err != nil {
|
||||
@@ -104,12 +105,23 @@ func toBizAuthority(po authorityPO) system.Authority {
|
||||
return system.Authority{CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, DeletedAt: deletedAt, AuthorityID: po.AuthorityID, AuthorityName: po.AuthorityName, ParentID: po.ParentID, DataScope: po.DataScope, DefaultRouter: po.DefaultRouter}
|
||||
}
|
||||
|
||||
func baseBizUser(po *userPO) *system.User {
|
||||
func parseOriginSetting(raw string) (map[string]any, error) {
|
||||
setting := map[string]any(nil)
|
||||
if po.OriginSetting != "" {
|
||||
_ = json.Unmarshal([]byte(po.OriginSetting), &setting)
|
||||
if raw == "" {
|
||||
return setting, nil
|
||||
}
|
||||
return &system.User{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, UUID: po.UUID, Username: po.Username, Password: po.Password, NickName: po.NickName, HeaderImg: po.HeaderImg, AuthorityID: po.AuthorityID, DeptID: po.DeptID, Phone: po.Phone, Email: po.Email, Enable: po.Enable, OriginSetting: setting, MustChangePassword: po.MustChangePassword, PasswordUpdatedAt: po.PasswordUpdatedAt}
|
||||
if err := json.Unmarshal([]byte(raw), &setting); err != nil {
|
||||
return nil, fmt.Errorf("解析用户设置: %w", err)
|
||||
}
|
||||
return setting, nil
|
||||
}
|
||||
|
||||
func baseBizUser(po *userPO) (*system.User, error) {
|
||||
setting, err := parseOriginSetting(po.OriginSetting)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &system.User{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, UUID: po.UUID, Username: po.Username, Password: po.Password, NickName: po.NickName, HeaderImg: po.HeaderImg, AuthorityID: po.AuthorityID, DeptID: po.DeptID, Phone: po.Phone, Email: po.Email, Enable: po.Enable, OriginSetting: setting, MustChangePassword: po.MustChangePassword, PasswordUpdatedAt: po.PasswordUpdatedAt}, nil
|
||||
}
|
||||
|
||||
func (r *userRepo) loadUsers(ctx context.Context, pos []userPO) ([]*system.User, error) {
|
||||
@@ -189,7 +201,10 @@ func (r *userRepo) loadUsers(ctx context.Context, pos []userPO) ([]*system.User,
|
||||
users := make([]*system.User, 0, len(pos))
|
||||
for i := range pos {
|
||||
po := &pos[i]
|
||||
user := baseBizUser(po)
|
||||
user, err := baseBizUser(po)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Authority = toBizAuthority(authorityByID[po.AuthorityID])
|
||||
user.Authorities = make([]system.Authority, 0, len(authorityByUser[po.ID]))
|
||||
for _, id := range authorityByUser[po.ID] {
|
||||
@@ -376,7 +391,10 @@ func (r *userRepo) CreateUserWithAuthorities(ctx context.Context, user *system.U
|
||||
// query. The reference handler returns the request's authority IDs and
|
||||
// leaves related authority/department records at their zero values; keep
|
||||
// that response contract while retaining the persisted timestamps/UUID.
|
||||
created := baseBizUser(&po)
|
||||
created, err := baseBizUser(&po)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
created.Authorities = createdAuthorities
|
||||
return created, nil
|
||||
}
|
||||
@@ -521,16 +539,7 @@ func (r *userRepo) UpdatePassword(ctx context.Context, id uint, password string,
|
||||
return r.data.DB().WithContext(ctx).Model(&userPO{}).Where("id = ?", id).Updates(updates).Error
|
||||
}
|
||||
func (r *userRepo) ListAuthorities(ctx context.Context) ([]*system.Authority, error) {
|
||||
var pos []authorityPO
|
||||
if err := r.data.DB().WithContext(ctx).Order("authority_id").Find(&pos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := make([]*system.Authority, 0, len(pos))
|
||||
for _, po := range pos {
|
||||
a := toBizAuthority(po)
|
||||
result = append(result, &a)
|
||||
}
|
||||
return result, nil
|
||||
return (&authorityAccessRepo{data: r.data}).ListAuthorities(ctx)
|
||||
}
|
||||
|
||||
func (r *userRepo) SetUserAuthorities(ctx context.Context, id uint, authorityIDs []uint) error {
|
||||
|
||||
Reference in New Issue
Block a user