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.

134 lines
4.4 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. "gin-vue-admin/global"
  5. "gin-vue-admin/model/common/request"
  6. "gin-vue-admin/model/system"
  7. "gin-vue-admin/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("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("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(uuid uuid.UUID, authorityId string) (err error) {
  71. err = global.GVA_DB.Where("uuid = ?", uuid).First(&system.SysUser{}).Update("authority_id", authorityId).Error
  72. return err
  73. }
  74. //@author: [piexlmax](https://github.com/piexlmax)
  75. //@function: DeleteUser
  76. //@description: 删除用户
  77. //@param: id float64
  78. //@return: err error
  79. func (userService *UserService) DeleteUser(id float64) (err error) {
  80. var user system.SysUser
  81. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  82. return err
  83. }
  84. //@author: [piexlmax](https://github.com/piexlmax)
  85. //@function: SetUserInfo
  86. //@description: 设置用户信息
  87. //@param: reqUser model.SysUser
  88. //@return: err error, user model.SysUser
  89. func (userService *UserService) SetUserInfo(reqUser system.SysUser) (err error, user system.SysUser) {
  90. err = global.GVA_DB.Updates(&reqUser).Error
  91. return err, reqUser
  92. }
  93. //@author: [SliverHorn](https://github.com/SliverHorn)
  94. //@function: FindUserById
  95. //@description: 通过id获取用户信息
  96. //@param: id int
  97. //@return: err error, user *model.SysUser
  98. func (userService *UserService) FindUserById(id int) (err error, user *system.SysUser) {
  99. var u system.SysUser
  100. err = global.GVA_DB.Where("`id` = ?", id).First(&u).Error
  101. return err, &u
  102. }
  103. //@author: [SliverHorn](https://github.com/SliverHorn)
  104. //@function: FindUserByUuid
  105. //@description: 通过uuid获取用户信息
  106. //@param: uuid string
  107. //@return: err error, user *model.SysUser
  108. func (userService *UserService) FindUserByUuid(uuid string) (err error, user *system.SysUser) {
  109. var u system.SysUser
  110. if err = global.GVA_DB.Where("`uuid` = ?", uuid).First(&u).Error; err != nil {
  111. return errors.New("用户不存在"), &u
  112. }
  113. return nil, &u
  114. }