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.

102 lines
3.7 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. // @return err error
  14. // @return userInter *SysUser
  15. func Register(u *model.SysUser) (err error, userInter *model.SysUser) {
  16. var user model.SysUser
  17. //判断用户名是否注册
  18. notRegister := global.GVA_DB.Where("username = ?", u.Username).First(&user).RecordNotFound()
  19. //notRegister为false表明读取到了 不能注册
  20. if !notRegister {
  21. return errors.New("用户名已注册"), nil
  22. } else {
  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. }
  28. return err, u
  29. }
  30. // @title Login
  31. // @description login, 用户登录
  32. // @auth (2020/04/05 20:22 )
  33. // @return err error
  34. // @return userInter *SysUser
  35. func Login(u *model.SysUser) (err error, userInter *model.SysUser) {
  36. var user model.SysUser
  37. u.Password = utils.MD5V([]byte(u.Password))
  38. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Error
  39. if err != nil {
  40. return err, &user
  41. }
  42. err = global.GVA_DB.Where("authority_id = ?", user.AuthorityId).First(&user.Authority).Error
  43. return err, &user
  44. }
  45. // @title ChangePassword
  46. // @description change the password of a certain user, 修改用户密码
  47. // @auth (2020/04/05 20:22 )
  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. //后期修改jwt+password模式
  54. u.Password = utils.MD5V([]byte(u.Password))
  55. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  56. return err, u
  57. }
  58. // @title GetInfoList
  59. // @description get user list by pagination, 分页获取数据
  60. // @auth (2020/04/05 20:22 )
  61. // @param PageInfo int
  62. // @return err error
  63. // @return list interface{}
  64. // @return total int
  65. func GetUserInfoList(info request.PageInfo) (err error, list interface{}, total int) {
  66. limit := info.PageSize
  67. offset := info.PageSize * (info.Page - 1)
  68. db := global.GVA_DB
  69. var userList []model.SysUser
  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 UploadHeaderImg
  84. // @description upload avatar, 用户头像上传更新地址
  85. // @auth (2020/04/05 20:22 )
  86. // @param uuid UUID
  87. // @param filePath string
  88. // @return err error
  89. // @return userInter *SysUser
  90. func UploadHeaderImg(uuid uuid.UUID, filePath string) (err error, userInter *model.SysUser) {
  91. var user model.SysUser
  92. err = global.GVA_DB.Where("uuid = ?", uuid).First(&user).Update("header_img", filePath).First(&user).Error
  93. return err, &user
  94. }