add gorm update create hook

This commit is contained in:
landaiqing
2024-09-10 22:45:03 +08:00
parent e87a511ece
commit 5c30db51f1
7 changed files with 154 additions and 3 deletions

View File

@@ -122,6 +122,7 @@ func HandelUserLogin(userId string) (bool, map[string]interface{}) {
"data": data,
"success": true,
}
global.DB.Set("user_id", userId)
return true, responseData
}

View File

@@ -344,7 +344,7 @@ func handelUserLogin(user model.ScaAuthUser, autoLogin bool, c *gin.Context) {
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
return
}
global.DB.Set("user_id", user.UID)
result.OkWithData(data, c)
}

View File

@@ -2,6 +2,9 @@ package model
import (
"encoding/json"
"fmt"
"gorm.io/gorm"
"schisandra-cloud-album/global"
"time"
)
@@ -39,3 +42,36 @@ func (permission *ScaAuthPermission) MarshalBinary() ([]byte, error) {
func (permission *ScaAuthPermission) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, permission)
}
func (permission *ScaAuthPermission) BeforeCreate(tx *gorm.DB) (err error) {
userId, b := global.DB.Get("user_id")
if !b {
global.LOG.Error("user_id is not in global.DB")
return fmt.Errorf("user_id is not in global.DB")
}
userIdStr, ok := userId.(*string)
if !ok {
global.LOG.Error("user_id is not of type *string")
return fmt.Errorf("user_id is not of type *string")
}
permission.CreatedBy = userIdStr
return nil
}
func (permission *ScaAuthPermission) BeforeUpdate(tx *gorm.DB) (err error) {
userId, b := global.DB.Get("user_id")
if !b {
global.LOG.Error("user_id is not in global.DB")
return fmt.Errorf("user_id is not in global.DB")
}
userIdStr, ok := userId.(*string)
if !ok {
global.LOG.Error("user_id is not of type *string")
return fmt.Errorf("user_id is not of type *string")
}
permission.UpdateBy = userIdStr
return nil
}

View File

@@ -2,6 +2,9 @@ package model
import (
"encoding/json"
"fmt"
"gorm.io/gorm"
"schisandra-cloud-album/global"
"time"
)
@@ -31,3 +34,39 @@ func (role *ScaAuthRole) MarshalBinary() ([]byte, error) {
func (role *ScaAuthRole) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, role)
}
func (role *ScaAuthRole) BeforeCreate(tx *gorm.DB) (err error) {
userId, b := global.DB.Get("user_id")
if !b {
creator := "system"
role.CreatedBy = &creator
return nil
}
userIdStr, ok := userId.(*string)
if !ok {
global.LOG.Error("user_id is not of type *string")
return fmt.Errorf("user_id is not of type *string")
}
role.CreatedBy = userIdStr
return nil
}
func (role *ScaAuthRole) BeforeUpdate(tx *gorm.DB) (err error) {
userId, b := global.DB.Get("user_id")
if !b {
creator := "system"
role.CreatedBy = &creator
return nil
}
userIdStr, ok := userId.(*string)
if !ok {
global.LOG.Error("user_id is not of type *string")
return fmt.Errorf("user_id is not of type *string")
}
role.UpdateBy = userIdStr
return nil
}

View File

@@ -2,6 +2,9 @@ package model
import (
"encoding/json"
"fmt"
"gorm.io/gorm"
"schisandra-cloud-album/global"
"time"
)
@@ -42,3 +45,38 @@ func (user *ScaAuthUser) MarshalBinary() ([]byte, error) {
func (user *ScaAuthUser) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, user)
}
func (user *ScaAuthUser) BeforeCreate(tx *gorm.DB) (err error) {
userId, b := global.DB.Get("user_id")
if !b {
creator := "system"
user.CreatedBy = &creator
return nil
}
userIdStr, ok := userId.(*string)
if !ok {
global.LOG.Error("user_id is not of type *string")
return fmt.Errorf("user_id is not of type *string")
}
user.CreatedBy = userIdStr
return nil
}
func (user *ScaAuthUser) BeforeUpdate(tx *gorm.DB) (err error) {
userId, b := global.DB.Get("user_id")
if !b {
creator := "system"
user.CreatedBy = &creator
return nil
}
userIdStr, ok := userId.(*string)
if !ok {
global.LOG.Error("user_id is not of type *string")
return fmt.Errorf("user_id is not of type *string")
}
user.UpdateBy = userIdStr
return nil
}

