You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
6.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package system
  2. import (
  3. "errors"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  7. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  8. uuid "github.com/satori/go.uuid"
  9. "gorm.io/gorm"
  10. )
  11. //@author: [piexlmax](https://github.com/piexlmax)
  12. //@function: Register
  13. //@description: 用户注册
  14. //@param: u model.SysUser
  15. //@return: err error, userInter model.SysUser
  16. type UserService struct {
  17. }
  18. func (userService *UserService) Register(u system.SysUser) (err error, userInter system.SysUser) {
  19. var user system.SysUser
  20. if !errors.Is(global.GVA_DB.Where("username = ?", u.Username).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
  21. return errors.New("用户名已注册"), userInter
  22. }
  23. // 否则 附加uuid 密码md5简单加密 注册
  24. u.Password = utils.MD5V([]byte(u.Password))
  25. u.UUID = uuid.NewV4()
  26. err = global.GVA_DB.Create(&u).Error
  27. return err, u
  28. }
  29. //@author: [piexlmax](https://github.com/piexlmax)
  30. //@function: Login
  31. //@description: 用户登录
  32. //@param: u *model.SysUser
  33. //@return: err error, userInter *model.SysUser
  34. func (userService *UserService) Login(u *system.SysUser) (err error, userInter *system.SysUser) {
  35. var user system.SysUser
  36. u.Password = utils.MD5V([]byte(u.Password))
  37. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).Preload("Authorities").Preload("Authority").First(&user).Error
  38. return err, &user
  39. }
  40. //@author: [piexlmax](https://github.com/piexlmax)
  41. //@function: ChangePassword
  42. //@description: 修改用户密码
  43. //@param: u *model.SysUser, newPassword string
  44. //@return: err error, userInter *model.SysUser
  45. func (userService *UserService) ChangePassword(u *system.SysUser, newPassword string) (err error, userInter *system.SysUser) {
  46. var user system.SysUser
  47. u.Password = utils.MD5V([]byte(u.Password))
  48. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  49. return err, u
  50. }
  51. //@author: [piexlmax](https://github.com/piexlmax)
  52. //@function: GetUserInfoList
  53. //@description: 分页获取数据
  54. //@param: info request.PageInfo
  55. //@return: err error, list interface{}, total int64
  56. func (userService *UserService) GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  57. limit := info.PageSize
  58. offset := info.PageSize * (info.Page - 1)
  59. db := global.GVA_DB.Model(&system.SysUser{})
  60. var userList []system.SysUser
  61. err = db.Count(&total).Error
  62. err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Find(&userList).Error
  63. return err, userList, total
  64. }
  65. //@author: [piexlmax](https://github.com/piexlmax)
  66. //@function: SetUserAuthority
  67. //@description: 设置一个用户的权限
  68. //@param: uuid uuid.UUID, authorityId string
  69. //@return: err error
  70. func (userService *UserService) SetUserAuthority(id uint, uuid uuid.UUID, authorityId string) (err error) {
  71. assignErr := global.GVA_DB.Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&system.SysUseAuthority{}).Error
  72. if errors.Is(assignErr, gorm.ErrRecordNotFound) {
  73. return errors.New("该用户无此角色")
  74. }
  75. err = global.GVA_DB.Where("uuid = ?", uuid).First(&system.SysUser{}).Update("authority_id", authorityId).Error
  76. return err
  77. }
  78. //@author: [piexlmax](https://github.com/piexlmax)
  79. //@function: SetUserAuthorities
  80. //@description: 设置一个用户的权限
  81. //@param: id uint, authorityIds []string
  82. //@return: err error
  83. func (userService *UserService) SetUserAuthorities(id uint, authorityIds []string) (err error) {
  84. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  85. TxErr := tx.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  86. if TxErr != nil {
  87. return TxErr
  88. }
  89. useAuthority := []system.SysUseAuthority{}
  90. for _, v := range authorityIds {
  91. useAuthority = append(useAuthority, system.SysUseAuthority{
  92. id, v,
  93. })
  94. }
  95. TxErr = tx.Create(&useAuthority).Error
  96. if TxErr != nil {
  97. return TxErr
  98. }
  99. // 返回 nil 提交事务
  100. return nil
  101. })
  102. }
  103. //@author: [piexlmax](https://github.com/piexlmax)
  104. //@function: DeleteUser
  105. //@description: 删除用户
  106. //@param: id float64
  107. //@return: err error
  108. func (userService *UserService) DeleteUser(id float64) (err error) {
  109. var user system.SysUser
  110. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  111. err = global.GVA_DB.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  112. return err
  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) SetUserInfo(reqUser system.SysUser) (err error, user system.SysUser) {
  120. err = global.GVA_DB.Updates(&reqUser).Error
  121. return err, reqUser
  122. }
  123. //@author: [piexlmax](https://github.com/piexlmax)
  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) (err error, user system.SysUser) {
  129. var reqUser system.SysUser
  130. err = global.GVA_DB.Preload("Authorities").Preload("Authority").First(&reqUser, "uuid = ?", uuid).Error
  131. return err, reqUser
  132. }
  133. //@author: [SliverHorn](https://github.com/SliverHorn)
  134. //@function: FindUserById
  135. //@description: 通过id获取用户信息
  136. //@param: id int
  137. //@return: err error, user *model.SysUser
  138. func (userService *UserService) FindUserById(id int) (err error, user *system.SysUser) {
  139. var u system.SysUser
  140. err = global.GVA_DB.Where("`id` = ?", id).First(&u).Error
  141. return err, &u
  142. }
  143. //@author: [SliverHorn](https://github.com/SliverHorn)
  144. //@function: FindUserByUuid
  145. //@description: 通过uuid获取用户信息
  146. //@param: uuid string
  147. //@return: err error, user *model.SysUser
  148. func (userService *UserService) FindUserByUuid(uuid string) (err error, user *system.SysUser) {
  149. var u system.SysUser
  150. if err = global.GVA_DB.Where("`uuid` = ?", uuid).First(&u).Error; err != nil {
  151. return errors.New("用户不存在"), &u
  152. }
  153. return nil, &u
  154. }