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.

104 lines
3.8 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. //后期修改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.Limit(limit).Offset(offset).Preload("Authority").Find(&userList).Error
  74. return err, userList, total
  75. }
  76. // @title SetUserAuthority
  77. // @description set the authority of a certain user, 设置一个用户的权限
  78. // @auth (2020/04/05 20:22)
  79. // @param uuid UUID
  80. // @param authorityId string
  81. // @return err error
  82. func SetUserAuthority(uuid uuid.UUID, authorityId string) (err error) {
  83. err = global.GVA_DB.Where("uuid = ?", uuid).First(&model.SysUser{}).Update("authority_id", authorityId).Error
  84. return err
  85. }
  86. // @title UploadHeaderImg
  87. // @description upload avatar, 用户头像上传更新地址
  88. // @auth (2020/04/05 20:22)
  89. // @param uuid UUID
  90. // @param filePath string
  91. // @return err error
  92. // @return userInter *SysUser
  93. func UploadHeaderImg(uuid uuid.UUID, filePath string) (err error, userInter *model.SysUser) {
  94. var user model.SysUser
  95. err = global.GVA_DB.Where("uuid = ?", uuid).First(&user).Update("header_img", filePath).First(&user).Error
  96. return err, &user
  97. }