sys_user.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package model
  2. import (
  3. model2 "cold-delivery/common/model"
  4. "errors"
  5. "fmt"
  6. coreModel "gogs.baozhida.cn/zoie/OAuth-core/model"
  7. "golang.org/x/crypto/bcrypt"
  8. "gorm.io/gorm"
  9. )
  10. var (
  11. UserTypeSys = "sys"
  12. UserTypeCustomer = "customer"
  13. SysUserTypeSys = 1
  14. SysUserTypeStoreSys = 2
  15. SysUserTypeDriver = 3
  16. SysUserTypeSalesman = 4
  17. )
  18. type SysUser struct {
  19. model2.Model
  20. coreModel.User
  21. Type int `json:"type" gorm:"size:128;comment:类型"` // 管理员1 门店管理员2 配送员3 4销售人员
  22. UserType string `json:"userType" gorm:"size:128;comment:用户类型"`
  23. Openid string `json:"openid" gorm:"size:128;comment:微信openid"` // 微信openid
  24. Dept SysDept `json:"dept" gorm:"->"` // 部门
  25. Role SysRole `json:"-" gorm:"-"` // 角色
  26. CreateUser SysUserOmit `json:"createUser" gorm:"->;foreignkey:CreateBy;references:Id"` // 角色
  27. model2.ControlBy
  28. model2.ModelTime
  29. }
  30. func (SysUser) TableName() string {
  31. return "sys_user"
  32. }
  33. func (e *SysUser) Generate() model2.ActiveRecord {
  34. o := *e
  35. return &o
  36. }
  37. func (e *SysUser) GetId() interface{} {
  38. return e.Id
  39. }
  40. // 加密
  41. func (e *SysUser) Encrypt() (err error) {
  42. if e.Password == "" {
  43. return
  44. }
  45. var hash []byte
  46. if hash, err = bcrypt.GenerateFromPassword([]byte(e.Password), bcrypt.DefaultCost); err != nil {
  47. return
  48. } else {
  49. e.Password = string(hash)
  50. return
  51. }
  52. }
  53. var (
  54. ErrForbidUpdateSysUser = errors.New("禁止修改系统用户")
  55. ErrForbidDeleteSysUser = errors.New("禁止删除系统用户")
  56. )
  57. func (e *SysUser) BeforeCreate(_ *gorm.DB) error {
  58. return e.Encrypt()
  59. }
  60. func (e *SysUser) BeforeUpdate(_ *gorm.DB) error {
  61. if e.Username == "admin" {
  62. return ErrForbidUpdateSysUser
  63. }
  64. var err error
  65. if e.Password != "" {
  66. err = e.Encrypt()
  67. }
  68. return err
  69. }
  70. func (e *SysUser) BeforeDelete(_ *gorm.DB) error {
  71. if e.Username == "admin" {
  72. return ErrForbidDeleteSysUser
  73. }
  74. return nil
  75. }
  76. func GetUserCacheKey(id int) string {
  77. return fmt.Sprintf("oauth:user:%d", id)
  78. }
  79. type SysUserOmit struct {
  80. Id int `json:"id,omitempty"` // 主键编码
  81. NickName string `json:"nickName,omitempty"` // 昵称
  82. Phone string `json:"phone,omitempty"` // 昵称
  83. }
  84. func (SysUserOmit) TableName() string {
  85. return "sys_user"
  86. }
  87. type SysUserForBind struct {
  88. model2.Model
  89. coreModel.User
  90. Type int `json:"type" gorm:"size:128;comment:类型"` // 管理员1 仓管2 司机3
  91. UserType string `json:"userType" gorm:"size:128;comment:用户类型"` // 系统用户-sys 客户-customer
  92. Openid string `json:"openid" gorm:"size:128;comment:微信openid"` // 微信openid
  93. IsBind bool
  94. model2.ControlBy
  95. model2.ModelTime
  96. }
  97. func (SysUserForBind) TableName() string {
  98. return "sys_user"
  99. }