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.

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