This commit is contained in:
2025-04-14 15:28:51 +08:00
parent f100427f8b
commit 371b89ee8d
93 changed files with 3757 additions and 1038 deletions

View File

@@ -1,5 +1,11 @@
package form
import (
"net/http"
"github.com/zhang2092/browser"
)
type Login struct {
Email string `form:"email" binding:"required,email"`
Password string `form:"password" binding:"required,min=6"`
@@ -14,6 +20,20 @@ type Login struct {
Url string
}
func (login Login) SetAttributes(r *http.Request) Login {
login.Ip = r.RemoteAddr
login.Referrer = r.Header.Get("Referer")
login.Url = r.URL.RequestURI()
br, err := browser.NewBrowser(r.Header.Get("User-Agent"))
if err == nil {
login.Os = br.Platform().Name()
login.Browser = br.Name()
}
return login
}
type User struct {
ID *int32 `form:"id" binding:"required"`
Email string `form:"email" binding:"required,email"`

View File

@@ -0,0 +1,71 @@
package system
import (
"context"
"net/http"
"strconv"
"strings"
"time"
"management/internal/db/model/dto"
)
type AuditLogRepository interface {
Create(ctx context.Context, obj *AuditLog) error
List(ctx context.Context, q dto.SearchDto) ([]*AuditLog, int64, error)
}
type AuditLog struct {
ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at"`
Email string `json:"email"`
StartAt time.Time `json:"start_at"`
EndAt time.Time `json:"end_at"`
Duration string `json:"duration"`
Url string `json:"url"`
Method string `json:"method"`
Parameters string `json:"parameters"`
RefererUrl string `json:"referer_url"`
Os string `json:"os"`
Ip string `json:"ip"`
Browser string `json:"browser"`
Remark string `json:"remark"`
}
func (AuditLog) TableName() string {
return "sys_audit_log"
}
func NewAuditLog(r *http.Request, email, os, browser string, start, end time.Time) *AuditLog {
var params string
method := r.Method
if method == "GET" {
params = r.URL.Query().Encode()
} else if method == "POST" {
contentType := r.Header.Get("Content-Type")
if strings.Contains(contentType, "application/json") {
body := make([]byte, r.ContentLength)
r.Body.Read(body)
params = string(body)
} else if strings.Contains(contentType, "application/x-www-form-urlencoded") {
params = r.Form.Encode()
}
}
duration := end.Sub(start)
return &AuditLog{
CreatedAt: time.Now(),
Email: email,
StartAt: start,
EndAt: end,
Duration: strconv.FormatInt(duration.Milliseconds(), 10),
Url: r.URL.RequestURI(),
Method: method,
Parameters: params,
RefererUrl: r.Header.Get("Referer"),
Os: os,
Ip: r.RemoteAddr,
Browser: browser,
}
}

View File

@@ -0,0 +1,28 @@
package system
import (
"context"
"time"
"management/internal/db/model/dto"
)
type ConfigRepository interface {
Create(ctx context.Context, obj *Config) error
Update(ctx context.Context, obj *Config) error
Get(ctx context.Context, id int32) (*Config, error)
GetByKey(ctx context.Context, key string) (*Config, error)
List(ctx context.Context, q dto.SearchDto) ([]*Config, int64, error)
}
type Config struct {
ID int32 `json:"id" gorm:"primaryKey;autoIncrement;not null"`
Key string `json:"key" gorm:"type:varchar(200);not null;uniqueIndex"`
Value []byte `json:"value" gorm:"type:bytea;not null;"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamptz;not null;default:'now()'"`
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamptz;not null;default:'0001-01-01 00:00:00+8';"`
}
func (Config) TableName() string {
return "sys_config"
}

View File

@@ -0,0 +1,32 @@
package system
import (
"context"
"time"
"management/internal/db/model/dto"
)
type DepartmentRepository interface {
Create(ctx context.Context, obj *Department) error
Update(ctx context.Context, obj *Department) error
Get(ctx context.Context, id int32) (*Department, error)
All(ctx context.Context) ([]*Department, error)
List(ctx context.Context, q dto.SearchDto) ([]*Department, int64, error)
RebuildParentPath(ctx context.Context) error
}
type Department struct {
ID int32 `json:"id" gorm:"primaryKey;autoIncrement;not null"`
Name string `json:"name" gorm:"type:varchar(200);not null;uniqueIndex"`
ParentID int32 `json:"parent_id" gorm:"type:int;not null;"`
ParentPath string `json:"parent_path" gorm:"type:varchar(500);not null;"`
Status int32 `json:"status" gorm:"type:int;not null;default:0;"`
Sort int32 `json:"sort" gorm:"type:int;not null;default:0;"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamptz;not null;default:'now()'"`
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamptz;not null;default:'0001-01-01 00:00:00+8';"`
}
func (Department) TableName() string {
return "sys_department"
}

View File

@@ -0,0 +1,56 @@
package system
import (
"context"
"time"
"management/internal/db/model/dto"
)
type LoginLogRepository interface {
Create(ctx context.Context, obj *LoginLog) error
GetLatest(ctx context.Context, email string) (*LoginLog, error)
List(ctx context.Context, q dto.SearchDto) ([]*LoginLog, int64, error)
Count(ctx context.Context, email string) (int64, error)
}
type LoginLog struct {
ID int64 `json:"id" gorm:"primaryKey;autoIncrement;not null"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamptz;not null;default:'now()'"`
Email string `json:"email" gorm:"type:varchar(100);not null;"`
IsSuccess bool `json:"is_success" gorm:"type:boolean;not null;"`
Message string `json:"message" gorm:"type:varchar(300);not null;"`
RefererUrl string `json:"referer_url" gorm:"type:varchar(500);not null;"`
Url string `json:"url" gorm:"type:varchar(500);not null;"`
Os string `json:"os" gorm:"type:varchar(50);not null;"`
Ip string `json:"ip" gorm:"type:varchar(20);not null;"`
Browser string `json:"browser" gorm:"type:varchar(100);not null;"`
}
func (LoginLog) TableName() string {
return "sys_user_login_log"
}
func NewLoginLog(email, os, ip, browser, url, referer string) *LoginLog {
return &LoginLog{
CreatedAt: time.Now(),
Email: email,
IsSuccess: false,
RefererUrl: referer,
Url: url,
Os: os,
Ip: ip,
Browser: browser,
}
}
func (l *LoginLog) SetMessage(message string) *LoginLog {
l.Message = message
return l
}
func (l *LoginLog) SetOk(message string) *LoginLog {
l.Message = message
l.IsSuccess = true
return l
}

View File

@@ -0,0 +1,37 @@
package system
import (
"context"
"time"
)
type MenuRepository interface {
Create(ctx context.Context, obj *Menu) error
Update(ctx context.Context, obj *Menu) error
Get(ctx context.Context, id int32) (*Menu, error)
GetByUrl(ctx context.Context, url string) (*Menu, error)
All(ctx context.Context) ([]*Menu, error)
RebuildParentPath(ctx context.Context) error
}
type Menu struct {
ID int32 `json:"id" gorm:"primaryKey;autoIncrement;not null"`
Name string `json:"name" gorm:"type:varchar(200);not null;uniqueIndex"`
DisplayName string `json:"display_name" gorm:"type:varchar(200);not null;uniqueIndex"`
Url string `json:"url" gorm:"type:varchar(200);not null;"`
Type string `json:"type" gorm:"type:varchar(50);not null;"`
ParentID int32 `json:"parent_id" gorm:"type:int;not null;"`
ParentPath string `json:"parent_path" gorm:"type:varchar(500);not null;"`
Avatar string `json:"avatar" gorm:"type:varchar(100);not null;"`
Style string `json:"style" gorm:"type:varchar(100);not null;"`
Visible bool `json:"visible" gorm:"type:boolean;not null;"`
IsList bool `json:"is_list" gorm:"type:boolean;not null;"`
Status int32 `json:"status" gorm:"type:int;not null;default:0;"`
Sort int32 `json:"sort" gorm:"type:int;not null;default:0;"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamptz;not null;default:'now()'"`
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamptz;not null;default:'0001-01-01 00:00:00+8';"`
}
func (Menu) TableName() string {
return "sys_menu"
}

View File

@@ -0,0 +1,34 @@
package system
import (
"context"
"time"
"management/internal/db/model/dto"
)
type RoleRepository interface {
Create(ctx context.Context, obj *Role) error
Update(ctx context.Context, obj *Role) error
Get(ctx context.Context, id int32) (*Role, error)
All(ctx context.Context) ([]*Role, error)
List(ctx context.Context, q dto.SearchDto) ([]*Role, int64, error)
RebuildParentPath(ctx context.Context) error
}
type Role struct {
ID int32 `json:"id" gorm:"primaryKey;autoIncrement;not null"`
Name string `json:"name" gorm:"type:varchar(200);not null;uniqueIndex"`
DisplayName string `json:"display_name" gorm:"type:varchar(200);not null;uniqueIndex"`
ParentID int32 `json:"parent_id" gorm:"type:int;not null;"`
ParentPath string `json:"parent_path" gorm:"type:varchar(500);not null;"`
Vip bool `json:"-" gorm:"type:boolean;not null;"`
Status int32 `json:"status" gorm:"type:int;not null;default:0;"`
Sort int32 `json:"sort" gorm:"type:int;not null;default:0;"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamptz;not null;default:'now()'"`
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamptz;not null;default:'0001-01-01 00:00:00+8';"`
}
func (Role) TableName() string {
return "sys_role"
}

View File

@@ -0,0 +1,18 @@
package system
import "context"
type RoleMenuRepository interface {
Create(ctx context.Context, obj []*RoleMenu) error
DeleteByRoleID(ctx context.Context, roleID int32) error
ListByRoleID(ctx context.Context, roleID int32) ([]*RoleMenu, error)
}
type RoleMenu struct {
RoleID int32 `json:"role_id" gorm:"primaryKey;autoIncrement:false;type:int;not null;"`
MenuID int32 `json:"menu_id" gorm:"primaryKey;autoIncrement:false;type:int;not null;"`
}
func (RoleMenu) TableName() string {
return "sys_role_menu"
}

View File

@@ -1,38 +1,41 @@
package system
import (
"context"
"time"
"management/internal/db/model/dto"
"github.com/google/uuid"
)
type UserRepository interface {
Create(ctx context.Context, obj *User) error
Update(ctx context.Context, obj *User) error
Get(ctx context.Context, id int32) (*User, error)
GetByEmail(ctx context.Context, email string) (*User, error)
All(ctx context.Context) ([]*User, error)
List(ctx context.Context, q dto.SearchDto) ([]*User, int64, error)
}
type User struct {
ID int32 `json:"id" gorm:"primaryKey"`
Uuid uuid.UUID `json:"uuid"`
// 邮箱地址
Email string `json:"email"`
// 用户名称
Username string `json:"username"`
// 加密密码
HashedPassword []byte `json:"hashed_password"`
// 密码盐值
Salt string `json:"salt"`
// 头像
Avatar string `json:"avatar"`
// 性别
Gender int32 `json:"gender"`
// 部门
DepartmentID int32 `json:"department_id"`
// 角色
RoleID int32 `json:"role_id"`
// 状态
Status int32 `json:"status"`
// 密码修改时间
ChangePasswordAt time.Time `json:"change_password_at"`
// 创建时间
CreatedAt time.Time `json:"created_at"`
// 更新时间
UpdatedAt time.Time `json:"updated_at"`
ID int32 `json:"id" gorm:"primaryKey;autoIncrement;not null"`
Uuid uuid.UUID `json:"uuid" gorm:"type:uuid;not null;uniqueIndex"`
Email string `json:"email" gorm:"type:varchar(100);not null;uniqueIndex"`
Username string `json:"username" gorm:"type:varchar(100);not null;uniqueIndex"`
HashedPassword []byte `json:"-" gorm:"type:bytea;not null;"`
Salt string `json:"-" gorm:"type:varchar(20);not null;"`
Avatar string `json:"avatar" gorm:"type:varchar(200);not null;"`
Gender int32 `json:"gender" gorm:"type:int;not null;default:0;"`
DepartmentID int32 `json:"department_id" gorm:"type:int;not null;default:0;"`
RoleID int32 `json:"role_id" gorm:"type:int;not null;default:0;"`
Status int32 `json:"status" gorm:"type:int;not null;default:0;"`
ChangePasswordAt time.Time `json:"-" gorm:"type:timestamptz;not null;default:'0001-01-01 00:00:00+8';"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamptz;not null;default:'now()'"`
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamptz;not null;default:'0001-01-01 00:00:00+8';"`
Role *Role `json:"role" gorm:"ForeignKey:RoleID"`
Department *Department `json:"department" gorm:"ForeignKey:DepartmentID"`
}
func (User) TableName() string {

View File

@@ -1,5 +1,7 @@
package view
import "time"
type LayuiTree struct {
ID string `json:"id"`
Title string `json:"title"`
@@ -17,3 +19,37 @@ type XmSelect struct {
Name string `json:"name"`
Value string `json:"value"`
}
type MenuTree struct {
ID int32 `json:"id"`
// 名称
Name string `json:"name"`
// 显示名称
DisplayName string `json:"display_name"`
// 菜单url
Url string `json:"url"`
// 菜单类型(node, menu, btn)
Type string `json:"type"`
// 上级id
ParentID int32 `json:"parent_id"`
// 树路径
ParentPath string `json:"parent_path"`
// 菜单图标
Avatar string `json:"avatar"`
// 菜单样式
Style string `json:"style"`
// 是否可见
Visible bool `json:"visible"`
// 是否列表
IsList bool `json:"is_list"`
// 状态
Status int32 `json:"status"`
// 排序
Sort int32 `json:"sort"`
// 创建时间
CreatedAt time.Time `json:"created_at"`
// 更新时间
UpdatedAt time.Time `json:"updated_at"`
Children []*MenuTree `json:"children"`
}