View File

@@ -1,6 +1,9 @@
package model
import (
"fmt"
"gorm.io/gorm"
"schisandra-cloud-album/global"
"time"
)
@@ -25,7 +28,7 @@ type ScaAuthUserDevice struct {
Platform *string `gorm:"column:platform;type:varchar(20);comment:平台" json:"platform"` // 平台
EngineName *string `gorm:"column:engine_name;type:varchar(20);comment:引擎名称" json:"engine_name"` // 引擎名称
EngineVersion *string `gorm:"column:engine_version;type:varchar(20);comment:引擎版本" json:"engine_version"` // 引擎版本
CreatedBy *string `gorm:"column:created_by;type:varchar(32);comment:创建人" json:"created_by"` // 创建人
CreatedBy *string `gorm:"column:created_by;type:varchar(32);default:system;comment:创建人" json:"created_by"` // 创建人
UpdateBy *string `gorm:"column:update_by;type:varchar(32);comment:更新人" json:"update_by"`
}
@@ -33,3 +36,19 @@ type ScaAuthUserDevice struct {
func (*ScaAuthUserDevice) TableName() string {
return TableNameScaAuthUserDevice
}
func (device *ScaAuthUserDevice) BeforeUpdate(tx *gorm.DB) (err error) {
userId, b := global.DB.Get("user_id")
if !b {
global.LOG.Error("user_id is not found in global.DB")
return fmt.Errorf("user_id is not found in global.DB")
}
userIdStr, ok := userId.(*string)
if !ok {
global.LOG.Error("user_id is not of type *string")
return fmt.Errorf("user_id is not of type *string")
}
device.UpdateBy = userIdStr
return nil
}

View File

@@ -1,6 +1,9 @@
package model
import (
"fmt"
"gorm.io/gorm"
"schisandra-cloud-album/global"
"time"
)
@@ -16,7 +19,7 @@ type ScaAuthUserSocial struct {
CreatedTime *time.Time `gorm:"column:created_time;type:datetime;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_time"` // 创建时间
UpdateTime *time.Time `gorm:"column:update_time;type:datetime;default:CURRENT_TIMESTAMP;comment:更新时间" json:"update_time"` // 更新时间
Deleted *int64 `gorm:"column:deleted;type:int(11);default:0;comment:是否删除" json:"deleted"` // 是否删除
CreatedBy *string `gorm:"column:created_by;type:varchar(32);comment:创建人" json:"created_by"` // 创建人
CreatedBy *string `gorm:"column:created_by;type:varchar(32);default:system;comment:创建人" json:"created_by"` // 创建人
UpdateBy *string `gorm:"column:update_by;type:varchar(32);comment:更新人" json:"update_by"`
}
@@ -24,3 +27,18 @@ type ScaAuthUserSocial struct {
func (*ScaAuthUserSocial) TableName() string {
return TableNameScaAuthUserSocial
}
func (social *ScaAuthUserSocial) BeforeUpdate(tx *gorm.DB) (err error) {
userId, b := global.DB.Get("user_id")
if !b {
global.LOG.Error("user_id is not found in global.DB")
return fmt.Errorf("user_id is not found in global.DB")
}
userIdStr, ok := userId.(*string)
if !ok {
global.LOG.Error("user_id is not of type *string")
return fmt.Errorf("user_id is not of type *string")
}
social.UpdateBy = userIdStr
return nil
}