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.

90 lines
2.9 KiB

5 years ago
  1. package sysModel
  2. import (
  3. "gin-vue-admin/controller/servers"
  4. "gin-vue-admin/init/qmsql"
  5. "gin-vue-admin/model/modelInterface"
  6. "gin-vue-admin/tools"
  7. "github.com/jinzhu/gorm"
  8. "github.com/pkg/errors"
  9. uuid "github.com/satori/go.uuid"
  10. )
  11. type SysUser struct {
  12. gorm.Model
  13. UUID uuid.UUID `json:"uuid"`
  14. Username string `json:"userName"`
  15. Password string `json:"-"`
  16. NickName string `json:"nickName" gorm:"default:'QMPlusUser'"`
  17. HeaderImg string `json:"headerImg" gorm:"default:'http://www.henrongyi.top/avatar/lufu.jpg'"`
  18. Authority SysAuthority `json:"authority" gorm:"ForeignKey:AuthorityId;AssociationForeignKey:AuthorityId"`
  19. AuthorityId string `json:"authorityId" gorm:"default:888"`
  20. }
  21. //type Propertie struct {
  22. // gorm.Model
  23. //}
  24. //注册接口model方法
  25. func (u *SysUser) Regist() (err error, userInter *SysUser) {
  26. var user SysUser
  27. //判断用户名是否注册
  28. findErr := qmsql.DEFAULTDB.Where("username = ?", u.Username).First(&user).Error
  29. //err为nil表明读取到了 不能注册
  30. if findErr == nil {
  31. return errors.New("用户名已注册"), nil
  32. } else {
  33. // 否则 附加uuid 密码md5简单加密 注册
  34. u.Password = tools.MD5V(u.Password)
  35. u.UUID = uuid.NewV4()
  36. err = qmsql.DEFAULTDB.Create(u).Error
  37. }
  38. return err, u
  39. }
  40. //修改用户密码
  41. func (u *SysUser) ChangePassword(newPassword string) (err error, userInter *SysUser) {
  42. var user SysUser
  43. //后期修改jwt+password模式
  44. u.Password = tools.MD5V(u.Password)
  45. err = qmsql.DEFAULTDB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Update("password", tools.MD5V(newPassword)).Error
  46. return err, u
  47. }
  48. //用户更新接口
  49. func (u *SysUser) SetUserAuthority(uuid uuid.UUID, AuthorityId string) (err error) {
  50. err = qmsql.DEFAULTDB.Where("uuid = ?", uuid).First(&SysUser{}).Update("authority_id", AuthorityId).Error
  51. return err
  52. }
  53. //用户登录
  54. func (u *SysUser) Login() (err error, userInter *SysUser) {
  55. var user SysUser
  56. u.Password = tools.MD5V(u.Password)
  57. err = qmsql.DEFAULTDB.Where("username = ? AND password = ?", u.Username, u.Password).First(&user).Error
  58. if err != nil {
  59. return err, &user
  60. }
  61. err = qmsql.DEFAULTDB.Where("authority_id = ?", user.AuthorityId).First(&user.Authority).Error
  62. return err, &user
  63. }
  64. // 用户头像上传更新地址
  65. func (u *SysUser) UploadHeaderImg(uuid uuid.UUID, filePath string) (err error, userInter *SysUser) {
  66. var user SysUser
  67. err = qmsql.DEFAULTDB.Where("uuid = ?", uuid).First(&user).Update("header_img", filePath).First(&user).Error
  68. return err, &user
  69. }
  70. // 分页获取数据 需要分页实现这个接口即可
  71. func (u *SysUser) GetInfoList(info modelInterface.PageInfo) (err error, list interface{}, total int) {
  72. // 封装分页方法 调用即可 传入 当前的结构体和分页信息
  73. err, db, total := servers.PagingServer(u, info)
  74. if err != nil {
  75. return
  76. } else {
  77. var userList []SysUser
  78. err = db.Preload("Authority").Find(&userList).Error
  79. return err, userList, total
  80. }
  81. }