This commit is contained in:
oneao committed 2026-02-25 17:31:09 +08:00
1 parent 835958886e
commit d04d22723b
155 files changed
+543 -9793

No files matched your search

@@ -0,0 +1,28 @@
// Code generated by hertz generator. DO NOT EDIT.
package auth
import (
auth "allapp/biz/handler/auth"
"github.com/cloudwego/hertz/pkg/app/server"
)
/*
This file will register all the routes of the services in the master idl.
And it will update automatically when you use the "update" command for the idl.
So don't modify the contents of the file, or your code will be deleted when it is updated.
*/
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
{
_auth := root.Group("/auth", _authMw()...)
{
_login := _auth.Group("/login", _loginMw()...)
_login.POST("/qq", append(_loginqqMw(), auth.LoginQq)...)
_login.POST("/wx", append(_loginwxMw(), auth.LoginWx)...)
}
}
}
@@ -0,0 +1,62 @@
// Code generated by hertz generator.
package auth
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _authMw() []app.HandlerFunc {
// your code...
return nil
}
func _loginMw() []app.HandlerFunc {
// your code...
return nil
}
func _logindefaultMw() []app.HandlerFunc {
// your code...
return nil
}
func _registerMw() []app.HandlerFunc {
// your code...
return nil
}
func _registerdefaultMw() []app.HandlerFunc {
// your code...
return nil
}
func _refreshaccesstokenMw() []app.HandlerFunc {
// your code...
return nil
}
func _refreshMw() []app.HandlerFunc {
// your code...
return nil
}
func _loginthirdpartyMw() []app.HandlerFunc {
// your code...
return nil
}
func _loginqqMw() []app.HandlerFunc {
// your code...
return nil
}
func _loginwxMw() []app.HandlerFunc {
// your code...
return nil
}
@@ -0,0 +1,78 @@
package middleware
import (
"allapp/utils/jwtx"
"allapp/utils/pathx"
"allapp/utils/response"
"context"
"strings"
"github.com/cloudwego/hertz/pkg/app"
)
// 私有 key,防止冲突
type ctxKeyUser struct{}
// ContextUser 保存用户信息,包括 UserID、DeviceUUID 和当前 SpaceID
type ContextUser struct {
UserID int64
DeviceUUID string
}
// Auth 中间件,支持 header 中传 spaceid 并校验权限
func Auth() app.HandlerFunc {
whitelistPaths := []string{
"/auth/login",
"/auth/register",
"/auth/refresh/token",
"/ws",
}
return func(ctx context.Context, c *app.RequestContext) {
path := string(c.Request.URI().Path())
if pathx.MatchPath(path, whitelistPaths) {
c.Next(ctx)
return
}
// 校验 token
token := strings.TrimSpace(string(c.GetHeader("Token")))
verifyToken, flag := jwtx.VerifyToken(token)
if !flag {
response.Fail(c).Code(response.HttpCode.Unauthorized).Send()
c.Abort()
return
}
userID := verifyToken.UserID
// 设置 context
user := &ContextUser{
UserID: userID,
}
ctx = setContextUser(ctx, user)
c.Next(ctx)
}
}
// GetContextUser 从 context 获取用户信息
func GetContextUser(ctx context.Context) *ContextUser {
user, ok := ctx.Value(ctxKeyUser{}).(*ContextUser)
if !ok || user == nil {
return &ContextUser{}
}
return user
}
// GetUserID 从 context 获取 user_id
func GetUserID(ctx context.Context) int64 {
return GetContextUser(ctx).UserID
}
// 内部中间件设置 ContextUser
func setContextUser(ctx context.Context, user *ContextUser) context.Context {
return context.WithValue(ctx, ctxKeyUser{}, user)
}
@@ -0,0 +1,100 @@
package middleware
import (
"allapp/logx"
"allapp/utils/response"
"context"
"fmt"
"go.uber.org/zap"
"runtime/debug"
"strings"
"github.com/cloudwego/hertz/pkg/app"
"github.com/pkg/errors"
)
// ErrorInfo 封装错误信息和调用栈
type ErrorInfo struct {
Message string // 错误信息
FuncName string // 出错函数
File string // 文件路径
Line string // 行号
Stack string // 完整堆栈信息
}
// Error 中间件
func Error() app.HandlerFunc {
return func(ctx context.Context, c *app.RequestContext) {
c.Next(ctx)
if e := c.Errors.Last(); e != nil && e.Err != nil {
info := ParseError(e.Err)
logx.CtxError(ctx, "系统异常",
zap.String("Message", info.Message),
zap.String("File", info.File),
zap.String("Line", info.Line),
zap.String("Func", info.FuncName),
zap.String("Stack", info.Stack),
)
// 返回给前端
response.Fail(c).Message("系统异常,请联系管理员!").Send()
}
}
}
// ParseError 解析 error 为 ErrorInfo
func ParseError(err error) *ErrorInfo {
if err == nil {
return nil
}
info := &ErrorInfo{
Message: err.Error(),
Stack: string(debug.Stack()), // 默认 runtime stack
}
// 尝试解析 pkg/errors stack
type stackTracer interface {
StackTrace() errors.StackTrace
}
if errWithStack, ok := err.(stackTracer); ok {
st := errWithStack.StackTrace()
var funcName, fileLine string
for _, f := range st {
line := fmt.Sprintf("%+v", f)
// 取 biz/ 目录下第一个函数作为业务函数
if strings.Contains(line, "/biz/") {
parts := strings.Split(line, "\n")
if len(parts) >= 2 {
funcName = strings.TrimSpace(parts[0])
fileLine = strings.TrimSpace(parts[1])
} else {
funcName = strings.TrimSpace(line)
fileLine = strings.TrimSpace(line)
}
break
}
}
info.FuncName = funcName
// 分离文件和行号
if idx := strings.LastIndex(fileLine, ":"); idx > 0 {
info.File = fileLine[:idx]
info.Line = fileLine[idx+1:]
} else {
info.File = fileLine
info.Line = ""
}
// 完整堆栈
stackStr := ""
for _, f := range st {
stackStr += fmt.Sprintf("%+v\n", f)
}
info.Stack = stackStr
}
return info
}
@@ -0,0 +1,35 @@
package middleware
import (
"allapp/logx"
"context"
"time"
"github.com/cloudwego/hertz/pkg/app"
"github.com/hertz-contrib/requestid"
"go.uber.org/zap"
)
func Log() app.HandlerFunc {
return func(c context.Context, ctx *app.RequestContext) {
reqId := requestid.Get(ctx)
c = context.WithValue(c, "RequestID", reqId)
start := time.Now() // 记录开始时间
// 执行后续处理
ctx.Next(c)
// 计算耗时
duration := time.Since(start).Milliseconds()
logx.CtxInfo(c, "请求完成",
zap.String("IP", ctx.ClientIP()),
zap.String("Method", string(ctx.Method())),
zap.String("Path", string(ctx.Path())),
zap.Int("Status", ctx.GetResponse().StatusCode()),
zap.Int64("Duration", duration),
)
}
}
@@ -0,0 +1,30 @@
package middleware
import (
"allapp/db"
"allapp/logx"
"context"
"github.com/cloudwego/hertz/pkg/app"
)
func RefreshActive() app.HandlerFunc {
return func(c context.Context, ctx *app.RequestContext) {
// 先执行后续 handler
ctx.Next(c)
userID := GetUserID(c)
if userID == 0 {
return
}
// 异步刷新活跃时间
go func(userID int64) {
ctx := context.Background()
if err := db.Queries.UpdateUserLastLoginTime(ctx, userID); err != nil {
logx.CtxError(ctx, "刷新用户活跃时间失败, userID=%d, err=%v", userID, err)
}
}(userID)
}
}
@@ -0,0 +1,102 @@
// Code generated by hertz generator.
package money
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _moneyMw() []app.HandlerFunc {
// your code...
return nil
}
func _categoryMw() []app.HandlerFunc {
// your code...
return nil
}
func _queryusermoneycategoryMw() []app.HandlerFunc {
// your code...
return nil
}
func _deleteusermoneycategoryMw() []app.HandlerFunc {
// your code...
return nil
}
func _updatemoneycategorysortMw() []app.HandlerFunc {
// your code...
return nil
}
func _querymoneyrecordMw() []app.HandlerFunc {
// your code...
return nil
}
func _recordMw() []app.HandlerFunc {
// your code...
return nil
}
func _insertmoneyrecordMw() []app.HandlerFunc {
// your code...
return nil
}
func _updatemoneyrecordMw() []app.HandlerFunc {
// your code...
return nil
}
func _deletemoneyrecordMw() []app.HandlerFunc {
// your code...
return nil
}
func _createmoneycategoryMw() []app.HandlerFunc {
// your code...
return nil
}
func _listMw() []app.HandlerFunc {
// your code...
return nil
}
func _updatemoneycategoryMw() []app.HandlerFunc {
// your code...
return nil
}
func _listmoneyrecordsMw() []app.HandlerFunc {
// your code...
return nil
}
func _analysismoneyMw() []app.HandlerFunc {
// your code...
return nil
}
func _analysisMw() []app.HandlerFunc {
// your code...
return nil
}
func _analysismoneylineMw() []app.HandlerFunc {
// your code...
return nil
}
func _analysismoneypieMw() []app.HandlerFunc {
// your code...
return nil
}
@@ -0,0 +1,41 @@
// Code generated by hertz generator. DO NOT EDIT.
package money
import (
money "allapp/biz/handler/money"
"github.com/cloudwego/hertz/pkg/app/server"
)
/*
This file will register all the routes of the services in the master idl.
And it will update automatically when you use the "update" command for the idl.
So don't modify the contents of the file, or your code will be deleted when it is updated.
*/
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
{
_money := root.Group("/money", _moneyMw()...)
_money.GET("/category", append(_listMw(), money.List)...)
_category := _money.Group("/category", _categoryMw()...)
_category.POST("/create", append(_createmoneycategoryMw(), money.CreateMoneyCategory)...)
_category.POST("/delete", append(_deleteusermoneycategoryMw(), money.DeleteUserMoneyCategory)...)
_category.POST("/update", append(_updatemoneycategoryMw(), money.UpdateMoneyCategory)...)
_category.POST("/updateSort", append(_updatemoneycategorysortMw(), money.UpdateMoneyCategorySort)...)
_money.POST("/records", append(_listmoneyrecordsMw(), money.ListMoneyRecords)...)
{
_analysis := _money.Group("/analysis", _analysisMw()...)
_analysis.POST("/line", append(_analysismoneylineMw(), money.AnalysisMoneyLine)...)
_analysis.POST("/pie", append(_analysismoneypieMw(), money.AnalysisMoneyPie)...)
}
{
_record := _money.Group("/record", _recordMw()...)
_record.POST("/delete", append(_deletemoneyrecordMw(), money.DeleteMoneyRecord)...)
_record.POST("/insert", append(_insertmoneyrecordMw(), money.InsertMoneyRecord)...)
_record.POST("/update", append(_updatemoneyrecordMw(), money.UpdateMoneyRecord)...)
}
}
}
@@ -0,0 +1,22 @@
// Code generated by hertz generator. DO NOT EDIT.
package router
import (
auth "allapp/biz/router/auth"
money "allapp/biz/router/money"
space "allapp/biz/router/space"
user "allapp/biz/router/user"
"github.com/cloudwego/hertz/pkg/app/server"
)
// GeneratedRegister registers routers generated by IDL.
func GeneratedRegister(r *server.Hertz) {
//INSERT_POINT: DO NOT DELETE THIS LINE!
user.Register(r)
space.Register(r)
auth.Register(r)
money.Register(r)
}
@@ -0,0 +1,72 @@
// Code generated by hertz generator.
package space
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _spaceMw() []app.HandlerFunc {
// your code...
return nil
}
func _createspaceMw() []app.HandlerFunc {
// your code...
return nil
}
func _dissolvespaceMw() []app.HandlerFunc {
// your code...
return nil
}
func _exitspaceMw() []app.HandlerFunc {
// your code...
return nil
}
func _joinspaceMw() []app.HandlerFunc {
// your code...
return nil
}
func _removespaceuserMw() []app.HandlerFunc {
// your code...
return nil
}
func _listspaceusersMw() []app.HandlerFunc {
// your code...
return nil
}
func _updateroleMw() []app.HandlerFunc {
// your code...
return nil
}
func _updatespaceuserroleMw() []app.HandlerFunc {
// your code...
return nil
}
func _listspacemembersMw() []app.HandlerFunc {
// your code...
return nil
}
func _removespacememberMw() []app.HandlerFunc {
// your code...
return nil
}
func _updatespacememberroleMw() []app.HandlerFunc {
// your code...
return nil
}
@@ -0,0 +1,30 @@
// Code generated by hertz generator. DO NOT EDIT.
package space
import (
space "allapp/biz/handler/space"
"github.com/cloudwego/hertz/pkg/app/server"
)
/*
This file will register all the routes of the services in the master idl.
And it will update automatically when you use the "update" command for the idl.
So don't modify the contents of the file, or your code will be deleted when it is updated.
*/
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
{
_space := root.Group("/space", _spaceMw()...)
_space.POST("/create", append(_createspaceMw(), space.CreateSpace)...)
_space.POST("/dissolve", append(_dissolvespaceMw(), space.DissolveSpace)...)
_space.POST("/exit", append(_exitspaceMw(), space.ExitSpace)...)
_space.POST("/join", append(_joinspaceMw(), space.JoinSpace)...)
_space.POST("/members", append(_listspacemembersMw(), space.ListSpaceMembers)...)
_space.POST("/remove", append(_removespacememberMw(), space.RemoveSpaceMember)...)
_space.POST("/updateRole", append(_updatespacememberroleMw(), space.UpdateSpaceMemberRole)...)
}
}
@@ -0,0 +1,27 @@
// Code generated by hertz generator.
package user
import (
"github.com/cloudwego/hertz/pkg/app"
)
func rootMw() []app.HandlerFunc {
// your code...
return nil
}
func _userMw() []app.HandlerFunc {
// your code...
return nil
}
func _listuserspacesMw() []app.HandlerFunc {
// your code...
return nil
}
func _getuserspaceMw() []app.HandlerFunc {
// your code...
return nil
}
@@ -0,0 +1,24 @@
// Code generated by hertz generator. DO NOT EDIT.
package user
import (
user "allapp/biz/handler/user"
"github.com/cloudwego/hertz/pkg/app/server"
)
/*
This file will register all the routes of the services in the master idl.
And it will update automatically when you use the "update" command for the idl.
So don't modify the contents of the file, or your code will be deleted when it is updated.
*/
// Register register routes based on the IDL 'api.${HTTP Method}' annotation.
func Register(r *server.Hertz) {
root := r.Group("/", rootMw()...)
{
_user := root.Group("/user", _userMw()...)
_user.GET("/space", append(_getuserspaceMw(), user.GetUserSpace)...)
}
}