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.

141 lines
4.9 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. // @title Register
  12. // @description register, 用户注册
  13. // @auth (2020/04/05 20:22)
  14. // @param u model.SysUser
  15. // @return err error
  16. // @return userInter *SysUser
  17. func Register(u model.SysUser) (err error, userInter model.SysUser) {
  18. var user model.SysUser
  19. if !errors.Is(global.GVA_DB.Where("username = ?", u.Username).First(&user).Error, gorm.ErrRecordNotFound) { // 判断用户名是否注册
  20. return errors.New("用户名已注册"), userInter
  21. }
  22. // 否则 附加uuid 密码md5简单加密 注册
  23. u.Password = utils.MD5V([]byte(u.Password))
  24. u.UUID = uuid.NewV4()
  25. err = global.GVA_DB.Create(&u).Error
  26. return err, u
  27. }
  28. // @title Login
  29. // @description login, 用户登录
  30. // @auth (2020/04/05 20:22)
  31. // @param u *model.SysUser
  32. // @return err error
  33. // @return userInter *SysUser
  34. func Login(u *model.SysUser) (err error, userInter *model.SysUser) {
  35. var user model.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. // @title ChangePassword
  41. // @description change the password of a certain user, 修改用户密码
  42. // @auth (2020/04/05 20:22)
  43. // @param u *model.SysUser
  44. // @param newPassword string
  45. // @return err error
  46. // @return userInter *SysUser
  47. func ChangePassword(u *model.SysUser, newPassword string) (err error, userInter *model.SysUser) {
  48. var user model.SysUser
  49. u.Password = utils.MD5V([]byte(u.Password))
  50. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  51. return err, u
  52. }
  53. // @title GetInfoList
  54. // @description get user list by pagination, 分页获取数据
  55. // @auth (2020/04/05 20:22)
  56. // @param info request.PageInfo
  57. // @return err error
  58. // @return list interface{}
  59. // @return total int
  60. func GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  61. limit := info.PageSize
  62. offset := info.PageSize * (info.Page - 1)
  63. db := global.GVA_DB.Model(&model.SysUser{})
  64. var userList []model.SysUser
  65. err = db.Count(&total).Error
  66. err = db.Limit(limit).Offset(offset).Preload("Authority").Find(&userList).Error
  67. return err, userList, total
  68. }
  69. // @title SetUserAuthority
  70. // @description set the authority of a certain user, 设置一个用户的权限
  71. // @auth (2020/04/05 20:22)
  72. // @param uuid UUID
  73. // @param authorityId string
  74. // @return err error
  75. func SetUserAuthority(uuid uuid.UUID, authorityId string) (err error) {
  76. err = global.GVA_DB.Where("uuid = ?", uuid).First(&model.SysUser{}).Update("authority_id", authorityId).Error
  77. return err
  78. }
  79. // @title SetUserAuthority
  80. // @description set the authority of a certain user, 设置一个用户的权限
  81. // @auth (2020/04/05 20:22)
  82. // @param uuid UUID
  83. // @param authorityId string
  84. // @return err error
  85. func DeleteUser(id float64) (err error) {
  86. var user model.SysUser
  87. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  88. return err
  89. }
  90. // @title SetUserInfo
  91. // @description set the authority of a certain user, 设置用户信息
  92. // @auth (2020/04/05 20:22)
  93. // @param uuid UUID
  94. // @param authorityId string
  95. // @return err error
  96. func SetUserInfo(reqUser model.SysUser) (err error, user model.SysUser) {
  97. err = global.GVA_DB.Updates(&reqUser).Error
  98. return err, reqUser
  99. }
  100. // @title FindUserById
  101. // @description Get user information by id, 通过id获取用户信息
  102. // @auth (2020/04/05 20:22)
  103. // @param id int
  104. // @return err error
  105. // @return user *model.SysUser
  106. func FindUserById(id int) (err error, user *model.SysUser) {
  107. var u model.SysUser
  108. err = global.GVA_DB.Where("`id` = ?", id).First(&u).Error
  109. return err, &u
  110. }
  111. // @title FindUserByUuid
  112. // @description Get user information by uuid, 通过uuid获取用户信息
  113. // @auth (2020/04/05 20:22)
  114. // @param uuid string
  115. // @return err error
  116. // @return user *model.SysUser
  117. func FindUserByUuid(uuid string) (err error, user *model.SysUser) {
  118. var u model.SysUser
  119. if err = global.GVA_DB.Where("`uuid` = ?", uuid).First(&u).Error; err != nil{
  120. return errors.New("用户不存在"), &u
  121. }
  122. return nil, &u
  123. }