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.

89 lines
2.9 KiB

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