userService.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.GetRoleNames(user.TenantId, user.RoleId),
  26. DeptName: "",
  27. PostName: "",
  28. SexName: util.GetSexName(user.TenantId, user.Sex),
  29. }
  30. return detail, nil
  31. }
  32. func (s *userService) GetOne(tenantId, account, password string) *dao.User {
  33. // 密码前端先MD5 后端再sha1加密
  34. t := sha1.New()
  35. _, _ = io.WriteString(t, password)
  36. password = fmt.Sprintf("%x", t.Sum(nil))
  37. user := &dao.User{
  38. TenantId: tenantId,
  39. Account: account,
  40. Password: password,
  41. }
  42. err := user.GetUserByPwd()
  43. if err != nil {
  44. return nil
  45. }
  46. return user
  47. }
  48. func (s *userService) List(account, realName string, current, size int) ([]model.UserDetail, *util.Errors) {
  49. user := dao.User{}
  50. offset := (current - 1) * size
  51. limit := size
  52. if account != "" {
  53. user.Account = account
  54. }
  55. if realName != "" {
  56. user.RealName = realName
  57. }
  58. users, err := user.GetUsers(offset, limit)
  59. if err != nil {
  60. return nil, util.FailResponse(err.Error(), nil)
  61. }
  62. var details []model.UserDetail
  63. for _, user := range users {
  64. details = append(details, model.UserDetail{
  65. User: user,
  66. TenantName: util.GetTenantName(user.TenantId, user.TenantId),
  67. RoleName: util.GetRoleNames(user.TenantId, user.RoleId),
  68. DeptName: "", //智慧路灯 ?
  69. PostName: "",
  70. SexName: util.GetSexName(user.TenantId, user.Sex),
  71. })
  72. }
  73. return details, nil
  74. }
  75. func (s *userService) Submit(req dao.User) *util.Errors {
  76. user := &req
  77. err := user.Save()
  78. if err != nil {
  79. return util.FailResponse(err.Error(), nil)
  80. }
  81. return nil
  82. }
  83. func (s *userService) Update(req dao.User) *util.Errors {
  84. user := &req
  85. err := user.Update()
  86. if err != nil {
  87. return util.FailResponse(err.Error(), nil)
  88. }
  89. return nil
  90. }
  91. func (s *userService) Remove(id int64) *util.Errors {
  92. user := &dao.User{
  93. ID: id,
  94. IsDeleted: 1,
  95. UpdateUser: id, // todo 使用登录态
  96. UpdateTime: time.Now(),
  97. }
  98. err := user.Remove()
  99. if err != nil {
  100. return util.FailResponse(err.Error(), nil)
  101. }
  102. return nil
  103. }
  104. func (s *userService) ResetPwd(id int64) *util.Errors {
  105. user := &dao.User{ID: id}
  106. err := user.UpdatePwd(model.DefaultPwd)
  107. if err != nil {
  108. return util.FailResponse(err.Error(), nil)
  109. }
  110. return nil
  111. }
  112. func (s *userService) GetList() ([]dao.User, error) {
  113. user := &dao.User{}
  114. users, err := user.GetAll()
  115. if err != nil {
  116. return nil, err
  117. }
  118. return users, nil
  119. }
  120. func (s *userService) Grant(userIds []string, roleIds string) error {
  121. user := &dao.User{}
  122. return user.UpdateRoles(userIds, roleIds)
  123. }