userService.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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/cache"
  9. "iot_manager_service/util/common"
  10. "time"
  11. )
  12. // 用户管理服务
  13. var UserService = new(userService)
  14. type userService struct{}
  15. func (s *userService) Get(id int64) (*model.UserDetail, error) {
  16. user := &dao.User{
  17. ID: id,
  18. }
  19. err := user.GetUser()
  20. if err != nil {
  21. return nil, err
  22. }
  23. detail := &model.UserDetail{
  24. User: *user,
  25. TenantName: TenantService.GetTenantName(user.TenantId, user.TenantId),
  26. RoleName: RoleService.GetRoleName(user.TenantId, user.RoleId),
  27. SexName: DictService.GetSexName(user.TenantId, user.Sex),
  28. }
  29. return detail, nil
  30. }
  31. func (s *userService) GetOneByTenantId(tenantId int) *dao.User {
  32. user := &dao.User{
  33. TenantId: tenantId,
  34. }
  35. err := user.GetUserByTenantId()
  36. if err != nil {
  37. return nil
  38. }
  39. return user
  40. }
  41. func (s *userService) GetOne(tenantId int, account, password string) *dao.User {
  42. // 密码前端先MD5 后端再sha1加密
  43. t := sha1.New()
  44. _, _ = io.WriteString(t, password)
  45. password = fmt.Sprintf("%x", t.Sum(nil))
  46. user := &dao.User{
  47. TenantId: tenantId,
  48. Account: account,
  49. Password: password,
  50. }
  51. err := user.GetUserByPwd()
  52. if err != nil {
  53. return nil
  54. }
  55. return user
  56. }
  57. func (s *userService) List(account, realName string, current, size int) ([]model.UserDetail, *common.Errors) {
  58. user := dao.User{}
  59. offset := (current - 1) * size
  60. limit := size
  61. if account != "" {
  62. user.Account = account
  63. }
  64. if realName != "" {
  65. user.RealName = realName
  66. }
  67. users, err := user.GetUsers(offset, limit)
  68. if err != nil {
  69. return nil, common.FailResponse(err.Error(), nil)
  70. }
  71. var details []model.UserDetail
  72. for _, user := range users {
  73. details = append(details, model.UserDetail{
  74. User: user,
  75. TenantName: TenantService.GetTenantName(user.TenantId, user.TenantId),
  76. RoleName: RoleService.GetRoleName(user.TenantId, user.RoleId),
  77. SexName: DictService.GetSexName(user.TenantId, user.Sex),
  78. })
  79. }
  80. return details, nil
  81. }
  82. func (s *userService) Submit(req dao.User) *common.Errors {
  83. user := &req
  84. err := user.Save()
  85. if err != nil {
  86. return common.FailResponse(err.Error(), nil)
  87. }
  88. return nil
  89. }
  90. func (s *userService) Update(req dao.User) *common.Errors {
  91. user := &req
  92. err := user.Update()
  93. if err != nil {
  94. return common.FailResponse(err.Error(), nil)
  95. }
  96. return nil
  97. }
  98. func (s *userService) Remove(userId, id int64) *common.Errors {
  99. user := &dao.User{
  100. ID: id,
  101. IsDeleted: 1,
  102. UpdateUser: userId,
  103. UpdateTime: time.Now(),
  104. }
  105. err := user.Remove()
  106. if err != nil {
  107. return common.FailResponse(err.Error(), nil)
  108. }
  109. return nil
  110. }
  111. func (s *userService) ResetPwd(id int64) *common.Errors {
  112. user := &dao.User{ID: id}
  113. err := user.UpdatePwd(model.DefaultPwd)
  114. if err != nil {
  115. return common.FailResponse(err.Error(), nil)
  116. }
  117. return nil
  118. }
  119. func (s *userService) GetList() ([]dao.User, error) {
  120. user := &dao.User{}
  121. users, err := user.GetAll()
  122. if err != nil {
  123. return nil, err
  124. }
  125. return users, nil
  126. }
  127. func (s *userService) Grant(userIds []string, roleIds string) error {
  128. user := &dao.User{}
  129. return user.UpdateRoles(userIds, roleIds)
  130. }
  131. const UserKey = "%d:sys:user:%d"
  132. func (s *userService) GetUserName(tenantId int, id int64) string {
  133. var name string
  134. key := fmt.Sprintf(UserKey, tenantId, id)
  135. name, err := cache.Redis.Get(key).Result()
  136. if err != nil {
  137. user := &dao.User{ID: id}
  138. if err := user.GetUser(); err != nil {
  139. fmt.Printf("GetUser err = %s \n", err)
  140. } else {
  141. name = user.Name
  142. cache.Redis.Set(key, name, time.Second*10)
  143. }
  144. }
  145. return name
  146. }