sys_user.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. return userList, total, err
  76. }
  77. //@author: [piexlmax](https://github.com/piexlmax)
  78. //@function: SetUserAuthority
  79. //@description: 设置一个用户的权限
  80. //@param: uuid uuid.UUID, authorityId string
  81. //@return: err error
  82. func (userService *UserService) SetUserAuthority(id uint, authorityId uint) (err error) {
  83. _, assignErr := dao.QueryUserAuthority(id, authorityId)
  84. if errors.Is(assignErr, gorm.ErrRecordNotFound) {
  85. return errors.New("该用户无此角色")
  86. }
  87. return dao.UpdateUserAuthority(id, authorityId)
  88. }
  89. //@author: [piexlmax](https://github.com/piexlmax)
  90. //@function: SetUserAuthorities
  91. //@description: 设置一个用户的权限
  92. //@param: id uint, authorityIds []string
  93. //@return: err error
  94. func (userService *UserService) SetUserAuthorities(id uint, authorityId uint) (err error) {
  95. return dao.SetUserAuthorities(id, authorityId)
  96. }
  97. //@author: [piexlmax](https://github.com/piexlmax)
  98. //@function: DeleteUser
  99. //@description: 删除用户
  100. //@param: id float64
  101. //@return: err error
  102. func (userService *UserService) DeleteUser(id int) (err error) {
  103. return dao.DeleteUser(id)
  104. }
  105. //@author: [piexlmax](https://github.com/piexlmax)
  106. //@function: SetUserInfo
  107. //@description: 设置用户信息
  108. //@param: reqUser model.SysUser
  109. //@return: err error, user model.SysUser
  110. func (userService *UserService) SetUserInfo(req dao.SysUser) error {
  111. req.UpdatedAt = time.Now()
  112. return req.SetUserInfo()
  113. }
  114. //@author: [piexlmax](https://github.com/piexlmax)
  115. //@function: SetUserInfo
  116. //@description: 设置用户信息
  117. //@param: reqUser model.SysUser
  118. //@return: err error, user model.SysUser
  119. func (userService *UserService) SetSelfInfo(req dao.SysUser) error {
  120. return req.SetUserInfoById()
  121. }
  122. //@author: [piexlmax](https://github.com/piexlmax)
  123. //@author: [SliverHorn](https://github.com/SliverHorn)
  124. //@function: GetUserInfo
  125. //@description: 获取用户信息
  126. //@param: uuid uuid.UUID
  127. //@return: err error, user system.SysUser
  128. func (userService *UserService) GetUserInfo(uuid uuid.UUID) (user dao.SysUser, err error) {
  129. reqUser, err := dao.GetUserInfoByUUID(uuid)
  130. if err != nil {
  131. return reqUser, err
  132. }
  133. MenuServiceApp.UserAuthorityDefaultRouter(&reqUser)
  134. return reqUser, err
  135. }
  136. //@author: [SliverHorn](https://github.com/SliverHorn)
  137. //@function: FindUserById
  138. //@description: 通过id获取用户信息
  139. //@param: id int
  140. //@return: err error, user *model.SysUser
  141. func (userService *UserService) FindUserById(id int) (user *dao.SysUser, err error) {
  142. u, err := dao.QueryUserByUserId(uint(id))
  143. return &u, err
  144. }
  145. //@author: [SliverHorn](https://github.com/SliverHorn)
  146. //@function: FindUserByUuid
  147. //@description: 通过uuid获取用户信息
  148. //@param: uuid string
  149. //@return: err error, user *model.SysUser
  150. func (userService *UserService) FindUserByUuid(uuidStr string) (user *dao.SysUser, err error) {
  151. id, err := uuid.FromString(uuidStr)
  152. if err != nil {
  153. fmt.Println("Error parsing UUID:", err)
  154. return
  155. }
  156. u, err := dao.GetUserInfoByUUID(id)
  157. if err != nil {
  158. return &u, errors.New("用户不存在")
  159. }
  160. return &u, nil
  161. }
  162. //@author: [piexlmax](https://github.com/piexlmax)
  163. //@function: resetPassword
  164. //@description: 修改用户密码
  165. //@param: ID uint
  166. //@return: err error
  167. func (userService *UserService) ResetPassword(ID uint) error {
  168. return dao.SysUser{
  169. GVA_MODEL: global.GVA_MODEL{
  170. ID: ID,
  171. },
  172. Password: utils.BcryptHash("123456"),
  173. }.SetUserInfoById()
  174. }