sys_user.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package model
  2. import (
  3. model2 "Medical_OAuth/common/model"
  4. "errors"
  5. "fmt"
  6. "github.com/google/uuid"
  7. coreModel "gogs.baozhida.cn/zoie/OAuth-core/model"
  8. "golang.org/x/crypto/bcrypt"
  9. "gorm.io/gorm"
  10. )
  11. type SysUser struct {
  12. model2.Model
  13. coreModel.User
  14. Dept SysDept `json:"dept" gorm:"-"` // 部门
  15. Role SysRole `json:"role" gorm:"-"` // 角色
  16. //Post SysPost `json:"post" gorm:"-"` // 岗位
  17. model2.ControlBy
  18. model2.ModelTime
  19. }
  20. func (SysUser) TableName() string {
  21. return "sys_user"
  22. }
  23. func (e *SysUser) Generate() model2.ActiveRecord {
  24. o := *e
  25. return &o
  26. }
  27. func (e *SysUser) GetId() interface{} {
  28. return e.Id
  29. }
  30. // 加密
  31. func (e *SysUser) Encrypt() (err error) {
  32. if e.Password == "" {
  33. return
  34. }
  35. var hash []byte
  36. if hash, err = bcrypt.GenerateFromPassword([]byte(e.Password), bcrypt.DefaultCost); err != nil {
  37. return
  38. } else {
  39. e.Password = string(hash)
  40. return
  41. }
  42. }
  43. func (e *SysUser) BeforeCreate(_ *gorm.DB) error {
  44. e.Uuid = uuid.NewString()
  45. return e.Encrypt()
  46. }
  47. func (e *SysUser) BeforeUpdate(_ *gorm.DB) error {
  48. if e.Username == "admin" {
  49. return errors.New("禁止修改系统用户")
  50. }
  51. var err error
  52. if e.Password != "" {
  53. err = e.Encrypt()
  54. }
  55. return err
  56. }
  57. func (e *SysUser) BeforeDelete(_ *gorm.DB) error {
  58. if e.Username == "admin" {
  59. return errors.New("禁止删除系统用户")
  60. }
  61. return nil
  62. }
  63. func GetVerifyCodeCacheKey(phone string) string {
  64. return fmt.Sprintf("verify-code-%s", phone)
  65. }
  66. func GetUserCacheKey(id int) string {
  67. return fmt.Sprintf("oauth:user:%d", id)
  68. }