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.

124 lines
4.3 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).First(&user).Error
  41. if err != nil {
  42. return err, &user
  43. }
  44. err = global.GVA_DB.Where("authority_id = ?", user.AuthorityId).First(&user.Authority).Error
  45. return err, &user
  46. }
  47. // @title ChangePassword
  48. // @description change the password of a certain user, 修改用户密码
  49. // @auth (2020/04/05 20:22)
  50. // @param u *model.SysUser
  51. // @param newPassword string
  52. // @return err error
  53. // @return userInter *SysUser
  54. func ChangePassword(u *model.SysUser, newPassword string) (err error, userInter *model.SysUser) {
  55. var user model.SysUser
  56. // TODO:后期修改jwt+password模式
  57. u.Password = utils.MD5V([]byte(u.Password))
  58. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  59. return err, u
  60. }
  61. // @title GetInfoList
  62. // @description get user list by pagination, 分页获取数据
  63. // @auth (2020/04/05 20:22)
  64. // @param info request.PageInfo
  65. // @return err error
  66. // @return list interface{}
  67. // @return total int
  68. func GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int) {
  69. limit := info.PageSize
  70. offset := info.PageSize * (info.Page - 1)
  71. db := global.GVA_DB
  72. var userList []model.SysUser
  73. err = db.Find(&userList).Count(&total).Error
  74. err = db.Limit(limit).Offset(offset).Preload("Authority").Find(&userList).Error
  75. return err, userList, total
  76. }
  77. // @title SetUserAuthority
  78. // @description set the authority of a certain user, 设置一个用户的权限
  79. // @auth (2020/04/05 20:22)
  80. // @param uuid UUID
  81. // @param authorityId string
  82. // @return err error
  83. func SetUserAuthority(uuid uuid.UUID, authorityId string) (err error) {
  84. err = global.GVA_DB.Where("uuid = ?", uuid).First(&model.SysUser{}).Update("authority_id", authorityId).Error
  85. return err
  86. }
  87. // @title SetUserAuthority
  88. // @description set the authority of a certain user, 设置一个用户的权限
  89. // @auth (2020/04/05 20:22)
  90. // @param uuid UUID
  91. // @param authorityId string
  92. // @return err error
  93. func DeleteUser(id float64) (err error) {
  94. var user model.SysUser
  95. err = global.GVA_DB.Where("id = ?", id).Delete(&user).Error
  96. return err
  97. }
  98. // @title UploadHeaderImg
  99. // @description upload avatar, 用户头像上传更新地址
  100. // @auth (2020/04/05 20:22)
  101. // @param uuid UUID
  102. // @param filePath string
  103. // @return err error
  104. // @return userInter *SysUser
  105. func UploadHeaderImg(uuid uuid.UUID, filePath string) (err error, userInter *model.SysUser) {
  106. var user model.SysUser
  107. err = global.GVA_DB.Where("uuid = ?", uuid).First(&user).Update("header_img", filePath).First(&user).Error
  108. return err, &user
  109. }