# SevenAdmin
**Repository Path**: illusoryNone/seven-admin
## Basic Information
- **Project Name**: SevenAdmin
- **Description**: SevenAdmin 是一个基于 Go + Vue3 的多租户管理后台系统,SaaS 应用设计。它提供了完整的基础功能和强大的代码生成工具,让你专注于业务开发而不是重复的基础代码。
- **Primary Language**: Go
- **License**: MIT
- **Default Branch**: v2
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 12
- **Forks**: 5
- **Created**: 2025-02-14
- **Last Updated**: 2025-12-29
## Categories & Tags
**Categories**: backend
**Tags**: Go语言, saas, pgsql, gorm, Redis
## README
# SevenAdmin
[](https://golang.org)
[](https://vuejs.org)
[](LICENSE)
[简体中文](README.md) | [English](README_en.md)
---
## 📖 项目简介
SevenAdmin 是一个基于 **Go + Vue3** 的多租户管理后台系统,采用 SaaS 应用设计。提供完整的基础功能和强大的代码生成工具,让你专注于业务开发。
### ✨ 核心特性
- **🏢 多租户架构** - 基于 Context 的透明租户隔离,全链路自动过滤
- **🛠️ 强大的 CLI** - 一键生成前后端完整代码(Model、Repository、DTO、Service、Controller、Vue、API)
- **🗄️ 多数据库支持** - 支持 SQL (PostgreSQL/MySQL/SQLite) 和 MongoDB,统一 API
- **🎯 完整基础功能** - 用户、角色、权限、菜单、租户、日志、文件管理等开箱即用
- **⚡ 技术栈** - Go + Gin + GORM/MongoDB + Vue3 + TypeScript + Naive UI
---
## 📸 系统截图
### 菜单管理

### 文件管理

### 租户管理

---
## 📋 功能列表
| 功能模块 | 功能说明 |
|---------|---------|
| **用户管理** | 用户 CRUD、角色分配、多部门、禁用/启用、重置密码、一键登录 |
| **租户管理** | 租户 CRUD、自动初始化、套餐配置、禁用/启用 |
| **角色管理** | 角色 CRUD、权限配置、角色分组、批量操作 |
| **菜单管理** | 动态菜单、权限绑定、树形管理 |
| **权限管理** | 基于 Casbin 的 RBAC、菜单权限、按钮权限、数据权限 |
| **字典管理** | 数据字典大类/小类、多列排序、导入/导出 |
| **操作日志** | 记录所有操作、请求追踪、审计功能、自动清理过期日志 |
| **文件管理** | 多存储支持(本地/OSS/MinIO/COS/S3)、上传/下载/预览/删除 |
| **消息系统** | SSE 实时消息推送、多租户消息管理、已读未读状态、消息中心 |
| **定时任务** | 基于 Cron 的任务调度、系统任务、自定义任务、执行日志、立即执行 |
---
## 🚀 快速开始
### 环境要求
| 组件 | 版本要求 |
|------|---------|
| Go | 1.21+ |
| Node.js | 18.x+ |
| PostgreSQL | 13+ (推荐) / MySQL 8+ / SQLite 3 / MongoDB 6+ |
### 1. 克隆项目
```bash
git clone https://gitee.com/illusoryNone/seven-admin.git
cd seven-admin
```
### 2. 启动后端
> **提示:** 无需手动导入数据库,系统会自动迁移表结构并初始化默认超级管理员、角色和菜单数据。
```bash
cd backend
# 安装依赖
go mod download
# 配置数据库(编辑 config/config.yaml)
vim config/config.yaml
# 运行项目
go run cmd/main.go
```
后端服务启动在 `http://localhost:8080`
### 3. 启动前端
```bash
cd frontend
# 安装依赖
pnpm install
# 启动开发服务器
pnpm dev
```
前端服务启动在 `http://localhost:5173`
### 4. 访问系统
打开浏览器访问 `http://localhost:5173`
默认账号:
- 用户名:`admin`
- 密码:`admin123`
---
## 🔧 核心功能详解
### 1. CLI 代码生成工具
一键生成标准化的前后端业务代码。
#### 编译工具
```bash
cd backend
go build -o generate cmd/generate/main.go
```
#### 生成 Model + Repository
```bash
# SQL 数据库(默认)
./generate model Product sys_product --comment=商品
# MongoDB 数据库
./generate model Product --db=mongo --comment=商品
# 非多租户模型
./generate model Tenant sys_tenant --comment=租户 --tenant=false
```
#### 生成完整应用层(后端)
```bash
# 为已有模型生成 DTO + Service + Controller
./generate app Product --module=Product --comment=商品
```
#### 生成前端代码
```bash
# 生成前端页面(TypeScript + Vue + API)
./generate frontend Product --module=product --comment=商品
# 简写形式
./generate fe Product --comment=商品
# 支持多种列表模式
./generate fe Menu --mode=tree --comment=菜单 # 树形列表
./generate fe Product --mode=card --comment=商品 # 卡片列表
./generate fe Banner --mode=draggable --comment=轮播图 # 可拖拽列表
```
**详细文档:** [CLI 工具使用指南](doc/cli.md)
---
### 2. 多租户系统
基于 Context 的透明租户隔离,开箱即用。
#### 请求链路
```
HTTP Request (Header: X-Tenant-ID)
↓
Middleware (注入 TenantID 到 Context)
↓
Controller (传递 ctx.Request.Context())
↓
Service (使用 ctx)
↓
Repository (自动应用租户过滤)
↓
Database (WHERE tenant_id = ?)
```
#### 使用示例
```go
// Controller 层
func (c *productController) List(ctx *gin.Context) {
var req dto.ProductListReq
if err := ctx.ShouldBindJSON(&req); err != nil {
response.FailWithMsg(ctx, response.ParamsValidError, "参数错误")
return
}
// 使用 ctx.Request.Context() 传递租户信息
data, err := c.srv.List(ctx.Request.Context(), &req)
response.CheckAndRespWithData(ctx, data, err)
}
// Service 层
func (s *productService) Create(ctx context.Context, req *dto.ProductCreateReq, operatorID uint) (*dto.ProductRes, error) {
entity := &models.Product{
TenantBaseModel: types.TenantBaseModel{
TenantID: 0, // 将由中间件自动设置
CreatedBy: operatorID,
},
Name: &req.Name,
Price: &req.Price,
}
// 从上下文获取租户ID
if tenantID, ok := database.GetTenantID(ctx); ok && tenantID > 0 {
entity.TenantID = tenantID
}
// Repository 会自动应用租户过滤
if err := s.repo.Create(ctx, entity); err != nil {
return nil, err
}
return s.toDTO(entity), nil
}
```
**详细文档:** [多租户使用指南](doc/multi-tenant-context.md)
---
### 3. Repository 数据访问层
现代化的数据访问层,SQL 和 MongoDB 使用统一 API。
#### 基础 CRUD
```go
// 创建
product := &models.Product{Name: "iPhone 15", Price: 5999}
err := productRepo.Create(ctx, product)
// 查询
product, err := productRepo.GetById(ctx, id)
// 更新
product.Price = 6999
err := productRepo.Update(ctx, product)
// 删除(软删除)
err := productRepo.Delete(ctx, id)
```
#### 高级查询
```go
// Filter 查询构建器(SQL 和 MongoDB 通用)
filter := types.NewFilter().
Set("status", 1). // WHERE status = 1
Set("price >=", 100). // AND price >= 100
Set("name LIKE", "%手机%"). // AND name LIKE '%手机%'
Preload("Category"). // 预加载分类
OrderBy("created_at DESC") // ORDER BY created_at DESC
// SQL 和 MongoDB 使用完全相同的 API
products, total, err := productRepo.GetPage(ctx, filter, page, pageSize)
// 统计
count, err := productRepo.Count(ctx, filter)
// 存在性检查
exists, err := productRepo.Exists(ctx, filter)
// 聚合查询
sum, err := orderRepo.Sum(ctx, "amount", filter)
avg, err := productRepo.Avg(ctx, "price", filter)
```
#### 事务支持
```go
// 事务自动提交/回滚
err := orderRepo.Transaction(ctx, func(txCtx context.Context) error {
// 创建订单
if err := orderRepo.Create(txCtx, order); err != nil {
return err
}
// 扣减库存
if err := productRepo.Update(txCtx, product); err != nil {
return err
}
// 返回 nil 自动提交,返回 error 自动回滚
return nil
})
```
**详细文档:** [Repository 使用指南](doc/repo.md)
---
## 📚 相关文档
| 文档 | 说明 |
|------|------|
| [CLI 工具使用指南](doc/cli.md) | 代码生成工具详细文档 |
| [多租户使用指南](doc/multi-tenant-context.md) | 多租户实现原理和使用方法 |
| [Repository 使用指南](doc/repo.md) | 数据访问层完整使用文档 |
| [定时任务调度系统](doc/schedule-system.md) | 定时任务开发和使用指南 |
| [路由和菜单指南](doc/route-and-menu-guide.md) | 路由和菜单配置说明 |
| [消息系统](doc/message-system.md) | SSE 实时消息推送系统 |
---
## 📄 开源协议
本项目采用 [MIT License](LICENSE) 开源协议。
---
## 📞 联系方式
- **Gitee**: [https://gitee.com/illusoryNone/seven-admin](https://gitee.com/illusoryNone/seven-admin)
- **Issues**: [提交问题](https://gitee.com/illusoryNone/seven-admin/issues)
---
**如果这个项目对你有帮助,请给一个 ⭐️ Star ⭐️**
Made with ❤️ by SevenAdmin Team