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.

131 lines
4.1 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  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. func Register(u model.SysUser) (err error, userInter model.SysUser) {
  17. var user model.SysUser
  18. if !errors.Is(global.GVA_DB.Where("username = ?", u.Username).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
  19. return errors.New("用户名已注册"), userInter
  20. }
  21. // 否则 附加uuid 密码md5简单加密 注册
  22. u.Password = utils.MD5V([]byte(u.Password))
  23. u.UUID = uuid.NewV4()
  24. err = global.GVA_DB.Create(&u).Error
  25. return err, u
  26. }
  27. //@author: [piexlmax](https://github.com/piexlmax)
  28. //@function: Login
  29. //@description: 用户登录
  30. //@param: u *model.SysUser
  31. //@return: err error, userInter *model.SysUser
  32. func Login(u *model.SysUser) (err error, userInter *model.SysUser) {
  33. var user model.SysUser
  34. u.Password = utils.MD5V([]byte(u.Password))
  35. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).Preload("Authority").First(&user).Error
  36. return err, &user
  37. }
  38. //@author: [piexlmax](https://github.com/piexlmax)
  39. //@function: ChangePassword
  40. //@description: 修改用户密码
  41. //@param: u *model.SysUser, newPassword string
  42. //@return: err error, userInter *model.SysUser
  43. func ChangePassword(u *model.SysUser, newPassword string) (err error, userInter *model.SysUser) {
  44. var user model.SysUser
  45. u.Password = utils.MD5V([]byte(u.Password))
  46. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  47. return err, u
  48. }
  49. //@author: [piexlmax](https://github.com/piexlmax)
  50. //@function: GetUserInfoList
  51. //@description: 分页获取数据
  52. //@param: info request.PageInfo
  53. //@return: err error, list interface{}, total int64
  54. func GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  55. limit := info.PageSize
  56. offset := info.PageSize * (info.Page - 1)
  57. db := global.GVA_DB.Model(&model.SysUser{})
  58. var userList []model.SysUser
  59. err = db.Count(&total).Error
  60. err = db.Limit(limit).Offset(offset).Preload("Authority").Find(&userList).Error
  61. return err, userList, total
  62. }
  63. //@author: [piexlmax](https://github.com/piexlmax)
  64. //@function: SetUserAuthority
  65. //@description: 设置一个用户的权限
  66. //@param: uuid uuid.UUID, authorityId string
  67. //@return: err error
  68. func SetUserAuthority(uuid uuid.UUID, authorityId string) (err error) {
  69. err = global.GVA_DB.Where("uuid = ?", uuid).First(&model.SysUser{}).Update("authority_id", authorityId).Error
  70. return err
  71. }
  72. //@author: [piexlmax](https://github.com/piexlmax)
  73. //@function: DeleteUser
  74. //@description: 删除用户
  75. //@param: id float64
  76. //@return: err error
  77. func DeleteUser(id float64) (err error) {
  78. var user model.SysUser
  79. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  80. return err
  81. }
  82. //@author: [piexlmax](https://github.com/piexlmax)
  83. //@function: SetUserInfo
  84. //@description: 设置用户信息
  85. //@param: reqUser model.SysUser
  86. //@return: err error, user model.SysUser
  87. func SetUserInfo(reqUser model.SysUser) (err error, user model.SysUser) {
  88. err = global.GVA_DB.Updates(&reqUser).Error
  89. return err, reqUser
  90. }
  91. //@author: [SliverHorn](https://github.com/SliverHorn)
  92. //@function: FindUserById
  93. //@description: 通过id获取用户信息
  94. //@param: id int
  95. //@return: err error, user *model.SysUser
  96. func FindUserById(id int) (err error, user *model.SysUser) {
  97. var u model.SysUser
  98. err = global.GVA_DB.Where("`id` = ?", id).First(&u).Error
  99. return err, &u
  100. }
  101. //@author: [SliverHorn](https://github.com/SliverHorn)
  102. //@function: FindUserByUuid
  103. //@description: 通过uuid获取用户信息
  104. //@param: uuid string
  105. //@return: err error, user *model.SysUser
  106. func FindUserByUuid(uuid string) (err error, user *model.SysUser) {
  107. var u model.SysUser
  108. if err = global.GVA_DB.Where("`uuid` = ?", uuid).First(&u).Error; err != nil {
  109. return errors.New("用户不存在"), &u
  110. }
  111. return nil, &u
  112. }