refactor🎨 : 系统登录日志和操作日志代码优化
This commit is contained in:
1 parent
55112db749
commit
199fc180b7
23 files changed
+1081
-915
No files matched your search
@@ -1,105 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
orm "go-admin/common/global"
|
||||
)
|
||||
|
||||
type LoginLog struct {
|
||||
InfoId int `json:"infoId" gorm:"primary_key;auto_increment;"` //主键
|
||||
Username string `json:"username" gorm:"size:128;"` //用户名
|
||||
Status string `json:"status" gorm:"size:4;"` //状态
|
||||
Ipaddr string `json:"ipaddr" gorm:"size:255;"` //ip地址
|
||||
LoginLocation string `json:"loginLocation" gorm:"size:255;"` //归属地
|
||||
Browser string `json:"browser" gorm:"size:255;"` //浏览器
|
||||
Os string `json:"os" gorm:"size:255;"` //系统
|
||||
Platform string `json:"platform" gorm:"size:255;"` // 固件
|
||||
LoginTime time.Time `json:"loginTime" gorm:"type:timestamp;"` //登录时间
|
||||
CreateBy string `json:"createBy" gorm:"size:128;"` //创建人
|
||||
UpdateBy string `json:"updateBy" gorm:"size:128;"` //更新者
|
||||
DataScope string `json:"dataScope" gorm:"-"` //数据
|
||||
Params string `json:"params" gorm:"-"` //
|
||||
Remark string `json:"remark" gorm:"size:255;"` //备注
|
||||
Msg string `json:"msg" gorm:"size:255;"`
|
||||
BaseModel
|
||||
}
|
||||
|
||||
func (LoginLog) TableName() string {
|
||||
return "sys_loginlog"
|
||||
}
|
||||
|
||||
func (e *LoginLog) Get() (LoginLog, error) {
|
||||
var doc LoginLog
|
||||
|
||||
table := orm.Eloquent.Table(e.TableName())
|
||||
if e.Ipaddr != "" {
|
||||
table = table.Where("ipaddr = ?", e.Ipaddr)
|
||||
}
|
||||
if e.InfoId != 0 {
|
||||
table = table.Where("info_id = ?", e.InfoId)
|
||||
}
|
||||
|
||||
if err := table.First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *LoginLog) GetPage(pageSize int, pageIndex int) ([]LoginLog, int, error) {
|
||||
var doc []LoginLog
|
||||
|
||||
table := orm.Eloquent.Table(e.TableName())
|
||||
if e.Ipaddr != "" {
|
||||
table = table.Where("ipaddr = ?", e.Ipaddr)
|
||||
}
|
||||
if e.Status != "" {
|
||||
table = table.Where("status = ?", e.Status)
|
||||
}
|
||||
if e.Username != "" {
|
||||
table = table.Where("userName = ?", e.Username)
|
||||
}
|
||||
|
||||
var count int64
|
||||
|
||||
if err := table.Order("info_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
//table.Where("`deleted_at` IS NULL").Count(&count)
|
||||
return doc, int(count), nil
|
||||
}
|
||||
|
||||
func (e *LoginLog) Create() (LoginLog, error) {
|
||||
var doc LoginLog
|
||||
e.CreateBy = "0"
|
||||
e.UpdateBy = "0"
|
||||
result := orm.Eloquent.Table(e.TableName()).Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *LoginLog) Update(id int) (update LoginLog, err error) {
|
||||
|
||||
if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *LoginLog) BatchDelete(id []int) (Result bool, err error) {
|
||||
if err = orm.Eloquent.Table(e.TableName()).Where("info_id in (?)", id).Delete(&LoginLog{}).Error; err != nil {
|
||||
return
|
||||
}
|
||||
Result = true
|
||||
return
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
orm "go-admin/common/global"
|
||||
)
|
||||
|
||||
//sys_operlog
|
||||
type SysOperLog struct {
|
||||
OperId int `json:"operId" gorm:"primary_key;AUTO_INCREMENT"` //日志编码
|
||||
Title string `json:"title" gorm:"size:255;"` //操作模块
|
||||
BusinessType string `json:"businessType" gorm:"size:128;"` //操作类型
|
||||
BusinessTypes string `json:"businessTypes" gorm:"size:128;"`
|
||||
Method string `json:"method" gorm:"size:128;"` //函数
|
||||
RequestMethod string `json:"requestMethod" gorm:"size:128;"` //请求方式
|
||||
OperatorType string `json:"operatorType" gorm:"size:128;"` //操作类型
|
||||
OperName string `json:"operName" gorm:"size:128;"` //操作者
|
||||
DeptName string `json:"deptName" gorm:"size:128;"` //部门名称
|
||||
OperUrl string `json:"operUrl" gorm:"size:255;"` //访问地址
|
||||
OperIp string `json:"operIp" gorm:"size:128;"` //客户端ip
|
||||
OperLocation string `json:"operLocation" gorm:"size:128;"` //访问位置
|
||||
OperParam string `json:"operParam" gorm:"size:255;"` //请求参数
|
||||
Status string `json:"status" gorm:"size:4;"` //操作状态
|
||||
OperTime time.Time `json:"operTime" gorm:"type:timestamp;"` //操作时间
|
||||
JsonResult string `json:"jsonResult" gorm:"size:255;"` //返回数据
|
||||
CreateBy string `json:"createBy" gorm:"size:128;"` //创建人
|
||||
UpdateBy string `json:"updateBy" gorm:"size:128;"` //更新者
|
||||
DataScope string `json:"dataScope" gorm:"-"` //数据
|
||||
Params string `json:"params" gorm:"-"` //参数
|
||||
Remark string `json:"remark" gorm:"size:255;"` //备注
|
||||
LatencyTime string `json:"latencyime" gorm:"size:128;"` //耗时
|
||||
UserAgent string `json:"userAgent" gorm:"size:255;"` //ua
|
||||
BaseModel
|
||||
}
|
||||
|
||||
func (SysOperLog) TableName() string {
|
||||
return "sys_operlog"
|
||||
}
|
||||
|
||||
func (e *SysOperLog) Get() (SysOperLog, error) {
|
||||
var doc SysOperLog
|
||||
|
||||
table := orm.Eloquent.Table(e.TableName())
|
||||
if e.OperIp != "" {
|
||||
table = table.Where("oper_ip = ?", e.OperIp)
|
||||
}
|
||||
if e.OperId != 0 {
|
||||
table = table.Where("oper_id = ?", e.OperId)
|
||||
}
|
||||
|
||||
if err := table.First(&doc).Error; err != nil {
|
||||
return doc, err
|
||||
}
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysOperLog) GetPage(pageSize int, pageIndex int) ([]SysOperLog, int, error) {
|
||||
var doc []SysOperLog
|
||||
|
||||
table := orm.Eloquent.Table(e.TableName())
|
||||
if e.OperIp != "" {
|
||||
table = table.Where("oper_ip = ?", e.OperIp)
|
||||
}
|
||||
if e.Status != "" {
|
||||
table = table.Where("status = ?", e.Status)
|
||||
}
|
||||
if e.OperName != "" {
|
||||
table = table.Where("oper_name = ?", e.OperName)
|
||||
}
|
||||
if e.BusinessType != "" {
|
||||
table = table.Where("business_type = ?", e.BusinessType)
|
||||
}
|
||||
|
||||
var count int64
|
||||
|
||||
if err := table.Order("oper_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
//table.Where("`deleted_at` IS NULL").Count(&count)
|
||||
return doc, int(count), nil
|
||||
}
|
||||
|
||||
func (e *SysOperLog) Create() (SysOperLog, error) {
|
||||
var doc SysOperLog
|
||||
e.CreateBy = "0"
|
||||
e.UpdateBy = "0"
|
||||
result := orm.Eloquent.Table(e.TableName()).Create(&e)
|
||||
if result.Error != nil {
|
||||
err := result.Error
|
||||
return doc, err
|
||||
}
|
||||
doc = *e
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (e *SysOperLog) Update(id int) (update SysOperLog, err error) {
|
||||
if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
//参数1:是要修改的数据
|
||||
//参数2:是修改的数据
|
||||
if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (e *SysOperLog) BatchDelete(id []int) (Result bool, err error) {
|
||||
if err = orm.Eloquent.Table(e.TableName()).Where(" oper_id in (?)", id).Delete(&SysOperLog{}).Error; err != nil {
|
||||
return
|
||||
}
|
||||
Result = true
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-admin/common/models"
|
||||
|
||||
"time"
|
||||
)
|
||||
|
||||
type SysLoginLog struct {
|
||||
gorm.Model
|
||||
models.ControlBy
|
||||
|
||||
Username string `json:"username" gorm:"type:varchar(128);comment:用户名"` //
|
||||
Status string `json:"status" gorm:"type:varchar(4);comment:状态"` //
|
||||
Ipaddr string `json:"ipaddr" gorm:"type:varchar(255);comment:ip地址"` //
|
||||
LoginLocation string `json:"loginLocation" gorm:"type:varchar(255);comment:归属地"` //
|
||||
Browser string `json:"browser" gorm:"type:varchar(255);comment:浏览器"` //
|
||||
Os string `json:"os" gorm:"type:varchar(255);comment:系统"` //
|
||||
Platform string `json:"platform" gorm:"type:varchar(255);comment:固件"` //
|
||||
LoginTime time.Time `json:"loginTime" gorm:"type:timestamp;comment:登录时间"` //
|
||||
Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"` //
|
||||
Msg string `json:"msg" gorm:"type:varchar(255);comment:信息"` //
|
||||
}
|
||||
|
||||
func (SysLoginLog) TableName() string {
|
||||
return "sys_login_log"
|
||||
}
|
||||
|
||||
func (e *SysLoginLog) Generate() models.ActiveRecord {
|
||||
o := *e
|
||||
return &o
|
||||
}
|
||||
|
||||
func (e *SysLoginLog) GetId() interface{} {
|
||||
return e.ID
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"go-admin/common/models"
|
||||
|
||||
"time"
|
||||
)
|
||||
|
||||
type SysOperaLog struct {
|
||||
gorm.Model
|
||||
models.ControlBy
|
||||
|
||||
Title string `json:"title" gorm:"type:varchar(255);comment:操作模块"` //
|
||||
BusinessType string `json:"businessType" gorm:"type:varchar(128);comment:操作类型"` //
|
||||
BusinessTypes string `json:"businessTypes" gorm:"type:varchar(128);comment:BusinessTypes"` //
|
||||
Method string `json:"method" gorm:"type:varchar(128);comment:函数"` //
|
||||
RequestMethod string `json:"requestMethod" gorm:"type:varchar(128);comment:请求方式"` //
|
||||
OperatorType string `json:"operatorType" gorm:"type:varchar(128);comment:操作类型"` //
|
||||
OperName string `json:"operName" gorm:"type:varchar(128);comment:操作者"` //
|
||||
DeptName string `json:"deptName" gorm:"type:varchar(128);comment:部门名称"` //
|
||||
OperUrl string `json:"operUrl" gorm:"type:varchar(255);comment:访问地址"` //
|
||||
OperIp string `json:"operIp" gorm:"type:varchar(128);comment:客户端ip"` //
|
||||
OperLocation string `json:"operLocation" gorm:"type:varchar(128);comment:访问位置"` //
|
||||
OperParam string `json:"operParam" gorm:"type:varchar(255);comment:请求参数"` //
|
||||
Status string `json:"status" gorm:"type:varchar(4);comment:操作状态"` //
|
||||
OperTime time.Time `json:"operTime" gorm:"type:timestamp;comment:操作时间"` //
|
||||
JsonResult string `json:"jsonResult" gorm:"type:varchar(255);comment:返回数据"` //
|
||||
Remark string `json:"remark" gorm:"type:varchar(255);comment:备注"` //
|
||||
LatencyTime string `json:"latencyTime" gorm:"type:varchar(128);comment:耗时"` //
|
||||
UserAgent string `json:"userAgent" gorm:"type:varchar(255);comment:ua"` //
|
||||
}
|
||||
|
||||
func (SysOperaLog) TableName() string {
|
||||
return "sys_opera_log"
|
||||
}
|
||||
|
||||
func (e *SysOperaLog) Generate() models.ActiveRecord {
|
||||
o := *e
|
||||
return &o
|
||||
}
|
||||
|
||||
func (e *SysOperaLog) GetId() interface{} {
|
||||
return e.ID
|
||||
}
|
||||
Reference in new issue
Block a user