Files
slogan/common/auth_middleware.go
T
adminandClaude Opus 4.7 ef22f7672a feat: slogan-agent MVP 服务端完整实现
- 用户域:注册/登录(JWT)/修改密码/个人资料
- 照片/衣橱/身形/化身:上传存储 + 3D 化身模板匹配
- 穿搭生成:天气(高德+和风+缓存) → 规则预筛 → LLM 规划(1次调用)
  → 规则评分(5维100分制) → 全低分触发 LLM 兜底创作 → 异步任务状态机
- 效果图:选主方案后异步生成 3 视角(mock/wanx 供应商 + 内容 hash 缓存 + 每日限额)
- 商业化:合作门店列表(seed 4 家)
- 冒烟:全链路端到端验证通过(mock LLM/天气),23 个 API 端点

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-31 12:15:11 +08:00

91 lines
1.6 KiB
Go
Raw 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
import (
"net/http"
"strings"
"github.com/gogf/gf/v2/net/ghttp"
)
var publicPaths = []string{
"/user/login",
"/user/register",
"/hairstyle/list",
"/api.json",
}
func Auth(r *ghttp.Request) {
path := r.URL.Path
// 公开路径(精确匹配)
for _, p := range publicPaths {
if path == p {
r.Middleware.Next()
return
}
}
// workspace 文件通过前缀匹配放行(浏览器图片请求不带 Authorization
if strings.HasPrefix(path, "/workspace/") {
r.Middleware.Next()
return
}
auth := r.Header.Get("Authorization")
if auth == "" || !strings.HasPrefix(auth, "Bearer ") {
r.Response.WriteJson(ghttp.DefaultHandlerResponse{
Code: http.StatusUnauthorized,
Message: "未登录或登录已过期",
})
r.Exit()
return
}
claims, err := ParseToken(auth[7:])
if err != nil {
r.Response.WriteJson(ghttp.DefaultHandlerResponse{
Code: http.StatusUnauthorized,
Message: "登录已过期,请重新登录",
})
r.Exit()
return
}
r.SetCtxVar("userId", claims.UserId)
r.SetCtxVar("role", claims.Role)
r.SetCtxVar("agentId", claims.AgentId)
r.Middleware.Next()
}
func GetUserId(r *ghttp.Request) int64 {
v := r.GetCtxVar("userId")
if v == nil {
return 0
}
return v.Int64()
}
func GetRole(r *ghttp.Request) string {
v := r.GetCtxVar("role")
if v == nil {
return ""
}
return v.String()
}
func GetAgentId(r *ghttp.Request) int64 {
v := r.GetCtxVar("agentId")
if v == nil {
return 0
}
return v.Int64()
}
func CheckAdmin(r *ghttp.Request) bool {
return GetRole(r) == "admin"
}
func CheckAgent(r *ghttp.Request) bool {
return GetRole(r) == "agent"
}