userService.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. )
  10. // 用户管理服务
  11. var UserService = new(userService)
  12. type userService struct{}
  13. func (s *userService) Get(id int64) (*model.UserDetail, error) {
  14. user := &dao.User{
  15. ID: id,
  16. }
  17. err := user.GetUser()
  18. if err != nil {
  19. return nil, err
  20. }
  21. detail := &model.UserDetail{
  22. User: *user,
  23. TenantName: util.GetTenantName(user.TenantId, user.TenantId),
  24. RoleName: util.GetRoleNames(user.TenantId, user.RoleId),
  25. DeptName: "",
  26. PostName: "",
  27. SexName: util.GetSexName(user.TenantId, user.Sex),
  28. }
  29. return detail, nil
  30. }
  31. func (s *userService) GetOne(tenantId, account, password string) *dao.User {
  32. // 密码前端先MD5 后端再sha1加密
  33. t := sha1.New()
  34. _, _ = io.WriteString(t, password)
  35. password = fmt.Sprintf("%x", t.Sum(nil))
  36. user := &dao.User{
  37. TenantId: tenantId,
  38. Account: account,
  39. Password: password,
  40. }
  41. err := user.GetUserByPwd()
  42. if err != nil {
  43. return nil
  44. }
  45. return user
  46. }
  47. func (s *userService) List(account, realName string, current, size int) ([]model.UserDetail, *util.Errors) {
  48. user := dao.User{}
  49. offset := (current - 1) * size
  50. limit := size
  51. if account != "" {
  52. user.Account = account
  53. }
  54. if realName != "" {
  55. user.RealName = realName
  56. }
  57. users, err := user.GetUsers(offset, limit)
  58. if err != nil {
  59. return nil, util.FailResponse(err.Error(), nil)
  60. }
  61. var details []model.UserDetail
  62. for _, user := range users {
  63. details = append(details, model.UserDetail{
  64. User: user,
  65. TenantName: util.GetTenantName(user.TenantId, user.TenantId),
  66. RoleName: util.GetRoleNames(user.TenantId, user.RoleId),
  67. DeptName: "", //智慧路灯 ?
  68. PostName: "",
  69. SexName: util.GetSexName(user.TenantId, user.Sex),
  70. })
  71. }
  72. return details, nil
  73. }
  74. func (s *userService) Submit(req dao.User) *util.Errors {
  75. user := &req
  76. err := user.Save()
  77. if err != nil {
  78. return util.FailResponse(err.Error(), nil)
  79. }
  80. return nil
  81. }
  82. func (s *userService) Update(req dao.User) *util.Errors {
  83. user := &req
  84. err := user.Update()
  85. if err != nil {
  86. return util.FailResponse(err.Error(), nil)
  87. }
  88. return nil
  89. }
  90. func (s *userService) Remove(id int64) *util.Errors {
  91. user := &dao.User{ID: id}
  92. err := user.Remove()
  93. if err != nil {
  94. return util.FailResponse(err.Error(), nil)
  95. }
  96. return nil
  97. }