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.

189 lines
6.3 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. if err != nil {
  63. return
  64. }
  65. err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Find(&userList).Error
  66. return err, userList, total
  67. }
  68. //@author: [piexlmax](https://github.com/piexlmax)
  69. //@function: SetUserAuthority
  70. //@description: 设置一个用户的权限
  71. //@param: uuid uuid.UUID, authorityId string
  72. //@return: err error
  73. func (userService *UserService) SetUserAuthority(id uint, uuid uuid.UUID, authorityId string) (err error) {
  74. assignErr := global.GVA_DB.Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&system.SysUseAuthority{}).Error
  75. if errors.Is(assignErr, gorm.ErrRecordNotFound) {
  76. return errors.New("该用户无此角色")
  77. }
  78. err = global.GVA_DB.Where("uuid = ?", uuid).First(&system.SysUser{}).Update("authority_id", authorityId).Error
  79. return err
  80. }
  81. //@author: [piexlmax](https://github.com/piexlmax)
  82. //@function: SetUserAuthorities
  83. //@description: 设置一个用户的权限
  84. //@param: id uint, authorityIds []string
  85. //@return: err error
  86. func (userService *UserService) SetUserAuthorities(id uint, authorityIds []string) (err error) {
  87. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  88. TxErr := tx.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  89. if TxErr != nil {
  90. return TxErr
  91. }
  92. useAuthority := []system.SysUseAuthority{}
  93. for _, v := range authorityIds {
  94. useAuthority = append(useAuthority, system.SysUseAuthority{
  95. id, v,
  96. })
  97. }
  98. TxErr = tx.Create(&useAuthority).Error
  99. if TxErr != nil {
  100. return TxErr
  101. }
  102. TxErr = tx.Where("id = ?", id).First(&system.SysUser{}).Update("authority_id", authorityIds[0]).Error
  103. if TxErr != nil {
  104. return TxErr
  105. }
  106. // 返回 nil 提交事务
  107. return nil
  108. })
  109. }
  110. //@author: [piexlmax](https://github.com/piexlmax)
  111. //@function: DeleteUser
  112. //@description: 删除用户
  113. //@param: id float64
  114. //@return: err error
  115. func (userService *UserService) DeleteUser(id float64) (err error) {
  116. var user system.SysUser
  117. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  118. if err != nil {
  119. return err
  120. }
  121. err = global.GVA_DB.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  122. return err
  123. }
  124. //@author: [piexlmax](https://github.com/piexlmax)
  125. //@function: SetUserInfo
  126. //@description: 设置用户信息
  127. //@param: reqUser model.SysUser
  128. //@return: err error, user model.SysUser
  129. func (userService *UserService) SetUserInfo(reqUser system.SysUser) (err error, user system.SysUser) {
  130. err = global.GVA_DB.Updates(&reqUser).Error
  131. return err, reqUser
  132. }
  133. //@author: [piexlmax](https://github.com/piexlmax)
  134. //@function: GetUserInfo
  135. //@description: 获取用户信息
  136. //@param: uuid uuid.UUID
  137. //@return: err error, user system.SysUser
  138. func (userService *UserService) GetUserInfo(uuid uuid.UUID) (err error, user system.SysUser) {
  139. var reqUser system.SysUser
  140. err = global.GVA_DB.Preload("Authorities").Preload("Authority").First(&reqUser, "uuid = ?", uuid).Error
  141. return err, reqUser
  142. }
  143. //@author: [SliverHorn](https://github.com/SliverHorn)
  144. //@function: FindUserById
  145. //@description: 通过id获取用户信息
  146. //@param: id int
  147. //@return: err error, user *model.SysUser
  148. func (userService *UserService) FindUserById(id int) (err error, user *system.SysUser) {
  149. var u system.SysUser
  150. err = global.GVA_DB.Where("`id` = ?", id).First(&u).Error
  151. return err, &u
  152. }
  153. //@author: [SliverHorn](https://github.com/SliverHorn)
  154. //@function: FindUserByUuid
  155. //@description: 通过uuid获取用户信息
  156. //@param: uuid string
  157. //@return: err error, user *model.SysUser
  158. func (userService *UserService) FindUserByUuid(uuid string) (err error, user *system.SysUser) {
  159. var u system.SysUser
  160. if err = global.GVA_DB.Where("`uuid` = ?", uuid).First(&u).Error; err != nil {
  161. return errors.New("用户不存在"), &u
  162. }
  163. return nil, &u
  164. }