✨ add gorm update create hook
This commit is contained in:
@@ -122,6 +122,7 @@ func HandelUserLogin(userId string) (bool, map[string]interface{}) {
|
|||||||
"data": data,
|
"data": data,
|
||||||
"success": true,
|
"success": true,
|
||||||
}
|
}
|
||||||
|
global.DB.Set("user_id", userId)
|
||||||
return true, responseData
|
return true, responseData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -344,7 +344,7 @@ func handelUserLogin(user model.ScaAuthUser, autoLogin bool, c *gin.Context) {
|
|||||||
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
|
result.FailWithMessage(ginI18n.MustGetMessage(c, "LoginFailed"), c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
global.DB.Set("user_id", user.UID)
|
||||||
result.OkWithData(data, c)
|
result.OkWithData(data, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,6 +2,9 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,3 +42,36 @@ func (permission *ScaAuthPermission) MarshalBinary() ([]byte, error) {
|
|||||||
func (permission *ScaAuthPermission) UnmarshalBinary(data []byte) error {
|
func (permission *ScaAuthPermission) UnmarshalBinary(data []byte) error {
|
||||||
return json.Unmarshal(data, permission)
|
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
|
||||||
|
}
|
||||||
|
@@ -2,6 +2,9 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -31,3 +34,39 @@ func (role *ScaAuthRole) MarshalBinary() ([]byte, error) {
|
|||||||
func (role *ScaAuthRole) UnmarshalBinary(data []byte) error {
|
func (role *ScaAuthRole) UnmarshalBinary(data []byte) error {
|
||||||
return json.Unmarshal(data, role)
|
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
|
||||||
|
}
|
||||||
|
@@ -2,6 +2,9 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -42,3 +45,38 @@ func (user *ScaAuthUser) MarshalBinary() ([]byte, error) {
|
|||||||
func (user *ScaAuthUser) UnmarshalBinary(data []byte) error {
|
func (user *ScaAuthUser) UnmarshalBinary(data []byte) error {
|
||||||
return json.Unmarshal(data, user)
|
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
|
||||||
|
}
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -25,7 +28,7 @@ type ScaAuthUserDevice struct {
|
|||||||
Platform *string `gorm:"column:platform;type:varchar(20);comment:平台" json:"platform"` // 平台
|
Platform *string `gorm:"column:platform;type:varchar(20);comment:平台" json:"platform"` // 平台
|
||||||
EngineName *string `gorm:"column:engine_name;type:varchar(20);comment:引擎名称" json:"engine_name"` // 引擎名称
|
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"` // 引擎版本
|
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"`
|
UpdateBy *string `gorm:"column:update_by;type:varchar(32);comment:更新人" json:"update_by"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,3 +36,19 @@ type ScaAuthUserDevice struct {
|
|||||||
func (*ScaAuthUserDevice) TableName() string {
|
func (*ScaAuthUserDevice) TableName() string {
|
||||||
return TableNameScaAuthUserDevice
|
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
|
||||||
|
}
|
||||||
|
@@ -1,6 +1,9 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"schisandra-cloud-album/global"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,7 +19,7 @@ type ScaAuthUserSocial struct {
|
|||||||
CreatedTime *time.Time `gorm:"column:created_time;type:datetime;default:CURRENT_TIMESTAMP;comment:创建时间" json:"created_time"` // 创建时间
|
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"` // 更新时间
|
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"` // 是否删除
|
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"`
|
UpdateBy *string `gorm:"column:update_by;type:varchar(32);comment:更新人" json:"update_by"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,3 +27,18 @@ type ScaAuthUserSocial struct {
|
|||||||
func (*ScaAuthUserSocial) TableName() string {
|
func (*ScaAuthUserSocial) TableName() string {
|
||||||
return TableNameScaAuthUserSocial
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user