package service import ( "crypto/sha1" "fmt" "io" "iot_manager_service/app/system/dao" "iot_manager_service/app/system/model" "iot_manager_service/util" "time" ) // 用户管理服务 var UserService = new(userService) type userService struct{} func (s *userService) Get(id int64) (*model.UserDetail, error) { user := &dao.User{ ID: id, } err := user.GetUser() if err != nil { return nil, err } detail := &model.UserDetail{ User: *user, TenantName: util.GetTenantName(user.TenantId, user.TenantId), RoleName: util.GetRoleName(user.TenantId, user.RoleId), SexName: util.GetSexName(user.TenantId, user.Sex), } return detail, nil } func (s *userService) GetOneByTenantId(tenantId int) *dao.User { user := &dao.User{ TenantId: tenantId, } err := user.GetUserByTenantId() if err != nil { return nil } return user } func (s *userService) GetOne(tenantId int, account, password string) *dao.User { // 密码前端先MD5 后端再sha1加密 t := sha1.New() _, _ = io.WriteString(t, password) password = fmt.Sprintf("%x", t.Sum(nil)) user := &dao.User{ TenantId: tenantId, Account: account, Password: password, } err := user.GetUserByPwd() if err != nil { return nil } return user } func (s *userService) List(account, realName string, current, size int) ([]model.UserDetail, *util.Errors) { user := dao.User{} offset := (current - 1) * size limit := size if account != "" { user.Account = account } if realName != "" { user.RealName = realName } users, err := user.GetUsers(offset, limit) if err != nil { return nil, util.FailResponse(err.Error(), nil) } var details []model.UserDetail for _, user := range users { details = append(details, model.UserDetail{ User: user, TenantName: util.GetTenantName(user.TenantId, user.TenantId), RoleName: util.GetRoleName(user.TenantId, user.RoleId), SexName: util.GetSexName(user.TenantId, user.Sex), }) } return details, nil } func (s *userService) Submit(req dao.User) *util.Errors { user := &req err := user.Save() if err != nil { return util.FailResponse(err.Error(), nil) } return nil } func (s *userService) Update(req dao.User) *util.Errors { user := &req err := user.Update() if err != nil { return util.FailResponse(err.Error(), nil) } return nil } func (s *userService) Remove(id int64) *util.Errors { user := &dao.User{ ID: id, IsDeleted: 1, UpdateUser: id, // todo 使用登录态 UpdateTime: time.Now(), } err := user.Remove() if err != nil { return util.FailResponse(err.Error(), nil) } return nil } func (s *userService) ResetPwd(id int64) *util.Errors { user := &dao.User{ID: id} err := user.UpdatePwd(model.DefaultPwd) if err != nil { return util.FailResponse(err.Error(), nil) } return nil } func (s *userService) GetList() ([]dao.User, error) { user := &dao.User{} users, err := user.GetAll() if err != nil { return nil, err } return users, nil } func (s *userService) Grant(userIds []string, roleIds string) error { user := &dao.User{} return user.UpdateRoles(userIds, roleIds) }