192 lines
8.3 KiB
Go
192 lines
8.3 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
func (s *SystemService) User(ctx context.Context, id uint) (map[string]any, error) {
|
|
value, err := s.uc.User(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return convertUser(value), nil
|
|
}
|
|
|
|
func (s *SystemService) Menus(ctx context.Context, authorityID uint) ([]map[string]any, error) {
|
|
menus, err := s.uc.Menus(ctx, authorityID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]map[string]any, 0, len(menus))
|
|
for _, menu := range menus {
|
|
result = append(result, convertMenu(menu))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
type UserInput struct {
|
|
ID uint
|
|
Username, Password, NickName, HeaderImg, Phone, Email string
|
|
AuthorityID uint
|
|
Enable int
|
|
AuthorityIDs []uint
|
|
}
|
|
|
|
func userInput(value *dto.UserRequest) UserInput {
|
|
return UserInput{ID: value.ID, Username: value.Username, Password: value.Password, NickName: value.NickName, HeaderImg: value.HeaderImg, AuthorityID: value.AuthorityID, AuthorityIDs: value.AuthorityIDs, Enable: value.Enable, Phone: value.Phone, Email: value.Email}
|
|
}
|
|
func (s *SystemService) ListUsersRequest(ctx context.Context, value *dto.UserListRequest) ([]map[string]any, int64, error) {
|
|
return s.ListUsers(ctx, value.Page, value.PageSize, &biz.UserListFilter{Username: value.Username, NickName: value.NickName, Phone: value.Phone, Email: value.Email, OrderKey: value.OrderKey, Desc: value.Desc})
|
|
}
|
|
func (s *SystemService) CreateUserRequest(ctx context.Context, value *dto.UserRequest) (map[string]any, error) {
|
|
return s.CreateUser(ctx, userInput(value))
|
|
}
|
|
func (s *SystemService) UpdateUserRequest(ctx context.Context, value *dto.UserRequest) error {
|
|
return s.UpdateUser(ctx, userInput(value))
|
|
}
|
|
|
|
func (s *SystemService) ListUsers(ctx context.Context, page, pageSize int, filter *biz.UserListFilter) ([]map[string]any, int64, error) {
|
|
users, total, err := s.uc.ListUsers(ctx, page, pageSize, filter)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
result := make([]map[string]any, 0, len(users))
|
|
for _, user := range users {
|
|
result = append(result, convertUser(user))
|
|
}
|
|
return result, total, nil
|
|
}
|
|
func (s *SystemService) CreateUser(ctx context.Context, input UserInput) (map[string]any, error) {
|
|
if err := s.settings.ValidatePassword(ctx, input.Password); err != nil {
|
|
return nil, err
|
|
}
|
|
security, _ := s.settings.CurrentSecurity(ctx)
|
|
mustChange := security != nil && security.ForceNewUserChangePassword
|
|
if input.AuthorityID == 0 && len(input.AuthorityIDs) > 0 {
|
|
input.AuthorityID = input.AuthorityIDs[0]
|
|
}
|
|
input.AuthorityIDs = includeAuthority(input.AuthorityIDs, input.AuthorityID)
|
|
user, err := s.uc.CreateUser(ctx, &biz.User{Username: input.Username, Password: input.Password, NickName: input.NickName, HeaderImg: input.HeaderImg, AuthorityID: input.AuthorityID, Phone: input.Phone, Email: input.Email, Enable: input.Enable, MustChangePassword: mustChange})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(input.AuthorityIDs) > 0 {
|
|
if err = s.uc.SetUserAuthorities(ctx, user.ID, input.AuthorityIDs); err != nil {
|
|
return nil, err
|
|
}
|
|
user, err = s.uc.User(ctx, user.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return convertUser(user), nil
|
|
}
|
|
func (s *SystemService) UpdateUser(ctx context.Context, input UserInput) error {
|
|
input.AuthorityIDs = includeAuthority(input.AuthorityIDs, input.AuthorityID)
|
|
if err := s.uc.UpdateUser(ctx, &biz.User{ID: input.ID, NickName: input.NickName, HeaderImg: input.HeaderImg, AuthorityID: input.AuthorityID, Phone: input.Phone, Email: input.Email, Enable: input.Enable}); err != nil {
|
|
return err
|
|
}
|
|
if len(input.AuthorityIDs) > 0 {
|
|
return s.uc.SetUserAuthorities(ctx, input.ID, input.AuthorityIDs)
|
|
}
|
|
return nil
|
|
}
|
|
func includeAuthority(ids []uint, primary uint) []uint {
|
|
if primary == 0 {
|
|
return ids
|
|
}
|
|
for _, id := range ids {
|
|
if id == primary {
|
|
return ids
|
|
}
|
|
}
|
|
return append(ids, primary)
|
|
}
|
|
func (s *SystemService) DeleteUser(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteUser(ctx, id)
|
|
}
|
|
func (s *SystemService) ResetPassword(ctx context.Context, id uint, password string) error {
|
|
if err := s.settings.ValidatePassword(ctx, password); err != nil {
|
|
return err
|
|
}
|
|
return s.uc.ResetPassword(ctx, id, password)
|
|
}
|
|
func (s *SystemService) ChangePassword(ctx context.Context, id uint, oldPassword, newPassword string) error {
|
|
if err := s.settings.ValidatePassword(ctx, newPassword); err != nil {
|
|
return err
|
|
}
|
|
return s.uc.ChangePassword(ctx, id, oldPassword, newPassword)
|
|
}
|
|
func (s *SystemService) Authorities(ctx context.Context) ([]map[string]any, error) {
|
|
values, err := s.uc.Authorities(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]map[string]any, 0, len(values))
|
|
for _, value := range values {
|
|
result = append(result, convertAuthority(*value))
|
|
}
|
|
return result, nil
|
|
}
|
|
func (s *SystemService) SetUserAuthorities(ctx context.Context, id uint, authorityIDs []uint) error {
|
|
return s.uc.SetUserAuthorities(ctx, id, authorityIDs)
|
|
}
|
|
func (s *SystemService) SwitchAuthority(ctx context.Context, id, authorityID uint) (*LoginResult, error) {
|
|
if err := s.uc.SetUserAuthority(ctx, id, authorityID); err != nil {
|
|
return nil, err
|
|
}
|
|
user, err := s.uc.User(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return s.issueLogin(user, authorityID)
|
|
}
|
|
func (s *SystemService) SetUserSetting(ctx context.Context, id uint, setting map[string]any) error {
|
|
return s.uc.SetUserSetting(ctx, id, setting)
|
|
}
|
|
|
|
func convertAuthority(value biz.Authority) map[string]any {
|
|
return map[string]any{"authorityId": value.AuthorityID, "authorityName": value.AuthorityName, "parentId": value.ParentID, "children": []map[string]any{}, "menus": []any{}, "dataScope": value.DataScope, "defaultRouter": value.DefaultRouter}
|
|
}
|
|
func convertUser(user *biz.User) map[string]any {
|
|
authorities := make([]map[string]any, 0, len(user.Authorities))
|
|
for _, value := range user.Authorities {
|
|
authorities = append(authorities, convertAuthority(value))
|
|
}
|
|
departments := make([]map[string]any, 0, len(user.Departments))
|
|
for i := range user.Departments {
|
|
departments = append(departments, departmentDTO(&user.Departments[i]))
|
|
}
|
|
positions := make([]map[string]any, 0, len(user.Positions))
|
|
for i := range user.Positions {
|
|
positions = append(positions, positionDTO(&user.Positions[i]))
|
|
}
|
|
department := map[string]any{}
|
|
if user.Department != nil {
|
|
department = departmentDTO(user.Department)
|
|
}
|
|
return map[string]any{"ID": user.ID, "CreatedAt": user.CreatedAt, "UpdatedAt": user.UpdatedAt, "DeletedAt": nil, "uuid": user.UUID, "userName": user.Username, "nickName": user.NickName, "headerImg": user.HeaderImg, "authorityId": user.AuthorityID, "authority": convertAuthority(user.Authority), "authorities": authorities, "deptId": user.DeptID, "dept": department, "departments": departments, "positions": positions, "phone": user.Phone, "email": user.Email, "enable": user.Enable, "originSetting": user.OriginSetting}
|
|
}
|
|
func convertMenu(menu *biz.Menu) map[string]any {
|
|
children := make([]map[string]any, 0, len(menu.Children))
|
|
for _, child := range menu.Children {
|
|
children = append(children, convertMenu(child))
|
|
}
|
|
buttons := make([]map[string]any, 0, len(menu.Buttons))
|
|
buttonAuthorities := make(map[string]uint, len(menu.Buttons))
|
|
for _, button := range menu.Buttons {
|
|
buttons = append(buttons, map[string]any{"ID": button.ID, "name": button.Name, "desc": button.Description, "sysBaseMenuID": button.MenuID})
|
|
if button.AuthorityID != 0 {
|
|
buttonAuthorities[button.Name] = button.AuthorityID
|
|
}
|
|
}
|
|
parameters := make([]map[string]any, 0, len(menu.Parameters))
|
|
for _, parameter := range menu.Parameters {
|
|
parameters = append(parameters, map[string]any{"ID": parameter.ID, "SysBaseMenuID": parameter.MenuID, "type": parameter.Type, "key": parameter.Key, "value": parameter.Value})
|
|
}
|
|
return map[string]any{"ID": menu.ID, "parentId": menu.ParentID, "path": menu.Path, "name": menu.Name, "hidden": menu.Hidden, "component": menu.Component, "sort": menu.Sort, "meta": map[string]any{"activeName": menu.ActiveName, "keepAlive": menu.KeepAlive, "defaultMenu": menu.DefaultMenu, "title": menu.Title, "icon": menu.Icon, "closeTab": menu.CloseTab, "transitionType": menu.TransitionType}, "children": children, "parameters": parameters, "menuBtn": buttons, "btns": buttonAuthorities}
|
|
}
|