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.

204 lines
6.7 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
3 years ago
  1. package system
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/flipped-aurora/gin-vue-admin/server/global"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  8. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  9. uuid "github.com/satori/go.uuid"
  10. "gorm.io/gorm"
  11. )
  12. //@author: [piexlmax](https://github.com/piexlmax)
  13. //@function: Register
  14. //@description: 用户注册
  15. //@param: u model.SysUser
  16. //@return: err error, userInter model.SysUser
  17. type UserService struct{}
  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. if nil == global.GVA_DB {
  36. return fmt.Errorf("db not init"), nil
  37. }
  38. var user system.SysUser
  39. u.Password = utils.MD5V([]byte(u.Password))
  40. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).Preload("Authorities").Preload("Authority").First(&user).Error
  41. return err, &user
  42. }
  43. //@author: [piexlmax](https://github.com/piexlmax)
  44. //@function: ChangePassword
  45. //@description: 修改用户密码
  46. //@param: u *model.SysUser, newPassword string
  47. //@return: err error, userInter *model.SysUser
  48. func (userService *UserService) ChangePassword(u *system.SysUser, newPassword string) (err error, userInter *system.SysUser) {
  49. var user system.SysUser
  50. u.Password = utils.MD5V([]byte(u.Password))
  51. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  52. return err, u
  53. }
  54. //@author: [piexlmax](https://github.com/piexlmax)
  55. //@function: GetUserInfoList
  56. //@description: 分页获取数据
  57. //@param: info request.PageInfo
  58. //@return: err error, list interface{}, total int64
  59. func (userService *UserService) GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  60. limit := info.PageSize
  61. offset := info.PageSize * (info.Page - 1)
  62. db := global.GVA_DB.Model(&system.SysUser{})
  63. var userList []system.SysUser
  64. err = db.Count(&total).Error
  65. if err != nil {
  66. return
  67. }
  68. err = db.Limit(limit).Offset(offset).Preload("Authorities").Preload("Authority").Find(&userList).Error
  69. return err, userList, total
  70. }
  71. //@author: [piexlmax](https://github.com/piexlmax)
  72. //@function: SetUserAuthority
  73. //@description: 设置一个用户的权限
  74. //@param: uuid uuid.UUID, authorityId string
  75. //@return: err error
  76. func (userService *UserService) SetUserAuthority(id uint, uuid uuid.UUID, authorityId string) (err error) {
  77. assignErr := global.GVA_DB.Where("sys_user_id = ? AND sys_authority_authority_id = ?", id, authorityId).First(&system.SysUseAuthority{}).Error
  78. if errors.Is(assignErr, gorm.ErrRecordNotFound) {
  79. return errors.New("该用户无此角色")
  80. }
  81. err = global.GVA_DB.Where("uuid = ?", uuid).First(&system.SysUser{}).Update("authority_id", authorityId).Error
  82. return err
  83. }
  84. //@author: [piexlmax](https://github.com/piexlmax)
  85. //@function: SetUserAuthorities
  86. //@description: 设置一个用户的权限
  87. //@param: id uint, authorityIds []string
  88. //@return: err error
  89. func (userService *UserService) SetUserAuthorities(id uint, authorityIds []string) (err error) {
  90. return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
  91. TxErr := tx.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  92. if TxErr != nil {
  93. return TxErr
  94. }
  95. useAuthority := []system.SysUseAuthority{}
  96. for _, v := range authorityIds {
  97. useAuthority = append(useAuthority, system.SysUseAuthority{
  98. id, v,
  99. })
  100. }
  101. TxErr = tx.Create(&useAuthority).Error
  102. if TxErr != nil {
  103. return TxErr
  104. }
  105. TxErr = tx.Where("id = ?", id).First(&system.SysUser{}).Update("authority_id", authorityIds[0]).Error
  106. if TxErr != nil {
  107. return TxErr
  108. }
  109. // 返回 nil 提交事务
  110. return nil
  111. })
  112. }
  113. //@author: [piexlmax](https://github.com/piexlmax)
  114. //@function: DeleteUser
  115. //@description: 删除用户
  116. //@param: id float64
  117. //@return: err error
  118. func (userService *UserService) DeleteUser(id float64) (err error) {
  119. var user system.SysUser
  120. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  121. if err != nil {
  122. return err
  123. }
  124. err = global.GVA_DB.Delete(&[]system.SysUseAuthority{}, "sys_user_id = ?", id).Error
  125. return err
  126. }
  127. //@author: [piexlmax](https://github.com/piexlmax)
  128. //@function: SetUserInfo
  129. //@description: 设置用户信息
  130. //@param: reqUser model.SysUser
  131. //@return: err error, user model.SysUser
  132. func (userService *UserService) SetUserInfo(reqUser system.SysUser) (err error, user system.SysUser) {
  133. err = global.GVA_DB.Updates(&reqUser).Error
  134. return err, reqUser
  135. }
  136. //@author: [piexlmax](https://github.com/piexlmax)
  137. //@function: GetUserInfo
  138. //@description: 获取用户信息
  139. //@param: uuid uuid.UUID
  140. //@return: err error, user system.SysUser
  141. func (userService *UserService) GetUserInfo(uuid uuid.UUID) (err error, user system.SysUser) {
  142. var reqUser system.SysUser
  143. err = global.GVA_DB.Preload("Authorities").Preload("Authority").First(&reqUser, "uuid = ?", uuid).Error
  144. return err, reqUser
  145. }
  146. //@author: [SliverHorn](https://github.com/SliverHorn)
  147. //@function: FindUserById
  148. //@description: 通过id获取用户信息
  149. //@param: id int
  150. //@return: err error, user *model.SysUser
  151. func (userService *UserService) FindUserById(id int) (err error, user *system.SysUser) {
  152. var u system.SysUser
  153. err = global.GVA_DB.Where("`id` = ?", id).First(&u).Error
  154. return err, &u
  155. }
  156. //@author: [SliverHorn](https://github.com/SliverHorn)
  157. //@function: FindUserByUuid
  158. //@description: 通过uuid获取用户信息
  159. //@param: uuid string
  160. //@return: err error, user *model.SysUser
  161. func (userService *UserService) FindUserByUuid(uuid string) (err error, user *system.SysUser) {
  162. var u system.SysUser
  163. if err = global.GVA_DB.Where("`uuid` = ?", uuid).First(&u).Error; err != nil {
  164. return errors.New("用户不存在"), &u
  165. }
  166. return nil, &u
  167. }
  168. //@author: [piexlmax](https://github.com/piexlmax)
  169. //@function: resetPassword
  170. //@description: 修改用户密码
  171. //@param: ID uint
  172. //@return: err error
  173. func (userService *UserService) ResetPassword(ID uint) (err error) {
  174. err = global.GVA_DB.Model(&system.SysUser{}).Where("id = ?", ID).Update("password", utils.MD5V([]byte("123456"))).Error
  175. return err
  176. }