1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package model
- import (
- model2 "Medical_OAuth/common/model"
- "errors"
- "fmt"
- "github.com/google/uuid"
- coreModel "gogs.baozhida.cn/zoie/OAuth-core/model"
- "golang.org/x/crypto/bcrypt"
- "gorm.io/gorm"
- )
- type SysUser struct {
- model2.Model
- coreModel.User
- Dept SysDept `json:"dept" gorm:"-"` // 部门
- Role SysRole `json:"role" gorm:"-"` // 角色
- //Post SysPost `json:"post" gorm:"-"` // 岗位
- model2.ControlBy
- model2.ModelTime
- }
- func (SysUser) TableName() string {
- return "sys_user"
- }
- func (e *SysUser) Generate() model2.ActiveRecord {
- o := *e
- return &o
- }
- func (e *SysUser) GetId() interface{} {
- return e.Id
- }
- // 加密
- func (e *SysUser) Encrypt() (err error) {
- if e.Password == "" {
- return
- }
- var hash []byte
- if hash, err = bcrypt.GenerateFromPassword([]byte(e.Password), bcrypt.DefaultCost); err != nil {
- return
- } else {
- e.Password = string(hash)
- return
- }
- }
- func (e *SysUser) BeforeCreate(_ *gorm.DB) error {
- e.Uuid = uuid.NewString()
- return e.Encrypt()
- }
- func (e *SysUser) BeforeUpdate(_ *gorm.DB) error {
- if e.Username == "admin" {
- return errors.New("禁止修改系统用户")
- }
- var err error
- if e.Password != "" {
- err = e.Encrypt()
- }
- return err
- }
- func (e *SysUser) BeforeDelete(_ *gorm.DB) error {
- if e.Username == "admin" {
- return errors.New("禁止删除系统用户")
- }
- return nil
- }
- func GetVerifyCodeCacheKey(phone string) string {
- return fmt.Sprintf("verify-code-%s", phone)
- }
- func GetUserCacheKey(id int) string {
- return fmt.Sprintf("oauth:user:%d", id)
- }
|