userService.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package service
  2. import (
  3. "crypto/sha1"
  4. "fmt"
  5. "io"
  6. "iot_manager_service/app/system/dao"
  7. "iot_manager_service/app/system/model"
  8. "iot_manager_service/util"
  9. "time"
  10. )
  11. // 用户管理服务
  12. var UserService = new(userService)
  13. type userService struct{}
  14. func (s *userService) Get(id int64) (*model.UserDetail, error) {
  15. user := &dao.User{
  16. ID: id,
  17. }
  18. err := user.GetUser()
  19. if err != nil {
  20. return nil, err
  21. }
  22. detail := &model.UserDetail{
  23. User: *user,
  24. TenantName: util.GetTenantName(user.TenantId, user.TenantId),
  25. RoleName: util.GetRoleName(user.TenantId, user.RoleId),
  26. SexName: util.GetSexName(user.TenantId, user.Sex),
  27. }
  28. return detail, nil
  29. }
  30. func (s *userService) GetOneByTenantId(tenantId int) *dao.User {
  31. user := &dao.User{
  32. TenantId: tenantId,
  33. }
  34. err := user.GetUserByTenantId()
  35. if err != nil {
  36. return nil
  37. }
  38. return user
  39. }
  40. func (s *userService) GetOne(tenantId int, account, password string) *dao.User {
  41. // 密码前端先MD5 后端再sha1加密
  42. t := sha1.New()
  43. _, _ = io.WriteString(t, password)
  44. password = fmt.Sprintf("%x", t.Sum(nil))
  45. user := &dao.User{
  46. TenantId: tenantId,
  47. Account: account,
  48. Password: password,
  49. }
  50. err := user.GetUserByPwd()
  51. if err != nil {
  52. return nil
  53. }
  54. return user
  55. }
  56. func (s *userService) List(account, realName string, current, size int) ([]model.UserDetail, *util.Errors) {
  57. user := dao.User{}
  58. offset := (current - 1) * size
  59. limit := size
  60. if account != "" {
  61. user.Account = account
  62. }
  63. if realName != "" {
  64. user.RealName = realName
  65. }
  66. users, err := user.GetUsers(offset, limit)
  67. if err != nil {
  68. return nil, util.FailResponse(err.Error(), nil)
  69. }
  70. var details []model.UserDetail
  71. for _, user := range users {
  72. details = append(details, model.UserDetail{
  73. User: user,
  74. TenantName: util.GetTenantName(user.TenantId, user.TenantId),
  75. RoleName: util.GetRoleName(user.TenantId, user.RoleId),
  76. SexName: util.GetSexName(user.TenantId, user.Sex),
  77. })
  78. }
  79. return details, nil
  80. }
  81. func (s *userService) Submit(req dao.User) *util.Errors {
  82. user := &req
  83. err := user.Save()
  84. if err != nil {
  85. return util.FailResponse(err.Error(), nil)
  86. }
  87. return nil
  88. }
  89. func (s *userService) Update(req dao.User) *util.Errors {
  90. user := &req
  91. err := user.Update()
  92. if err != nil {
  93. return util.FailResponse(err.Error(), nil)
  94. }
  95. return nil
  96. }
  97. func (s *userService) Remove(id int64) *util.Errors {
  98. user := &dao.User{
  99. ID: id,
  100. IsDeleted: 1,
  101. UpdateUser: id, // todo 使用登录态
  102. UpdateTime: time.Now(),
  103. }
  104. err := user.Remove()
  105. if err != nil {
  106. return util.FailResponse(err.Error(), nil)
  107. }
  108. return nil
  109. }
  110. func (s *userService) ResetPwd(id int64) *util.Errors {
  111. user := &dao.User{ID: id}
  112. err := user.UpdatePwd(model.DefaultPwd)
  113. if err != nil {
  114. return util.FailResponse(err.Error(), nil)
  115. }
  116. return nil
  117. }
  118. func (s *userService) GetList() ([]dao.User, error) {
  119. user := &dao.User{}
  120. users, err := user.GetAll()
  121. if err != nil {
  122. return nil, err
  123. }
  124. return users, nil
  125. }
  126. func (s *userService) Grant(userIds []string, roleIds string) error {
  127. user := &dao.User{}
  128. return user.UpdateRoles(userIds, roleIds)
  129. }