userService.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/config"
  9. "iot_manager_service/util/cache"
  10. "iot_manager_service/util/common"
  11. "iot_manager_service/util/logger"
  12. "iot_manager_service/util/minio"
  13. "mime/multipart"
  14. "strings"
  15. "time"
  16. )
  17. // 用户管理服务
  18. var UserService = new(userService)
  19. type userService struct{}
  20. func (s *userService) Get(id int) (*model.UserDetail, error) {
  21. user := &dao.User{
  22. ID: id,
  23. }
  24. err := user.GetUser()
  25. if err != nil {
  26. return nil, err
  27. }
  28. detail := &model.UserDetail{
  29. User: *user,
  30. TenantName: TenantService.GetTenantName(user.TenantId, user.TenantId),
  31. RoleName: RoleService.GetRoleName(user.TenantId, user.RoleId),
  32. SexName: DictService.GetSexName(user.TenantId, user.Sex),
  33. }
  34. return detail, nil
  35. }
  36. func (s *userService) GetOneByTenantId(tenantId string) *dao.User {
  37. user := &dao.User{
  38. TenantId: tenantId,
  39. }
  40. err := user.GetUserByTenantId()
  41. if err != nil {
  42. return nil
  43. }
  44. return user
  45. }
  46. func (s *userService) GetOne(tenantId string, account, password string) *dao.User {
  47. // 密码前端先MD5 后端再sha1加密
  48. t := sha1.New()
  49. _, _ = io.WriteString(t, password)
  50. password = fmt.Sprintf("%x", t.Sum(nil))
  51. user := &dao.User{
  52. TenantId: tenantId,
  53. Account: account,
  54. Password: password,
  55. }
  56. err := user.GetUserByPwd()
  57. if err != nil {
  58. return nil
  59. }
  60. return user
  61. }
  62. func (s *userService) List(account, realName string, current, size int) ([]model.UserDetail, int, *common.Errors) {
  63. user := dao.User{}
  64. offset := (current - 1) * size
  65. limit := size
  66. if account != "" {
  67. user.Account = account
  68. }
  69. if realName != "" {
  70. user.RealName = realName
  71. }
  72. users, counts, err := user.GetUsers(offset, limit)
  73. if err != nil {
  74. return nil, 0, common.FailResponse(err.Error(), nil)
  75. }
  76. var details []model.UserDetail
  77. for _, user := range users {
  78. details = append(details, model.UserDetail{
  79. User: user,
  80. TenantName: TenantService.GetTenantName(user.TenantId, user.TenantId),
  81. RoleName: RoleService.GetRoleName(user.TenantId, user.RoleId),
  82. SexName: DictService.GetSexName(user.TenantId, user.Sex),
  83. })
  84. }
  85. return details, counts, nil
  86. }
  87. func (s *userService) Info(userInfo *dao.User) *common.Errors {
  88. err := userInfo.GetUser()
  89. if err != nil {
  90. return common.FailResponse(err.Error(), nil)
  91. }
  92. return nil
  93. }
  94. // Submit Birthday为""时在数据库报错,给一个默认值.
  95. func (s *userService) Submit(req dao.User) *common.Errors {
  96. user := &req
  97. if user.Birthday == "" {
  98. user.Birthday = "1900-01-01 00:00:00"
  99. }
  100. err := user.Save()
  101. if err != nil {
  102. return common.FailResponse(err.Error(), nil)
  103. }
  104. return nil
  105. }
  106. func (s *userService) Update(req dao.User) *common.Errors {
  107. user := &req
  108. if user.Birthday == "" {
  109. user.Birthday = "1900-01-01 00:00:00"
  110. }
  111. err := user.Update()
  112. if err != nil {
  113. return common.FailResponse(err.Error(), nil)
  114. }
  115. return nil
  116. }
  117. func (s *userService) Remove(userId, id int) *common.Errors {
  118. user := &dao.User{
  119. ID: id,
  120. IsDeleted: 1,
  121. UpdateUser: userId,
  122. UpdateTime: time.Now(),
  123. }
  124. err := user.Remove()
  125. if err != nil {
  126. return common.FailResponse(err.Error(), nil)
  127. }
  128. return nil
  129. }
  130. func (s *userService) ResetPwd(id int) *common.Errors {
  131. user := &dao.User{ID: id}
  132. err := user.UpdatePwd(model.DefaultPwd)
  133. if err != nil {
  134. return common.FailResponse(err.Error(), nil)
  135. }
  136. return nil
  137. }
  138. func (s *userService) GetList() ([]dao.User, error) {
  139. user := &dao.User{}
  140. users, err := user.GetAll()
  141. if err != nil {
  142. return nil, err
  143. }
  144. return users, nil
  145. }
  146. func (s *userService) Grant(userIds []string, roleIds string) error {
  147. user := &dao.User{}
  148. return user.UpdateRoles(userIds, roleIds)
  149. }
  150. const UserKey = "%d:sys:user:%d"
  151. func (s *userService) GetUserName(tenantId string, id int) string {
  152. var name string
  153. key := fmt.Sprintf(UserKey, tenantId, id)
  154. name, err := cache.Redis.Get(key).Result()
  155. if err != nil {
  156. user := &dao.User{ID: id}
  157. if err := user.GetUser(); err != nil {
  158. logger.Logger.Errorf("GetUser err = %s \n", err)
  159. } else {
  160. name = user.Name
  161. cache.Redis.Set(key, name, time.Second*10)
  162. }
  163. }
  164. return name
  165. }
  166. func (s *userService) IsExist(user dao.User) bool {
  167. return user.IsExist()
  168. }
  169. // UpAvatar 上传头像
  170. func (s *userService) UpAvatar(tenantId string, userId int, fileHeader *multipart.FileHeader) *model.Link {
  171. //检查是否已有头像,成功上传后将其删除
  172. dao := &dao.User{TenantId: tenantId, ID: userId}
  173. dao.GetAvatar()
  174. oldAvatar := dao.Avatar
  175. file, err := fileHeader.Open()
  176. //文件校验
  177. if err != nil {
  178. logger.Logger.Errorf("UploadFile fileHeader.Open fail, err = %s", err.Error())
  179. return nil
  180. }
  181. fileNameArray := strings.Split(fileHeader.Filename, ".")
  182. if len(fileNameArray) < 2 {
  183. return nil
  184. }
  185. //上传
  186. bucket := fmt.Sprintf("picture-%d", tenantId)
  187. objectName := "user/" + common.RandomString2(16) + "_" + common.RandomString2(6) + "." + fileNameArray[len(fileNameArray)-1]
  188. err = minio.PutFile(minio.FileObject{
  189. Bucket: bucket,
  190. ObjectName: objectName,
  191. ObjectSize: fileHeader.Size,
  192. Reader: file,
  193. })
  194. if err != nil {
  195. logger.Logger.Errorf("UploadFile PutFile fail, err = %s", err.Error())
  196. return nil
  197. }
  198. //从minio删除旧的
  199. if oldAvatar != "" {
  200. old := strings.Split(oldAvatar, "user/")
  201. oldName := "user/" + old[1]
  202. minio.DeleteObject(bucket, oldName)
  203. }
  204. //新的保存到数据库
  205. url := config.Instance().Minio.Link + "/" + bucket + "/" + objectName
  206. dao.Avatar = url
  207. dao.Update()
  208. var link = model.Link{Link: url}
  209. return &link
  210. }