sys_user.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package system
  2. import (
  3. "errors"
  4. "fmt"
  5. "server/dao"
  6. "time"
  7. "github.com/gofrs/uuid/v5"
  8. "gorm.io/gorm"
  9. "server/global"
  10. "server/model/common/request"
  11. "server/utils"
  12. )
  13. //@author: [piexlmax](https://github.com/piexlmax)
  14. //@function: Register
  15. //@description: 用户注册
  16. //@param: u model.SysUser
  17. //@return: userInter system.SysUser, err error
  18. type UserService struct{}
  19. func (userService *UserService) Register(u dao.SysUser) (userInter dao.SysUser, err error) {
  20. _, err = dao.QueryUserByUserName(u.Username)
  21. if !errors.Is(err, gorm.ErrRecordNotFound) { // 判断用户名是否注册
  22. return userInter, errors.New("用户名已注册")
  23. }
  24. // 否则 附加uuid 密码hash加密 注册
  25. u.Password = utils.BcryptHash(u.Password)
  26. u.UUID = uuid.Must(uuid.NewV4())
  27. err = u.CreateUser()
  28. return u, err
  29. }
  30. //@author: [piexlmax](https://github.com/piexlmax)
  31. //@author: [SliverHorn](https://github.com/SliverHorn)
  32. //@function: Login
  33. //@description: 用户登录
  34. //@param: u *model.SysUser
  35. //@return: err error, userInter *model.SysUser
  36. func (userService *UserService) Login(u *dao.SysUser) (userInter *dao.SysUser, err error) {
  37. if nil == global.GVA_DB {
  38. return nil, fmt.Errorf("db not init")
  39. }
  40. user, err := dao.QueryUserAndAuthorityByUserName(u.Username)
  41. if err == nil {
  42. if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
  43. return nil, errors.New("密码错误")
  44. }
  45. MenuServiceApp.UserAuthorityDefaultRouter(&user)
  46. }
  47. return &user, err
  48. }
  49. //@author: [piexlmax](https://github.com/piexlmax)
  50. //@function: ChangePassword
  51. //@description: 修改用户密码
  52. //@param: u *model.SysUser, newPassword string
  53. //@return: userInter *model.SysUser,err error
  54. func (userService *UserService) ChangePassword(u *dao.SysUser, newPassword string) (userInter *dao.SysUser, err error) {
  55. user, err := dao.QueryUserByUserId(u.ID)
  56. if err != nil {
  57. return nil, err
  58. }
  59. if ok := utils.BcryptCheck(u.Password, user.Password); !ok {
  60. return nil, errors.New("原密码错误")
  61. }
  62. user.Password = utils.BcryptHash(newPassword)
  63. err = u.UpdateUser()
  64. return &user, err
  65. }
  66. //@author: [piexlmax](https://github.com/piexlmax)
  67. //@function: GetUserInfoList
  68. //@description: 分页获取数据
  69. //@param: info request.PageInfo
  70. //@return: err error, list interface{}, total int64
  71. func (userService *UserService) GetUserInfoList(info request.PageInfo) (list interface{}, total int64, err error) {
  72. limit := info.PageSize
  73. offset := info.PageSize * (info.Page - 1)
  74. userList, total, err := dao.QueryUserInfoList(limit, offset)
  75. for i, user := range userList {
  76. projectList, _ := dao.GetProjectListByUserIDNoPage(user.ID)
  77. userList[i].Projects = projectList
  78. }
  79. return userList, total, err
  80. }
  81. //@author: [piexlmax](https://github.com/piexlmax)
  82. //@function: SetUserAuthority
  83. //@description: 设置一个用户的权限
  84. //@param: uuid uuid.UUID, authorityId string
  85. //@return: err error
  86. func (userService *UserService) SetUserAuthority(id uint, authorityId uint) (err error) {
  87. _, assignErr := dao.QueryUserAuthority(id, authorityId)
  88. if errors.Is(assignErr, gorm.ErrRecordNotFound) {
  89. return errors.New("该用户无此角色")
  90. }
  91. return dao.UpdateUserAuthority(id, authorityId)
  92. }
  93. //@author: [piexlmax](https://github.com/piexlmax)
  94. //@function: SetUserAuthorities
  95. //@description: 设置一个用户的权限
  96. //@param: id uint, authorityIds []string
  97. //@return: err error
  98. func (userService *UserService) SetUserAuthorities(id uint, authorityId uint) (err error) {
  99. return dao.SetUserAuthorities(id, authorityId)
  100. }
  101. //@author: [piexlmax](https://github.com/piexlmax)
  102. //@function: DeleteUser
  103. //@description: 删除用户
  104. //@param: id float64
  105. //@return: err error
  106. func (userService *UserService) DeleteUser(id int) (err error) {
  107. return dao.DeleteUser(id)
  108. }
  109. //@author: [piexlmax](https://github.com/piexlmax)
  110. //@function: SetUserInfo
  111. //@description: 设置用户信息
  112. //@param: reqUser model.SysUser
  113. //@return: err error, user model.SysUser
  114. func (userService *UserService) SetUserInfo(req dao.SysUser) error {
  115. req.UpdatedAt = time.Now()
  116. return req.SetUserInfo()
  117. }
  118. //@author: [piexlmax](https://github.com/piexlmax)
  119. //@function: SetUserInfo
  120. //@description: 设置用户信息
  121. //@param: reqUser model.SysUser
  122. //@return: err error, user model.SysUser
  123. func (userService *UserService) SetSelfInfo(req dao.SysUser) error {
  124. return req.SetUserInfoById()
  125. }
  126. //@author: [piexlmax](https://github.com/piexlmax)
  127. //@author: [SliverHorn](https://github.com/SliverHorn)
  128. //@function: GetUserInfo
  129. //@description: 获取用户信息
  130. //@param: uuid uuid.UUID
  131. //@return: err error, user system.SysUser
  132. func (userService *UserService) GetUserInfo(uuid uuid.UUID) (user dao.SysUser, err error) {
  133. reqUser, err := dao.GetUserInfoByUUID(uuid)
  134. if err != nil {
  135. return reqUser, err
  136. }
  137. MenuServiceApp.UserAuthorityDefaultRouter(&reqUser)
  138. return reqUser, err
  139. }
  140. //@author: [SliverHorn](https://github.com/SliverHorn)
  141. //@function: FindUserById
  142. //@description: 通过id获取用户信息
  143. //@param: id int
  144. //@return: err error, user *model.SysUser
  145. func (userService *UserService) FindUserById(id int) (user *dao.SysUser, err error) {
  146. u, err := dao.QueryUserByUserId(uint(id))
  147. return &u, err
  148. }
  149. //@author: [SliverHorn](https://github.com/SliverHorn)
  150. //@function: FindUserByUuid
  151. //@description: 通过uuid获取用户信息
  152. //@param: uuid string
  153. //@return: err error, user *model.SysUser
  154. func (userService *UserService) FindUserByUuid(uuidStr string) (user *dao.SysUser, err error) {
  155. id, err := uuid.FromString(uuidStr)
  156. if err != nil {
  157. fmt.Println("Error parsing UUID:", err)
  158. return
  159. }
  160. u, err := dao.GetUserInfoByUUID(id)
  161. if err != nil {
  162. return &u, errors.New("用户不存在")
  163. }
  164. return &u, nil
  165. }
  166. //@author: [piexlmax](https://github.com/piexlmax)
  167. //@function: resetPassword
  168. //@description: 修改用户密码
  169. //@param: ID uint
  170. //@return: err error
  171. func (userService *UserService) ResetPassword(ID uint) error {
  172. return dao.SysUser{
  173. GVA_MODEL: global.GVA_MODEL{
  174. ID: ID,
  175. },
  176. Password: utils.BcryptHash("123456"),
  177. }.SetUserInfoById()
  178. }