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.

130 lines
4.6 KiB

  1. package model
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/utils"
  5. "github.com/jinzhu/gorm"
  6. "github.com/pkg/errors"
  7. uuid "github.com/satori/go.uuid"
  8. )
  9. type SysUser struct {
  10. gorm.Model
  11. UUID uuid.UUID `json:"uuid"`
  12. Username string `json:"userName"`
  13. Password string `json:"-"`
  14. NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
  15. HeaderImg string `json:"headerImg" gorm:"default:'http://www.henrongyi.top/avatar/lufu.jpg'"`
  16. Authority SysAuthority `json:"authority" gorm:"ForeignKey:AuthorityId;AssociationForeignKey:AuthorityId"`
  17. AuthorityId string `json:"authorityId" gorm:"default:888"`
  18. }
  19. type RegisterAndLoginStruct struct {
  20. Username string `json:"username"`
  21. Password string `json:"password"`
  22. Captcha string `json:"captcha"`
  23. CaptchaId string `json:"captchaId"`
  24. }
  25. type RegisterStruct struct {
  26. Username string `json:"userName"`
  27. Password string `json:"passWord"`
  28. NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
  29. HeaderImg string `json:"headerImg" gorm:"default:'http://www.henrongyi.top/avatar/lufu.jpg'"`
  30. AuthorityId string `json:"authorityId" gorm:"default:888"`
  31. }
  32. // @title Register
  33. // @description register, 用户注册
  34. // @auth (2020/04/05 20:22 )
  35. // @return err error
  36. // @return userInter *SysUser
  37. func (u *SysUser) Register() (err error, userInter *SysUser) {
  38. var user SysUser
  39. //判断用户名是否注册
  40. notResigt := global.GVA_DB.Where("username = ?", u.Username).First(&user).RecordNotFound()
  41. //notResigt为false表明读取到了 不能注册
  42. if !notResigt {
  43. return errors.New("用户名已注册"), nil
  44. } else {
  45. // 否则 附加uuid 密码md5简单加密 注册
  46. u.Password = utils.MD5V([]byte(u.Password))
  47. u.UUID = uuid.NewV4()
  48. err = global.GVA_DB.Create(u).Error
  49. }
  50. return err, u
  51. }
  52. // @title ChangePassword
  53. // @description change the password of a certain user, 修改用户密码
  54. // @auth (2020/04/05 20:22 )
  55. // @param newPassword string
  56. // @return err error
  57. // @return userInter *SysUser
  58. func (u *SysUser) ChangePassword(newPassword string) (err error, userInter *SysUser) {
  59. var user SysUser
  60. //后期修改jwt+password模式
  61. u.Password = utils.MD5V([]byte(u.Password))
  62. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", utils.MD5V([]byte(newPassword))).Error
  63. return err, u
  64. }
  65. // @title SetUserAuthority
  66. // @description set the authority of a certain user, 设置一个用户的权限
  67. // @auth (2020/04/05 20:22 )
  68. // @param uuid UUID
  69. // @param authorityId string
  70. // @return err error
  71. func (u *SysUser) SetUserAuthority(uuid uuid.UUID, authorityId string) (err error) {
  72. err = global.GVA_DB.Where("uuid = ?", uuid).First(&SysUser{}).Update("authority_id", authorityId).Error
  73. return err
  74. }
  75. // @title Login
  76. // @description login, 用户登录
  77. // @auth (2020/04/05 20:22 )
  78. // @return err error
  79. // @return userInter *SysUser
  80. func (u *SysUser) Login() (err error, userInter *SysUser) {
  81. var user SysUser
  82. u.Password = utils.MD5V([]byte(u.Password))
  83. err = global.GVA_DB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Error
  84. if err != nil {
  85. return err, &user
  86. }
  87. err = global.GVA_DB.Where("authority_id = ?", user.AuthorityId).First(&user.Authority).Error
  88. return err, &user
  89. }
  90. // @title UploadHeaderImg
  91. // @description upload avatar, 用户头像上传更新地址
  92. // @auth (2020/04/05 20:22 )
  93. // @param uuid UUID
  94. // @param filePath string
  95. // @return err error
  96. // @return userInter *SysUser
  97. func (u *SysUser) UploadHeaderImg(uuid uuid.UUID, filePath string) (err error, userInter *SysUser) {
  98. var user SysUser
  99. err = global.GVA_DB.Where("uuid = ?", uuid).First(&user).Update("header_img", filePath).First(&user).Error
  100. return err, &user
  101. }
  102. // @title GetInfoList
  103. // @description get user list by pagination, 分页获取数据
  104. // @auth (2020/04/05 20:22 )
  105. // @param PageInfo int
  106. // @return err error
  107. // @return list interface{}
  108. // @return total int
  109. func (u *SysUser) GetInfoList(info PageInfo) (err error, list interface{}, total int) {
  110. limit := info.PageSize
  111. offset := info.PageSize * (info.Page - 1)
  112. db := global.GVA_DB
  113. if err != nil {
  114. return
  115. } else {
  116. var userList []SysUser
  117. err = db.Limit(limit).Offset(offset).Preload("Authority").Find(&userList).Error
  118. return err, userList, total
  119. }
  120. }