u
This commit is contained in:
1 parent
755fc20c9a
commit
a57070f860
9 files changed
+557
-93
No files matched your search
@@ -8,6 +8,8 @@ import (
|
||||
"cn/oneao/base-go/utils/jwtx"
|
||||
"cn/oneao/base-go/utils/response"
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
auth "cn/oneao/base-go/biz/model/auth"
|
||||
|
||||
@@ -25,13 +27,18 @@ func LoginDefault(ctx context.Context, c *app.RequestContext) {
|
||||
c.String(consts.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
// 处理登录
|
||||
// 假设 ctx 已有,req.Account / req.Password 已有
|
||||
start := time.Now()
|
||||
|
||||
loginUser, err := db.Client.UserInfo.
|
||||
Query().
|
||||
Where(userinfo.AccountEQ(req.Account),
|
||||
userinfo.PasswordEQ(req.Password)).
|
||||
Only(ctx)
|
||||
|
||||
duration := time.Since(start) // 耗时
|
||||
fmt.Printf("登录查询: (耗时: %v)\n", duration)
|
||||
|
||||
if err != nil {
|
||||
c.String(consts.StatusInternalServerError, "账号或密码错误")
|
||||
return
|
||||
|
||||
@@ -13,7 +13,7 @@ func Error() app.HandlerFunc {
|
||||
|
||||
if err := c.Errors.Last(); err != nil {
|
||||
// 未知错误 -> 500
|
||||
c.JSON(500, ErrorResponse{500, "Internal Server Error"})
|
||||
//c.JSON(500, ErrorResponse{500, "Internal Server Error"})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/cloudwego/hertz/pkg/app"
|
||||
"github.com/cloudwego/hertz/pkg/common/hlog"
|
||||
"github.com/hertz-contrib/requestid"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func Log() app.HandlerFunc {
|
||||
return func(c context.Context, ctx *app.RequestContext) {
|
||||
start := time.Now() // 记录开始时间
|
||||
|
||||
// 执行后续处理
|
||||
ctx.Next(c)
|
||||
|
||||
// 计算耗时
|
||||
duration := time.Since(start).Milliseconds()
|
||||
|
||||
hlog.Info("请求完成",
|
||||
zap.String("RequestID", requestid.Get(ctx)),
|
||||
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),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user