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.

120 lines
4.2 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. // 判断用户名是否注册
  20. notRegister := errors.Is(global.GVA_DB.Where("username = ?", u.Username).First(&user).Error, gorm.ErrRecordNotFound)
  21. // notRegister为false表明读取到了 不能注册
  22. if !notRegister {
  23. return errors.New("用户名已注册"), userInter
  24. } else {
  25. // 否则 附加uuid 密码md5简单加密 注册
  26. u.Password = utils.MD5V([]byte(u.Password))
  27. u.UUID = uuid.NewV4()
  28. err = global.GVA_DB.Create(&u).Error
  29. }
  30. return err, u
  31. }
  32. // @title Login
  33. // @description login, 用户登录
  34. // @auth (2020/04/05 20:22)
  35. // @param u *model.SysUser
  36. // @return err error
  37. // @return userInter *SysUser
  38. func Login(u *model.SysUser) (err error, userInter *model.SysUser) {
  39. var user model.SysUser
  40. u.Password = utils.MD5V([]byte(u.Password))
  41. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).Preload("Authority").First(&user).Error
  42. return err, &user
  43. }
  44. // @title ChangePassword
  45. // @description change the password of a certain user, 修改用户密码
  46. // @auth (2020/04/05 20:22)
  47. // @param u *model.SysUser
  48. // @param newPassword string
  49. // @return err error
  50. // @return userInter *SysUser
  51. func ChangePassword(u *model.SysUser, newPassword string) (err error, userInter *model.SysUser) {
  52. var user model.SysUser
  53. u.Password = utils.MD5V([]byte(u.Password))
  54. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  55. return err, u
  56. }
  57. // @title GetInfoList
  58. // @description get user list by pagination, 分页获取数据
  59. // @auth (2020/04/05 20:22)
  60. // @param info request.PageInfo
  61. // @return err error
  62. // @return list interface{}
  63. // @return total int
  64. func GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int64) {
  65. limit := info.PageSize
  66. offset := info.PageSize * (info.Page - 1)
  67. db := global.GVA_DB.Model(&model.SysUser{})
  68. var userList []model.SysUser
  69. err = db.Count(&total).Error
  70. err = db.Limit(limit).Offset(offset).Preload("Authority").Find(&userList).Error
  71. return err, userList, total
  72. }
  73. // @title SetUserAuthority
  74. // @description set the authority of a certain user, 设置一个用户的权限
  75. // @auth (2020/04/05 20:22)
  76. // @param uuid UUID
  77. // @param authorityId string
  78. // @return err error
  79. func SetUserAuthority(uuid uuid.UUID, authorityId string) (err error) {
  80. err = global.GVA_DB.Where("uuid = ?", uuid).First(&model.SysUser{}).Update("authority_id", authorityId).Error
  81. return err
  82. }
  83. // @title SetUserAuthority
  84. // @description set the authority of a certain user, 设置一个用户的权限
  85. // @auth (2020/04/05 20:22)
  86. // @param uuid UUID
  87. // @param authorityId string
  88. // @return err error
  89. func DeleteUser(id float64) (err error) {
  90. var user model.SysUser
  91. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  92. return err
  93. }
  94. // @title UploadHeaderImg
  95. // @description upload avatar, 用户头像上传更新地址
  96. // @auth (2020/04/05 20:22)
  97. // @param uuid UUID
  98. // @param filePath string
  99. // @return err error
  100. // @return userInter *SysUser
  101. func UploadHeaderImg(uuid uuid.UUID, filePath string) (err error, userInter *model.SysUser) {
  102. var user model.SysUser
  103. err = global.GVA_DB.Where("uuid = ?", uuid).First(&user).Update("header_img", filePath).First(&user).Error
  104. return err, &user
  105. }