Files
mce/internal/common/context.go
Victor_Jay 2449a27eb5
Some checks failed
CI / Lint + Build (push) Has been cancelled
refactor: CSP nonce替代unsafe-inline, HSTS始终启用, router/api拆分, 管理后台+用户设置页拆分, DB健康降级, 时区配置, UID统一解析, CI接入
审计修复:
- CSP: unsafe-inline移除, nonce替代; unsafe-eval保留供Vditor使用
- HSTS: 始终启用(不再依赖release模式)
- Controller Exp: common.GetGinExp包装, 不再直接读context
- UID解析: common.ParseUIDParam统一, follow/admin controller改用

重构:
- router/api.go(198行)拆为7个域文件: auth/settings/posts/comments/reactions/social/studio
- 管理后台站点设置: 单页拆为4个子页(brand/security/registration/content)+子菜单
- 前台用户设置页: 1201行拆为6个独立模板+6个独立JS文件

新增:
- DB健康检测中间件(middleware/db_health.go): 5s ping+降级页面
- 时区配置: config.yaml server.timezone→time.Local初始化
- .gitea/workflows/ci.yml: strict模式CI流水线
2026-06-22 03:06:24 +08:00

51 lines
1.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package common 提供通用工具函数、错误哨兵和响应辅助方法。
package common
import (
"strconv"
"github.com/gin-gonic/gin"
)
// GetGinUser 从 Gin context 中提取当前登录用户的 uid 和 role
// 如果用户未登录ok 返回 falserole 可能为空字符串
func GetGinUser(c *gin.Context) (uid uint, role string, ok bool) {
if v, exists := c.Get("uid"); exists {
if u, isUint := v.(uint); isUint {
uid = u
ok = true
}
}
if v, exists := c.Get("role"); exists {
if r, isStr := v.(string); isStr {
role = r
}
}
return
}
// GetGinExp 从 Gin context 中提取当前用户的经验值
// 如果用户未登录或 exp 不存在ok 返回 false
func GetGinExp(c *gin.Context) (exp int, ok bool) {
if v, exists := c.Get("exp"); exists {
if e, isInt := v.(int); isInt {
return e, true
}
}
return 0, false
}
// ParseUIDParam 从 URL 路径参数中提取 uint 类型的 UID
// 统一 UID 解析逻辑,避免各 Controller 重复 strconv.ParseUint
func ParseUIDParam(c *gin.Context, paramName string) (uint, error) {
s := c.Param(paramName)
if s == "" {
return 0, ErrInvalidParam
}
v, err := strconv.ParseUint(s, 10, 64)
if err != nil {
return 0, ErrInvalidParam
}
return uint(v), nil
}