feat: 多班级版班级管理系统 v2.0
技术栈:Go (Gin + GORM) + PHP + MySQL 5.7 + Redis 主要功能: - 多班级完全隔离(class_id 贯穿全系统) - 后端从 Python FastAPI 重写为 Go Gin(端口 56789) - 超级管理员独立登录(env 配置路径,默认账密 admin/Admin123) - 科任老师/课代表新角色 - 课代表作业管理页面 - 排行榜分项排行(操行分/考勤/作业) - 角色加减分上下限由班主任配置 - 家长改密功能(可开关) - 班级角色按需开关 - 宿舍号格式:南0-000 - 周度/月度重置功能 - MySQL 5.7 兼容 - Nginx 反向代理部署 开发者: Canglan 版权归属: Sea Network Technology Studio 许可证: Apache License 2.0
This commit is contained in:
110
backend-go/pkg/crypto/password.go
Normal file
110
backend-go/pkg/crypto/password.go
Normal file
@@ -0,0 +1,110 @@
|
||||
// ===========================================
|
||||
// 多班级版班级管理系统 - Go 后端
|
||||
//
|
||||
// 开发者: Canglan
|
||||
// 联系方式: admin@sea-studio.top
|
||||
// 版权归属: Sea Network Technology Studio
|
||||
// 许可证: Apache License 2.0
|
||||
//
|
||||
// 版权所有 © Sea Network Technology Studio
|
||||
// ===========================================
|
||||
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
// HashPassword 密码哈希(与 Python 版完全兼容)
|
||||
// 算法: MD5(SHA1(password) + salt)
|
||||
// Python 参考: backend/utils/security.py -> sha1_md5_password()
|
||||
// 已知弱算法:MD5 和 SHA1 均不适合密码哈希场景,保留此实现仅为兼容 Python 版数据。
|
||||
// 后续迁移计划:迁移到 bcrypt/scrypt/argon2,并提供兼容层逐步过渡。
|
||||
func HashPassword(password string, salt string) string {
|
||||
// 第一层: SHA1(password)
|
||||
sha1Hash := sha1.Sum([]byte(password))
|
||||
sha1Hex := hex.EncodeToString(sha1Hash[:])
|
||||
|
||||
// 加盐: SHA1_hex + salt
|
||||
salted := sha1Hex + salt
|
||||
|
||||
// 第二层: MD5(salted)
|
||||
md5Hash := md5.Sum([]byte(salted))
|
||||
return hex.EncodeToString(md5Hash[:])
|
||||
}
|
||||
|
||||
// VerifyPassword 验证密码(使用常量时间比较,防止时序攻击)
|
||||
func VerifyPassword(plainPassword, hashedPassword, salt string) bool {
|
||||
computed := HashPassword(plainPassword, salt)
|
||||
return subtle.ConstantTimeCompare([]byte(computed), []byte(hashedPassword)) == 1
|
||||
}
|
||||
|
||||
// GenerateRandomPassword 生成随机密码
|
||||
// 与 Python 版 SecurityUtils.generate_random_password() 兼容
|
||||
func GenerateRandomPassword(length int) (string, error) {
|
||||
alphabet := "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
||||
result := make([]byte, length)
|
||||
for i := range result {
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphabet))))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("生成随机密码失败: %w", err)
|
||||
}
|
||||
result[i] = alphabet[n.Int64()]
|
||||
}
|
||||
return string(result), nil
|
||||
}
|
||||
|
||||
// ValidatePasswordStrength 验证密码强度
|
||||
// 要求: 大小写字母、数字、特殊符号至少包含 3 种,长度 6-20
|
||||
func ValidatePasswordStrength(password string) (bool, string) {
|
||||
if len(password) < 6 {
|
||||
return false, "密码长度至少6位"
|
||||
}
|
||||
if len(password) > 20 {
|
||||
return false, "密码长度不能超过20位"
|
||||
}
|
||||
|
||||
hasUpper := false
|
||||
hasLower := false
|
||||
hasDigit := false
|
||||
hasSpecial := false
|
||||
|
||||
for _, c := range password {
|
||||
switch {
|
||||
case c >= 'A' && c <= 'Z':
|
||||
hasUpper = true
|
||||
case c >= 'a' && c <= 'z':
|
||||
hasLower = true
|
||||
case c >= '0' && c <= '9':
|
||||
hasDigit = true
|
||||
default:
|
||||
hasSpecial = true
|
||||
}
|
||||
}
|
||||
|
||||
charTypes := 0
|
||||
if hasUpper {
|
||||
charTypes++
|
||||
}
|
||||
if hasLower {
|
||||
charTypes++
|
||||
}
|
||||
if hasDigit {
|
||||
charTypes++
|
||||
}
|
||||
if hasSpecial {
|
||||
charTypes++
|
||||
}
|
||||
|
||||
if charTypes < 3 {
|
||||
return false, "密码必须包含大写字母、小写字母、数字、特殊符号中的至少3种"
|
||||
}
|
||||
|
||||
return true, ""
|
||||
}
|
||||
71
backend-go/pkg/database/mysql.go
Normal file
71
backend-go/pkg/database/mysql.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// ===========================================
|
||||
// 多班级版班级管理系统 - Go 后端
|
||||
//
|
||||
// 开发者: Canglan
|
||||
// 联系方式: admin@sea-studio.top
|
||||
// 版权归属: Sea Network Technology Studio
|
||||
// 许可证: Apache License 2.0
|
||||
//
|
||||
// 版权所有 © Sea Network Technology Studio
|
||||
// ===========================================
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
|
||||
"hz-gitea.sea-studio.top/canglan/SharedClassManager/internal/config"
|
||||
)
|
||||
|
||||
// DB 全局数据库实例
|
||||
var DB *gorm.DB
|
||||
|
||||
// InitMySQL 初始化 MySQL 连接池
|
||||
func InitMySQL(cfg *config.Config) (*gorm.DB, error) {
|
||||
dsn := cfg.DSN()
|
||||
|
||||
// 根据 LogLevel 配置设置 GORM 日志级别
|
||||
gormLogLevel := logger.Info
|
||||
switch strings.ToLower(cfg.LogLevel) {
|
||||
case "silent":
|
||||
gormLogLevel = logger.Silent
|
||||
case "error":
|
||||
gormLogLevel = logger.Error
|
||||
case "warn", "warning":
|
||||
gormLogLevel = logger.Warn
|
||||
default:
|
||||
gormLogLevel = logger.Info
|
||||
}
|
||||
gormCfg := &gorm.Config{
|
||||
Logger: logger.Default.LogMode(gormLogLevel),
|
||||
}
|
||||
|
||||
db, err := gorm.Open(mysql.Open(dsn), gormCfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("连接数据库失败: %w", err)
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取底层 sql.DB 失败: %w", err)
|
||||
}
|
||||
|
||||
// 连接池配置
|
||||
sqlDB.SetMaxOpenConns(cfg.DBMaxOpenConns)
|
||||
sqlDB.SetMaxIdleConns(cfg.DBMaxIdleConns)
|
||||
sqlDB.SetConnMaxLifetime(time.Duration(cfg.DBConnMaxLife) * time.Second)
|
||||
|
||||
// 测试连接
|
||||
if err := sqlDB.Ping(); err != nil {
|
||||
return nil, fmt.Errorf("数据库 Ping 失败: %w", err)
|
||||
}
|
||||
|
||||
DB = db
|
||||
return db, nil
|
||||
}
|
||||
80
backend-go/pkg/database/redis.go
Normal file
80
backend-go/pkg/database/redis.go
Normal file
@@ -0,0 +1,80 @@
|
||||
// ===========================================
|
||||
// 多班级版班级管理系统 - Go 后端
|
||||
//
|
||||
// 开发者: Canglan
|
||||
// 联系方式: admin@sea-studio.top
|
||||
// 版权归属: Sea Network Technology Studio
|
||||
// 许可证: Apache License 2.0
|
||||
//
|
||||
// 版权所有 © Sea Network Technology Studio
|
||||
// ===========================================
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"hz-gitea.sea-studio.top/canglan/SharedClassManager/internal/config"
|
||||
)
|
||||
|
||||
// RDB 全局 Redis 客户端实例
|
||||
var RDB *redis.Client
|
||||
|
||||
// InitRedis 初始化 Redis 连接
|
||||
func InitRedis(cfg *config.Config) (*redis.Client, error) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: cfg.RedisAddr(),
|
||||
Password: cfg.RedisPassword,
|
||||
DB: cfg.RedisDB,
|
||||
PoolSize: cfg.RedisMaxConns,
|
||||
MinIdleConns: 5,
|
||||
DialTimeout: 5 * time.Second,
|
||||
ReadTimeout: 3 * time.Second,
|
||||
WriteTimeout: 3 * time.Second,
|
||||
})
|
||||
|
||||
// 测试连接
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||
return nil, fmt.Errorf("连接 Redis 失败: %w", err)
|
||||
}
|
||||
|
||||
RDB = rdb
|
||||
return rdb, nil
|
||||
}
|
||||
|
||||
// --- Token 存储操作(兼容 Python 版 Redis Token 管理) ---
|
||||
|
||||
const (
|
||||
tokenKeyPrefix = "user_token:"
|
||||
)
|
||||
|
||||
// SetUserToken 存储用户 Token
|
||||
func SetUserToken(ctx context.Context, userID int, token string, expireMinutes int) error {
|
||||
key := fmt.Sprintf("%s%d", tokenKeyPrefix, userID)
|
||||
return RDB.Set(ctx, key, token, time.Duration(expireMinutes)*time.Minute).Err()
|
||||
}
|
||||
|
||||
// GetUserToken 获取用户 Token
|
||||
func GetUserToken(ctx context.Context, userID int) (string, error) {
|
||||
key := fmt.Sprintf("%s%d", tokenKeyPrefix, userID)
|
||||
return RDB.Get(ctx, key).Result()
|
||||
}
|
||||
|
||||
// DeleteUserToken 删除用户 Token
|
||||
func DeleteUserToken(ctx context.Context, userID int) error {
|
||||
key := fmt.Sprintf("%s%d", tokenKeyPrefix, userID)
|
||||
return RDB.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
// ExpireToken 刷新 Token 过期时间(参数单位:分钟)
|
||||
func ExpireToken(ctx context.Context, userID int, expireMinutes int) error {
|
||||
key := fmt.Sprintf("%s%d", tokenKeyPrefix, userID)
|
||||
return RDB.Expire(ctx, key, time.Duration(expireMinutes)*time.Minute).Err()
|
||||
}
|
||||
93
backend-go/pkg/jwt/jwt.go
Normal file
93
backend-go/pkg/jwt/jwt.go
Normal file
@@ -0,0 +1,93 @@
|
||||
// ===========================================
|
||||
// 多班级版班级管理系统 - Go 后端
|
||||
//
|
||||
// 开发者: Canglan
|
||||
// 联系方式: admin@sea-studio.top
|
||||
// 版权归属: Sea Network Technology Studio
|
||||
// 许可证: Apache License 2.0
|
||||
//
|
||||
// 版权所有 © Sea Network Technology Studio
|
||||
// ===========================================
|
||||
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
goJwt "github.com/golang-jwt/jwt/v5"
|
||||
|
||||
"hz-gitea.sea-studio.top/canglan/SharedClassManager/internal/config"
|
||||
)
|
||||
|
||||
// getSigningMethod 根据配置返回对应的签名算法
|
||||
func getSigningMethod(algorithm string) goJwt.SigningMethod {
|
||||
switch algorithm {
|
||||
case "HS384":
|
||||
return goJwt.SigningMethodHS384
|
||||
case "HS512":
|
||||
return goJwt.SigningMethodHS512
|
||||
default:
|
||||
return goJwt.SigningMethodHS256
|
||||
}
|
||||
}
|
||||
|
||||
// Claims JWT 载荷结构(与 Python 版完全兼容)
|
||||
type Claims struct {
|
||||
UserID int `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
UserType string `json:"user_type"`
|
||||
StudentID *int `json:"student_id"`
|
||||
Role string `json:"role"`
|
||||
RealName string `json:"real_name"`
|
||||
ClassID *int `json:"class_id"`
|
||||
NeedChangePassword bool `json:"need_change_password"`
|
||||
goJwt.RegisteredClaims
|
||||
}
|
||||
|
||||
// CreateToken 创建 JWT Token
|
||||
func CreateToken(userID int, username, userType string, studentID *int, role, realName string, classID *int, needChangePassword bool) (string, error) {
|
||||
now := time.Now()
|
||||
cfg := config.AppConfig
|
||||
|
||||
claims := Claims{
|
||||
UserID: userID,
|
||||
Username: username,
|
||||
UserType: userType,
|
||||
StudentID: studentID,
|
||||
Role: role,
|
||||
RealName: realName,
|
||||
ClassID: classID,
|
||||
NeedChangePassword: needChangePassword,
|
||||
RegisteredClaims: goJwt.RegisteredClaims{
|
||||
ExpiresAt: goJwt.NewNumericDate(now.Add(time.Duration(cfg.JWTExpireMinutes) * time.Minute)),
|
||||
IssuedAt: goJwt.NewNumericDate(now),
|
||||
Issuer: cfg.AppName,
|
||||
},
|
||||
}
|
||||
|
||||
token := goJwt.NewWithClaims(getSigningMethod(cfg.JWTAlgorithm), claims)
|
||||
return token.SignedString([]byte(cfg.JWTSecretKey))
|
||||
}
|
||||
|
||||
// VerifyToken 验证 JWT Token,返回解析后的载荷
|
||||
func VerifyToken(tokenStr string) (*Claims, error) {
|
||||
cfg := config.AppConfig
|
||||
|
||||
token, err := goJwt.ParseWithClaims(tokenStr, &Claims{}, func(t *goJwt.Token) (interface{}, error) {
|
||||
if _, ok := t.Method.(*goJwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("不支持的签名算法: %v", t.Header["alg"])
|
||||
}
|
||||
return []byte(cfg.JWTSecretKey), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("token 验证失败: %w", err)
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(*Claims)
|
||||
if !ok || !token.Valid {
|
||||
return nil, fmt.Errorf("token 无效")
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
64
backend-go/pkg/logger/logger.go
Normal file
64
backend-go/pkg/logger/logger.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
// Log 全局日志实例
|
||||
var Log *zap.Logger
|
||||
|
||||
// Sugared 全局 SugaredLogger(便捷方法)
|
||||
var Sugared *zap.SugaredLogger
|
||||
|
||||
// Init 初始化日志
|
||||
func Init(level string, isProduction bool) {
|
||||
var zapLevel zapcore.Level
|
||||
switch level {
|
||||
case "debug":
|
||||
zapLevel = zapcore.DebugLevel
|
||||
case "info":
|
||||
zapLevel = zapcore.InfoLevel
|
||||
case "warn":
|
||||
zapLevel = zapcore.WarnLevel
|
||||
case "error":
|
||||
zapLevel = zapcore.ErrorLevel
|
||||
default:
|
||||
zapLevel = zapcore.InfoLevel
|
||||
}
|
||||
|
||||
encoderCfg := zap.NewProductionEncoderConfig()
|
||||
encoderCfg.TimeKey = "time"
|
||||
encoderCfg.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
encoderCfg.EncodeLevel = zapcore.CapitalLevelEncoder
|
||||
|
||||
var core zapcore.Core
|
||||
if isProduction {
|
||||
// 生产环境:JSON 格式输出到 stdout
|
||||
core = zapcore.NewCore(
|
||||
zapcore.NewJSONEncoder(encoderCfg),
|
||||
zapcore.Lock(os.Stdout),
|
||||
zapLevel,
|
||||
)
|
||||
} else {
|
||||
// 开发环境:Console 格式输出
|
||||
encoderCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
core = zapcore.NewCore(
|
||||
zapcore.NewConsoleEncoder(encoderCfg),
|
||||
zapcore.Lock(os.Stdout),
|
||||
zapLevel,
|
||||
)
|
||||
}
|
||||
|
||||
Log = zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
|
||||
Sugared = Log.Sugar()
|
||||
}
|
||||
|
||||
// Sync 刷新日志缓冲区
|
||||
func Sync() {
|
||||
if Log != nil {
|
||||
_ = Log.Sync()
|
||||
}
|
||||
}
|
||||
106
backend-go/pkg/response/response.go
Normal file
106
backend-go/pkg/response/response.go
Normal file
@@ -0,0 +1,106 @@
|
||||
// ===========================================
|
||||
// 多班级版班级管理系统 - Go 后端
|
||||
//
|
||||
// 开发者: Canglan
|
||||
// 联系方式: admin@sea-studio.top
|
||||
// 版权归属: Sea Network Technology Studio
|
||||
// 许可证: Apache License 2.0
|
||||
//
|
||||
// 版权所有 © Sea Network Technology Studio
|
||||
// ===========================================
|
||||
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Response 统一响应结构体
|
||||
type Response struct {
|
||||
Success bool `json:"success"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
// PageData 分页响应数据
|
||||
type PageData struct {
|
||||
Items interface{} `json:"items"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
}
|
||||
|
||||
// JSON 统一 JSON 响应
|
||||
func JSON(c *gin.Context, httpCode int, success bool, code int, message string, data interface{}) {
|
||||
c.JSON(httpCode, Response{
|
||||
Success: success,
|
||||
Code: code,
|
||||
Message: message,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Success 成功响应 (200)
|
||||
func Success(c *gin.Context, data interface{}, message string) {
|
||||
JSON(c, http.StatusOK, true, 200, message, data)
|
||||
}
|
||||
|
||||
// SuccessWithMessage 成功响应(仅消息)
|
||||
func SuccessWithMessage(c *gin.Context, message string) {
|
||||
JSON(c, http.StatusOK, true, 200, message, nil)
|
||||
}
|
||||
|
||||
// Created 创建成功响应 (201)
|
||||
func Created(c *gin.Context, data interface{}, message string) {
|
||||
JSON(c, http.StatusCreated, true, 201, message, data)
|
||||
}
|
||||
|
||||
// BadRequest 参数错误 (400)
|
||||
func BadRequest(c *gin.Context, message string) {
|
||||
JSON(c, http.StatusBadRequest, false, 400, message, nil)
|
||||
}
|
||||
|
||||
// Unauthorized 未授权 (401)
|
||||
func Unauthorized(c *gin.Context, message string) {
|
||||
JSON(c, http.StatusUnauthorized, false, 401, message, nil)
|
||||
}
|
||||
|
||||
// Forbidden 禁止访问 (403)
|
||||
func Forbidden(c *gin.Context, message string) {
|
||||
JSON(c, http.StatusForbidden, false, 403, message, nil)
|
||||
}
|
||||
|
||||
// NotFound 资源不存在 (404)
|
||||
func NotFound(c *gin.Context, message string) {
|
||||
JSON(c, http.StatusNotFound, false, 404, message, nil)
|
||||
}
|
||||
|
||||
// Conflict 冲突 (409)
|
||||
func Conflict(c *gin.Context, message string) {
|
||||
JSON(c, http.StatusConflict, false, 409, message, nil)
|
||||
}
|
||||
|
||||
// InternalError 服务器内部错误 (500)
|
||||
func InternalError(c *gin.Context, message string) {
|
||||
JSON(c, http.StatusInternalServerError, false, 500, message, nil)
|
||||
}
|
||||
|
||||
// Paginated 分页成功响应
|
||||
func Paginated(c *gin.Context, items interface{}, total int64, page, pageSize int) {
|
||||
totalPages := int(total) / pageSize
|
||||
if int(total)%pageSize > 0 {
|
||||
totalPages++
|
||||
}
|
||||
|
||||
Success(c, PageData{
|
||||
Items: items,
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
TotalPages: totalPages,
|
||||
}, "操作成功")
|
||||
}
|
||||
Reference in New Issue
Block a user